Added a single lifetime parameter

develop
Jef Roosens 2021-08-23 08:17:06 +02:00
parent 159da81b8d
commit 456c947ecd
Signed by untrusted user: Jef Roosens
GPG Key ID: 955C0660072F691F
2 changed files with 30 additions and 14 deletions

View File

@ -1,5 +1,5 @@
use diesel::{prelude::*, AsChangeset, Insertable, Queryable}; use diesel::{prelude::*, AsChangeset, Insertable, Queryable};
use serde::{Serialize, Deserialize}; use serde::{Deserialize, Serialize};
use uuid::Uuid; use uuid::Uuid;
use crate::{ use crate::{
@ -33,12 +33,17 @@ pub fn all(conn: &PgConnection) -> crate::Result<Vec<User>>
users.load::<User>(conn).map_err(|_| RBError::DBError) users.load::<User>(conn).map_err(|_| RBError::DBError)
} }
pub fn find(conn: &PgConnection, user_id: Uuid) -> Option<User> { pub fn find(conn: &PgConnection, user_id: Uuid) -> Option<User>
{
users.find(user_id).first::<User>(conn).ok() users.find(user_id).first::<User>(conn).ok()
} }
pub fn create(conn: &PgConnection, new_user: &NewUser) -> crate::Result<()> { 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)?; {
let count = diesel::insert_into(users)
.values(new_user)
.execute(conn)
.map_err(|_| RBError::DBError)?;
if count == 0 { if count == 0 {
return Err(RBError::DuplicateUser); return Err(RBError::DuplicateUser);
@ -47,8 +52,11 @@ pub fn create(conn: &PgConnection, new_user: &NewUser) -> crate::Result<()> {
Ok(()) Ok(())
} }
pub fn delete(conn: &PgConnection, user_id: Uuid) -> crate::Result<()> { pub fn delete(conn: &PgConnection, user_id: Uuid) -> crate::Result<()>
diesel::delete(users.filter(id.eq(user_id))).execute(conn).map_err(|_| RBError::DBError)?; {
diesel::delete(users.filter(id.eq(user_id)))
.execute(conn)
.map_err(|_| RBError::DBError)?;
Ok(()) Ok(())
} }

View File

@ -1,6 +1,10 @@
use rb::db::users::{User, NewUser}; use rb::{
use rb::db::users as db_users; db::{
use rb::errors::RBError; users as db_users,
users::{NewUser, User},
},
errors::RBError,
};
use rocket::serde::json::Json; use rocket::serde::json::Json;
use uuid::Uuid; use uuid::Uuid;
@ -17,14 +21,18 @@ async fn get_users(admin: Admin, conn: RbDbConn) -> rb::Result<Json<Vec<User>>>
Ok(Json(conn.run(|c| rb::db::users::all(c)).await?)) Ok(Json(conn.run(|c| rb::db::users::all(c)).await?))
} }
#[post("/users", data="<user>")] #[post("/users", data = "<user>")]
async fn create_user(admin: Admin, conn: RbDbConn, user: Json<NewUser>) -> rb::Result<()> { async fn create_user(admin: Admin, conn: RbDbConn, user: Json<NewUser>) -> rb::Result<()>
Ok(conn.run(move |c| db_users::create(c, &user.into_inner())).await?) {
Ok(conn
.run(move |c| db_users::create(c, &user.into_inner()))
.await?)
} }
#[get("/users/<user_id_str>")] #[get("/users/<user_id_str>")]
async fn get_user_info(_admin: Admin, conn: RbDbConn, user_id_str: String) -> rb::Result<Json<User>> { async fn get_user_info(_admin: Admin, conn: RbDbConn, user_id_str: &str) -> rb::Result<Json<User>>
let user_id = Uuid::parse_str(&user_id_str).map_err(|_| RBError::UnknownUser)?; {
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 { match conn.run(move |c| db_users::find(c, user_id)).await {
Some(user) => Ok(Json(user)), Some(user) => Ok(Json(user)),