more modular + registration done

This commit is contained in:
Reid 2023-08-27 14:09:59 -07:00
parent 4e2318b852
commit f3908219cd
Signed by: reidlab
GPG key ID: 6C9EAA3364F962C8
11 changed files with 239 additions and 66 deletions

View file

@ -1,12 +1,6 @@
#![feature(decl_macro)]
#[macro_use] extern crate rocket;
use rocket::form::Form;
use rocket::http::Status;
use rocket::response::status;
use diesel::prelude::*;
use diesel::result::Error;
mod db;
use db::*;
@ -14,64 +8,19 @@ use db::*;
mod helpers;
use helpers::*;
mod endpoints;
use endpoints::*;
#[get("/")]
fn index() -> String {
return String::from("index | coming soon to a localhost:8000 near u");
}
#[derive(FromForm)]
struct FormRegisterGJAccount {
userName: String,
password: String,
email: String
}
#[post("/memaddrefix/accounts/registerGJAccount.php", data = "<input>")]
fn register_gj_account(input: Form<FormRegisterGJAccount>) -> status::Custom<&'static str> {
use crate::schema::accounts::dsl::*;
use crate::models::NewAccount;
let connection = &mut establish_connection_pg();
if input.userName != clean::clean(input.userName.as_ref()) {
return status::Custom(Status::Ok, "-4")
}
if input.password.len() < 6 {
return status::Custom(Status::Ok, "-8")
}
if input.userName.len() < 3 {
return status::Custom(Status::Ok, "-9")
}
if input.userName.len() > 20 {
return status::Custom(Status::Ok, "-4")
}
if input.userName.len() > 254 {
return status::Custom(Status::Ok, "-6")
}
let account_name_usage = accounts.filter(username.eq(input.userName.clone())).count().get_result::<i64>(connection) as Result<i64, Error>;
let account_name_used = account_name_usage.expect("Fatal database name query error") != 0;
if account_name_used {
return status::Custom(Status::Ok, "-2")
}
let new_account = NewAccount {
username: input.userName.clone(),
gjp2: helpers::gjp2::get_gjp2_hashed(input.password.clone()),
email: input.email.clone()
};
diesel::insert_into(accounts)
.values(&new_account)
.execute(connection)
.expect("Fatal error saving the new account");
return status::Custom(Status::Ok, "1")
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index, register_gj_account])
rocket::build().mount("/", routes![
index,
endpoints::accounts::register_account::register_account
])
}