From 159da81b8d877ad8001df88c1963782c318b7f1c Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sun, 22 Aug 2021 22:35:07 +0200 Subject: [PATCH 1/2] Started on user management routes --- src/auth.rs | 1 - src/db/users.rs | 24 ++++++++++++++++++++++-- src/errors.rs | 2 ++ src/routes/admin.rs | 22 ++++++++++++++++++++-- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/auth.rs b/src/auth.rs index 613c210..efd991d 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -81,7 +81,6 @@ pub fn generate_jwt_token(conn: &PgConnection, user: &User) -> crate::Result crate::Result> { users.load::(conn).map_err(|_| RBError::DBError) } + +pub fn find(conn: &PgConnection, user_id: Uuid) -> Option { + users.find(user_id).first::(conn).ok() +} + +pub fn create(conn: &PgConnection, new_user: &NewUser) -> crate::Result<()> { + let count = diesel::insert_into(users).values(new_user).execute(conn).map_err(|_| RBError::DBError)?; + + if count == 0 { + return Err(RBError::DuplicateUser); + } + + Ok(()) +} + +pub fn delete(conn: &PgConnection, user_id: Uuid) -> crate::Result<()> { + diesel::delete(users.filter(id.eq(user_id))).execute(conn).map_err(|_| RBError::DBError)?; + + Ok(()) +} diff --git a/src/errors.rs b/src/errors.rs index bc3243f..7072dab 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -28,6 +28,7 @@ pub enum RBError InvalidRefreshToken, DuplicateRefreshToken, DBError, + DuplicateUser, } impl<'r> Responder<'r, 'static> for RBError @@ -46,6 +47,7 @@ impl<'r> Responder<'r, 'static> for RBError RBError::InvalidRefreshToken | RBError::DuplicateRefreshToken => { (Status::Unauthorized, "Invalid refresh token.") } + RBError::DuplicateUser => (Status::Conflict, "User already exists"), _ => (Status::InternalServerError, "Internal server error"), }; diff --git a/src/routes/admin.rs b/src/routes/admin.rs index db3ecc2..1ddce97 100644 --- a/src/routes/admin.rs +++ b/src/routes/admin.rs @@ -1,11 +1,14 @@ -use rb::db::users::User; +use rb::db::users::{User, NewUser}; +use rb::db::users as db_users; +use rb::errors::RBError; use rocket::serde::json::Json; +use uuid::Uuid; use crate::{guards::Admin, RbDbConn}; pub fn routes() -> Vec { - routes![get_users] + routes![get_users, get_user_info] } #[get("/users")] @@ -13,3 +16,18 @@ async fn get_users(admin: Admin, conn: RbDbConn) -> rb::Result>> { Ok(Json(conn.run(|c| rb::db::users::all(c)).await?)) } + +#[post("/users", data="")] +async fn create_user(admin: Admin, conn: RbDbConn, user: Json) -> rb::Result<()> { + Ok(conn.run(move |c| db_users::create(c, &user.into_inner())).await?) +} + +#[get("/users/")] +async fn get_user_info(_admin: Admin, conn: RbDbConn, user_id_str: String) -> rb::Result> { + let user_id = Uuid::parse_str(&user_id_str).map_err(|_| RBError::UnknownUser)?; + + match conn.run(move |c| db_users::find(c, user_id)).await { + Some(user) => Ok(Json(user)), + None => Err(RBError::UnknownUser), + } +} From 456c947ecddaac28610863edee53bdab1a67928f Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 23 Aug 2021 08:17:06 +0200 Subject: [PATCH 2/2] Added a single lifetime parameter --- src/db/users.rs | 20 ++++++++++++++------ src/routes/admin.rs | 24 ++++++++++++++++-------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/db/users.rs b/src/db/users.rs index b1bcfe6..b005295 100644 --- a/src/db/users.rs +++ b/src/db/users.rs @@ -1,5 +1,5 @@ use diesel::{prelude::*, AsChangeset, Insertable, Queryable}; -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; use uuid::Uuid; use crate::{ @@ -33,12 +33,17 @@ pub fn all(conn: &PgConnection) -> crate::Result> users.load::(conn).map_err(|_| RBError::DBError) } -pub fn find(conn: &PgConnection, user_id: Uuid) -> Option { +pub fn find(conn: &PgConnection, user_id: Uuid) -> Option +{ users.find(user_id).first::(conn).ok() } -pub fn create(conn: &PgConnection, new_user: &NewUser) -> crate::Result<()> { - let count = diesel::insert_into(users).values(new_user).execute(conn).map_err(|_| RBError::DBError)?; +pub fn create(conn: &PgConnection, new_user: &NewUser) -> crate::Result<()> +{ + let count = diesel::insert_into(users) + .values(new_user) + .execute(conn) + .map_err(|_| RBError::DBError)?; if count == 0 { return Err(RBError::DuplicateUser); @@ -47,8 +52,11 @@ pub fn create(conn: &PgConnection, new_user: &NewUser) -> crate::Result<()> { Ok(()) } -pub fn delete(conn: &PgConnection, user_id: Uuid) -> crate::Result<()> { - diesel::delete(users.filter(id.eq(user_id))).execute(conn).map_err(|_| RBError::DBError)?; +pub fn delete(conn: &PgConnection, user_id: Uuid) -> crate::Result<()> +{ + diesel::delete(users.filter(id.eq(user_id))) + .execute(conn) + .map_err(|_| RBError::DBError)?; Ok(()) } diff --git a/src/routes/admin.rs b/src/routes/admin.rs index 1ddce97..b3db29b 100644 --- a/src/routes/admin.rs +++ b/src/routes/admin.rs @@ -1,6 +1,10 @@ -use rb::db::users::{User, NewUser}; -use rb::db::users as db_users; -use rb::errors::RBError; +use rb::{ + db::{ + users as db_users, + users::{NewUser, User}, + }, + errors::RBError, +}; use rocket::serde::json::Json; use uuid::Uuid; @@ -17,14 +21,18 @@ async fn get_users(admin: Admin, conn: RbDbConn) -> rb::Result>> Ok(Json(conn.run(|c| rb::db::users::all(c)).await?)) } -#[post("/users", data="")] -async fn create_user(admin: Admin, conn: RbDbConn, user: Json) -> rb::Result<()> { - Ok(conn.run(move |c| db_users::create(c, &user.into_inner())).await?) +#[post("/users", data = "")] +async fn create_user(admin: Admin, conn: RbDbConn, user: Json) -> rb::Result<()> +{ + Ok(conn + .run(move |c| db_users::create(c, &user.into_inner())) + .await?) } #[get("/users/")] -async fn get_user_info(_admin: Admin, conn: RbDbConn, user_id_str: String) -> rb::Result> { - let user_id = Uuid::parse_str(&user_id_str).map_err(|_| RBError::UnknownUser)?; +async fn get_user_info(_admin: Admin, conn: RbDbConn, user_id_str: &str) -> rb::Result> +{ + let user_id = Uuid::parse_str(user_id_str).map_err(|_| RBError::UnknownUser)?; match conn.run(move |c| db_users::find(c, user_id)).await { Some(user) => Ok(Json(user)),