diff --git a/src/db/posts.rs b/src/db/posts.rs index 019e9cc..5a9e1ee 100644 --- a/src/db/posts.rs +++ b/src/db/posts.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use uuid::Uuid; use crate::{ - errors::{RbError, RbResult}, + errors::{RbError, RbResult, RbOption}, schema::{posts, posts::dsl::*}, }; @@ -48,6 +48,15 @@ pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult .map_err(|_| RbError::DbError("Couldn't query posts."))?) } +pub fn find(conn: &PgConnection, id_: &Uuid) -> RbOption +{ + match posts.find(id_).first(conn) { + Ok(val) => Ok(Some(val)), + Err(diesel::NotFound) => Ok(None), + _ => Err(RbError::DbError("Couldn't find post.")), + } +} + pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult { Ok(insert_into(posts) diff --git a/src/db/sections.rs b/src/db/sections.rs index 4adcc35..f18518a 100644 --- a/src/db/sections.rs +++ b/src/db/sections.rs @@ -12,6 +12,7 @@ pub struct Section { pub id: Uuid, pub title: String, + pub shortname: String, pub description: Option, pub is_default: bool, pub has_titles: bool, @@ -23,6 +24,7 @@ pub struct Section pub struct NewSection { title: String, + pub shortname: String, description: Option, is_default: Option, has_titles: Option, @@ -34,6 +36,7 @@ pub struct NewSection pub struct PatchSection { title: Option, + shortname: Option, description: Option, is_default: Option, has_titles: Option, diff --git a/src/errors.rs b/src/errors.rs index 1f9aff3..561fef7 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -87,4 +87,8 @@ impl<'r> Responder<'r, 'static> for RbError } } +/// Type alias for results that can return an RbError pub type RbResult = std::result::Result; + +/// Type alias for optional results that can fail & return an RbError +pub type RbOption = RbResult>; diff --git a/src/posts.rs b/src/posts.rs index c62dae7..cb11582 100644 --- a/src/posts.rs +++ b/src/posts.rs @@ -1,6 +1,6 @@ use rocket::serde::json::Json; -use crate::{db, errors::RbResult, guards::Admin, RbDbConn}; +use crate::{db, errors::RbResult, errors::RbOption, guards::Admin, RbDbConn}; #[get("/?&")] pub async fn get(conn: RbDbConn, offset: u32, limit: u32) -> RbResult>> @@ -23,6 +23,12 @@ pub async fn create( )) } +#[get("/")] +pub async fn find(conn: RbDbConn, id: uuid::Uuid) -> RbOption> +{ + Ok(conn.run(move |c| db::posts::find(c, &id)).await?.and_then(|p| Some(Json(p)))) +} + #[patch("/", data = "")] pub async fn patch( _admin: Admin, diff --git a/src/schema.rs b/src/schema.rs index 45b9813..d8b585e 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -21,6 +21,7 @@ table! { sections (id) { id -> Uuid, title -> Varchar, + shortname -> Varchar, description -> Nullable, is_default -> Bool, has_titles -> Bool, @@ -40,4 +41,9 @@ table! { joinable!(posts -> sections (section_id)); joinable!(refresh_tokens -> users (user_id)); -allow_tables_to_appear_in_same_query!(posts, refresh_tokens, sections, users,); +allow_tables_to_appear_in_same_query!( + posts, + refresh_tokens, + sections, + users, +);