Added a single lifetime parameter

pull/36/head
Jef Roosens 2021-08-23 08:17:06 +02:00
parent 159da81b8d
commit 456c947ecd
Signed by: 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 serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::{
@ -33,12 +33,17 @@ pub fn all(conn: &PgConnection) -> crate::Result<Vec<User>>
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()
}
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(())
}

View File

@ -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<Json<Vec<User>>>
Ok(Json(conn.run(|c| rb::db::users::all(c)).await?))
}
#[post("/users", data="<user>")]
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?)
#[post("/users", data = "<user>")]
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?)
}
#[get("/users/<user_id_str>")]
async fn get_user_info(_admin: Admin, conn: RbDbConn, user_id_str: String) -> rb::Result<Json<User>> {
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<Json<User>>
{
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)),