From 8919837d4d77fe51cdb958922cfb94db18b7a58a Mon Sep 17 00:00:00 2001 From: vanten-s Date: Sun, 27 Aug 2023 17:15:13 +0200 Subject: [PATCH] Changed so many things --- Cargo.toml | 2 +- src/main.rs | 96 ++++++++++++++++++++++++++++++----------------------- 2 files changed, 56 insertions(+), 42 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 15c2f7c..5b35bd0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,4 @@ edition = "2021" [dependencies] actix-web = "4.3.1" backend = { path = "../backend" } -serde_json = "1.0.105" +serde_json = { version = "1.0.105", features = ["std"] } diff --git a/src/main.rs b/src/main.rs index 4cb4a6f..affa6c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ extern crate backend; use actix_web::{get, post, App, HttpResponse, HttpServer, Responder}; -use serde_json::Value; +use actix_web::http::header::ContentType; +use serde_json::{json, Value}; macro_rules! get_value_str { ($i:expr) => { @@ -23,7 +24,8 @@ macro_rules! get_value_int { #[post("/new_user")] async fn createuser(req_body: String) -> impl Responder { - use backend::UserCreationStatus::*; + use backend::UserCreationStatus; + use backend::UserSelectionStatus; let connection = &mut backend::create_connection(); @@ -34,26 +36,41 @@ async fn createuser(req_body: String) -> impl Responder { } }; - let email: &str = v["email"].as_str().unwrap(); - let username: &str = v["username"].as_str().unwrap(); - let password: &str = v["password"].as_str().unwrap(); + let email = String::from(v["email"].as_str().unwrap()); + let username = String::from(v["username"].as_str().unwrap()); + let password = String::from(v["password"].as_str().unwrap()); - match backend::add_user( - String::from(username), - String::from(email), - String::from(password), - connection, - ) { - Succesful => HttpResponse::Ok().body("Succesful"), - UserExists => HttpResponse::Conflict().body("User Exists already"), - QueryError(_) => HttpResponse::InternalServerError().body("Query Error"), - DatabaseError(_) => HttpResponse::InternalServerError().body("Database Error"), - } + let status: UserCreationStatus = backend::add_user( + username, + email.clone(), + password, + connection + ); + + let id = match backend::get_id_from_email(email, connection) { + UserSelectionStatus::Success(v) => v, + _ => -1 + }; + + let (status, status_code, mut builder) = match status { + UserCreationStatus::Success => ("Success", 200, HttpResponse::Ok()), + UserCreationStatus::UserExists => ("User Already Exists", 400, HttpResponse::Conflict()), + UserCreationStatus::DatabaseError(_) => ("Database Error", 500, HttpResponse::InternalServerError()), + UserCreationStatus::QueryError(_) => ("Query Error", 500, HttpResponse::InternalServerError()) + }; + + builder.insert_header(ContentType::json()); + + builder.body(json!({ + "status": status, + "status_code": status_code, + "id": id + }).to_string()) } -#[post("/check_password")] +#[get("/check_password")] async fn check_password(req_body: String) -> impl Responder { - use backend::PasswordCheckStatus; + use backend::UserSelectionStatus; let connection = &mut backend::create_connection(); @@ -68,11 +85,26 @@ async fn check_password(req_body: String) -> impl Responder { let pass = get_value_str!(v["password"].clone()); match backend::check_password(id, pass, connection) { - PasswordCheckStatus::Correct => HttpResponse::Ok().body(""), - PasswordCheckStatus::Incorrect => HttpResponse::Unauthorized().body(""), - PasswordCheckStatus::UserDoesntExist => HttpResponse::ExpectationFailed().body(""), - PasswordCheckStatus::DatabaseError(_) => HttpResponse::InternalServerError().body(""), - PasswordCheckStatus::QueryError(_) => HttpResponse::InternalServerError().body("") + UserSelectionStatus::Success(b) => HttpResponse::Ok().insert_header(ContentType::json()).body(json!({ + "correct": b, + "status": "Success", + "status_code": 200 + }).to_string()), + UserSelectionStatus::UserDoesntExist => HttpResponse::ExpectationFailed().insert_header(ContentType::json()).body(json!({ + "correct": false, + "status": "User Doesn't Exist", + "status_code": 400 + }).to_string()), + UserSelectionStatus::DatabaseError(_) => HttpResponse::InternalServerError().insert_header(ContentType::json()).body(json!({ + "correct": false, + "status": "Database Error", + "status_code": 500 + }).to_string()), + UserSelectionStatus::QueryError(_) => HttpResponse::InternalServerError().insert_header(ContentType::json()).body(json!({ + "correct": false, + "status": "Query Error", + "status_code": 500 + }).to_string()) } } @@ -82,22 +114,4 @@ async fn main() -> std::io::Result<()> { .bind(("127.0.0.1", 8080))? .run() .await - - /*match backend::add_user( - String::from("vanten-s"), - String::from("vanten-s@vanten-s.com"), - String::from("XFAZ7EgjM1CKLK66QDGr0FmceqWHgtvsiMs7GRIi"), - &mut connection, - ) { - UserCreationStatus::UserExists => println!("User already exists"), - _ => println!("Got another error"), - }*/ - - /*match backend::check_password(18, String::from("12345"), &mut connection) { - Ok(v) => match v { - true => println!("CORRECT LES FUCKING GO"), - false => println!("NO ITS FALSEEE") - }, - Err(_) => println!("Error :(") - }*/ }