diff options
author | Curly Bryce <curlybryce@protonmail.com> | 2024-07-02 20:00:22 -0600 |
---|---|---|
committer | Curly Bryce <curlybryce@protonmail.com> | 2024-07-02 20:00:22 -0600 |
commit | 37bee342bb8ce8a487a150fb236eca5b0dc82266 (patch) | |
tree | 5510c252e0f5ff2119a6e53933b6b2be03e0b743 /src/main.rs | |
parent | 8fae88530f6cfdb208f2adf387bb8a7da3fbccb6 (diff) | |
download | poko_server-37bee342bb8ce8a487a150fb236eca5b0dc82266.tar.gz poko_server-37bee342bb8ce8a487a150fb236eca5b0dc82266.tar.bz2 poko_server-37bee342bb8ce8a487a150fb236eca5b0dc82266.zip |
update status codes
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/src/main.rs b/src/main.rs index 9d7dfd0..7b5809a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -105,6 +105,12 @@ impl DB { async fn new_user(&mut self, name: String, id: UID) -> Result<(), String> { let user = User::new(name, id); + for u in self.users.clone() { + if u.name == user.name { + return Err("Name taken".to_string()) + } + } + self.users.push(user); self.save().await } @@ -136,22 +142,19 @@ struct UserForm { #[post("/user", data="<data>", format="json")] async fn new_user(data: Json<UserForm>, db: &State<Mutex<DB>>) -> (Status, Result<(), Json<String>>) { let mut db = db.lock().await; + let id; + if data.id == "random".to_string() { match db.uid_generator.new_uid() { // Gotten a new UID to make a new user with - Ok(n) => match db.new_user(data.name.clone(), n).await { - // User has been created - Ok(_) => return (Status::Ok, Ok(())), - // Could not create user - Err(n) => return (Status::InternalServerError, Err(n.into())), - }, + Ok(n) => id = Some(n), // Did not get a new UID Err(n) => return (Status::InternalServerError, Err(n.clone().into())), - } + }; } else { match UID::from(data.id.clone()) { // Could not make UID from input - Err(n) => (Status::BadRequest, Err(n.into())), + Err(n) => return (Status::BadRequest, Err(n.into())), // Made a UID Ok(n) => { if db.uid_generator.is_taken(n) { @@ -163,15 +166,22 @@ async fn new_user(data: Json<UserForm>, db: &State<Mutex<DB>>) -> (Status, Resul // Could not add UID for some reason Err(n) => return (Status::InternalServerError, Err(n.into())), // Made - Ok(n) => match db.new_user(data.name.clone(), n).await { - // User has been created - Ok(_) => return (Status::Ok, Ok(())), - // Could not create user - Err(n) => return (Status::InternalServerError, Err(n.into())), - }, - } + Ok(n) => id = Some(n), + }; } }, + }; + } + + match id { + None => return (Status::InternalServerError, Err("".to_string().into())), + Some(n) => { + match db.new_user(data.name.clone(), n).await { + // User has been created + Ok(_) => return (Status::Ok, Ok(())), + // Could not create user + Err(n) => return (Status::InternalServerError, Err(n.into())), + } } } } |