Changed so many things

This commit is contained in:
vanten-s 2023-08-27 17:15:13 +02:00
parent a526b83697
commit 8919837d4d
Signed by: vanten-s
GPG key ID: DE3060396884D3F2
2 changed files with 56 additions and 42 deletions

View file

@ -8,4 +8,4 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = "4.3.1" actix-web = "4.3.1"
backend = { path = "../backend" } backend = { path = "../backend" }
serde_json = "1.0.105" serde_json = { version = "1.0.105", features = ["std"] }

View file

@ -1,7 +1,8 @@
extern crate backend; extern crate backend;
use actix_web::{get, post, App, HttpResponse, HttpServer, Responder}; 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 { macro_rules! get_value_str {
($i:expr) => { ($i:expr) => {
@ -23,7 +24,8 @@ macro_rules! get_value_int {
#[post("/new_user")] #[post("/new_user")]
async fn createuser(req_body: String) -> impl Responder { async fn createuser(req_body: String) -> impl Responder {
use backend::UserCreationStatus::*; use backend::UserCreationStatus;
use backend::UserSelectionStatus;
let connection = &mut backend::create_connection(); 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 email = String::from(v["email"].as_str().unwrap());
let username: &str = v["username"].as_str().unwrap(); let username = String::from(v["username"].as_str().unwrap());
let password: &str = v["password"].as_str().unwrap(); let password = String::from(v["password"].as_str().unwrap());
match backend::add_user( let status: UserCreationStatus = backend::add_user(
String::from(username), username,
String::from(email), email.clone(),
String::from(password), password,
connection, connection
) { );
Succesful => HttpResponse::Ok().body("Succesful"),
UserExists => HttpResponse::Conflict().body("User Exists already"), let id = match backend::get_id_from_email(email, connection) {
QueryError(_) => HttpResponse::InternalServerError().body("Query Error"), UserSelectionStatus::Success(v) => v,
DatabaseError(_) => HttpResponse::InternalServerError().body("Database Error"), _ => -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 { async fn check_password(req_body: String) -> impl Responder {
use backend::PasswordCheckStatus; use backend::UserSelectionStatus;
let connection = &mut backend::create_connection(); 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()); let pass = get_value_str!(v["password"].clone());
match backend::check_password(id, pass, connection) { match backend::check_password(id, pass, connection) {
PasswordCheckStatus::Correct => HttpResponse::Ok().body(""), UserSelectionStatus::Success(b) => HttpResponse::Ok().insert_header(ContentType::json()).body(json!({
PasswordCheckStatus::Incorrect => HttpResponse::Unauthorized().body(""), "correct": b,
PasswordCheckStatus::UserDoesntExist => HttpResponse::ExpectationFailed().body(""), "status": "Success",
PasswordCheckStatus::DatabaseError(_) => HttpResponse::InternalServerError().body(""), "status_code": 200
PasswordCheckStatus::QueryError(_) => HttpResponse::InternalServerError().body("") }).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))? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .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 :(")
}*/
} }