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,6 +1,6 @@
use diesel::prelude::*;
use serde::{Serialize, Deserialize};
use super::schema::accounts;
use super::schema::*;
#[derive(Queryable, Serialize)]
pub struct Account {
@ -30,4 +30,55 @@ pub struct NewAccount {
pub username: String,
pub gjp2: String,
pub email: String
}
#[derive(Queryable, Serialize)]
pub struct User {
pub id: i32,
pub udid: Option<String>,
pub account_id: Option<i32>,
pub registered: i32,
pub username: String,
pub stars: i32,
pub demons: i32,
pub coins: i32,
pub user_coins: i32,
pub diamonds: i32,
pub orbs: i32,
pub creator_points: i32,
pub completed_levels: i32,
pub icon_type: i32,
pub color1: i32,
pub color2: i32,
pub cube: i32,
pub ship: i32,
pub ball: i32,
pub ufo: i32,
pub wave: i32,
pub robot: i32,
pub spider: i32,
pub swing_copter: i32,
pub explosion: i32,
pub special: i32,
pub glow: i32,
pub created_at: String,
pub last_played: String,
pub is_banned: i32,
pub is_banned_upload: i32
}
// TODO: err uhh we might need to make changes because green users 😀😀😀 im gonna commit suicide
#[derive(Insertable, Deserialize)]
#[diesel(table_name = users)]
pub struct NewUser {
pub account_id: i32,
pub username: String,
pub registered: i32
}

View file

@ -18,6 +18,49 @@ diesel::table! {
twitter_url -> Nullable<Varchar>,
#[max_length = 20]
twitch_url -> Nullable<Varchar>,
created_at -> Timestamp,
created_at -> Text,
}
}
diesel::table! {
users (id) {
id -> Int4,
udid -> Nullable<Text>,
account_id -> Nullable<Int4>,
registered -> Int4,
username -> Text,
stars -> Int4,
demons -> Int4,
coins -> Int4,
user_coins -> Int4,
diamonds -> Int4,
orbs -> Int4,
creator_points -> Int4,
completed_levels -> Int4,
icon_type -> Int4,
color1 -> Int4,
color2 -> Int4,
cube -> Int4,
ship -> Int4,
ball -> Int4,
ufo -> Int4,
wave -> Int4,
robot -> Int4,
spider -> Int4,
swing_copter -> Int4,
explosion -> Int4,
special -> Int4,
glow -> Int4,
created_at -> Text,
last_played -> Text,
is_banned -> Int4,
is_banned_upload -> Int4,
}
}
diesel::joinable!(users -> accounts (account_id));
diesel::allow_tables_to_appear_in_same_query!(
accounts,
users,
);

1
src/endpoints.rs Normal file
View file

@ -0,0 +1 @@
pub mod accounts;

View file

@ -0,0 +1 @@
pub mod register_account;

View file

@ -0,0 +1,85 @@
use rocket::form::Form;
use rocket::http::Status;
use rocket::response::status;
use diesel::prelude::*;
use diesel::result::Error;
use crate::helpers;
use crate::db;
#[derive(FromForm)]
pub struct FormRegisterAccount {
userName: String,
password: String,
email: String
}
#[post("/memaddrefix/accounts/registerGJAccount.php", data = "<input>")]
pub fn register_account(input: Form<FormRegisterAccount>) -> status::Custom<&'static str> {
let connection = &mut db::establish_connection_pg();
if input.userName != helpers::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.email.len() > 254 {
return status::Custom(Status::Ok, "-6")
}
// account management
use crate::models::{Account, NewAccount};
let inserted_account: Account;
{
use crate::schema::accounts::dsl::*;
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()
};
inserted_account = diesel::insert_into(accounts)
.values(&new_account)
.get_result::<Account, >(connection)
.expect("Fatal error saving the new account");
}
// user management
use crate::models::{User, NewUser};
{
use crate::schema::users::dsl::*;
let new_user = NewUser {
account_id: inserted_account.id,
username: input.userName.clone(),
registered: 1
};
diesel::insert_into(users)
.values(&new_user)
.get_result::<User, >(connection)
.expect("Fatal error saving the new user");
}
return status::Custom(Status::Ok, "1")
}

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
])
}