aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCurly Bryce <curlybryce@protonmail.com>2024-07-02 20:00:22 -0600
committerCurly Bryce <curlybryce@protonmail.com>2024-07-02 20:00:22 -0600
commit37bee342bb8ce8a487a150fb236eca5b0dc82266 (patch)
tree5510c252e0f5ff2119a6e53933b6b2be03e0b743
parent8fae88530f6cfdb208f2adf387bb8a7da3fbccb6 (diff)
downloadpoko_server-37bee342bb8ce8a487a150fb236eca5b0dc82266.tar.gz
poko_server-37bee342bb8ce8a487a150fb236eca5b0dc82266.tar.bz2
poko_server-37bee342bb8ce8a487a150fb236eca5b0dc82266.zip
update status codes
-rw-r--r--TODO2
-rw-r--r--src/main.rs40
2 files changed, 26 insertions, 16 deletions
diff --git a/TODO b/TODO
index 5525133..5920364 100644
--- a/TODO
+++ b/TODO
@@ -1,4 +1,4 @@
-Err to not return 200 OK
+Need to make a proper http status code thingy
Deleting of users
Cookies
Transfers
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())),
+ }
}
}
}