aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCurly Bryce <curlybryce@protonmail.com>2024-07-03 20:06:02 -0600
committerCurly Bryce <curlybryce@protonmail.com>2024-07-03 20:06:02 -0600
commita975f5a0cd1f3849554d52930f06e2d5e42d4a6c (patch)
treec7d2a452b6e4bdec509033ec12acbafb8a39829d
parent11ba5c3d7e039c3c092e45fdd55c0c6a9e144842 (diff)
downloadpoko_server-a975f5a0cd1f3849554d52930f06e2d5e42d4a6c.tar.gz
poko_server-a975f5a0cd1f3849554d52930f06e2d5e42d4a6c.tar.bz2
poko_server-a975f5a0cd1f3849554d52930f06e2d5e42d4a6c.zip
register now returns name and id
-rw-r--r--README.md1
-rw-r--r--src/db.rs10
-rw-r--r--src/main.rs4
3 files changed, 10 insertions, 5 deletions
diff --git a/README.md b/README.md
index abb254b..4a489c0 100644
--- a/README.md
+++ b/README.md
@@ -23,6 +23,7 @@
- ID is optional
- Response
- 201 Created
+ - JSON `{"name":"NAME", "id": "ID"}`
- 400 Bad Request
- JSON `"UID is taken"`
- 500 Internal Server Error
diff --git a/src/db.rs b/src/db.rs
index 85e259b..c655cf7 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -235,10 +235,14 @@ impl DB {
}
}
- pub async fn new_user(&mut self, name: String, password: String, id: UID) -> Result<(), String> {
+ pub async fn new_user(&mut self, name: String, password: String, id: UID) -> Result<User, String> {
let user = User::new(name, password, id);
- self.users.push(user);
- self.save().await
+ self.users.push(user.clone());
+ match self.save().await {
+ Ok(_) => Ok(user),
+ Err(n) => Err(n),
+ }
+
}
pub async fn login(&mut self, id: UID, password: &String, clientid: &String) -> Result<String, String> {
diff --git a/src/main.rs b/src/main.rs
index 62ae685..e65c8c5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -81,7 +81,7 @@ struct UserForm {
id: UID,
}
#[post("/register", data="<data>", format="json")]
-async fn new_user(data: Json<UserForm>, db: &State<Mutex<DB>>) -> (Status, Result<(), Json<String>>) {
+async fn new_user(data: Json<UserForm>, db: &State<Mutex<DB>>) -> (Status, Result<Json<UserOut>, Json<String>>) {
let mut db = db.lock().await;
let id;
@@ -112,7 +112,7 @@ async fn new_user(data: Json<UserForm>, db: &State<Mutex<DB>>) -> (Status, Resul
Some(n) => {
match db.new_user(data.name.clone(), data.password.clone(), n).await {
// User has been created
- Ok(_) => return (Status::Created, Ok(())),
+ Ok(u) => return (Status::Created, Ok(Json(u.into()))),
// Could not create user
Err(n) => return (Status::InternalServerError, Err(n.into())),
}