From 456c947ecddaac28610863edee53bdab1a67928f Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 23 Aug 2021 08:17:06 +0200 Subject: [PATCH] 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)),