diff --git a/src/db/posts.rs b/src/db/posts.rs index e729fec..677ed20 100644 --- a/src/db/posts.rs +++ b/src/db/posts.rs @@ -18,8 +18,9 @@ pub struct Post pub content: String, } -#[derive(Insertable)] +#[derive(Deserialize, Insertable)] #[table_name = "posts"] +#[serde(rename_all = "camelCase")] pub struct NewPost { pub section_id: Uuid, @@ -32,7 +33,6 @@ pub struct NewPost #[table_name = "posts"] pub struct PatchPost { - pub id: Option, pub section_id: Option, pub title: Option, pub publish_date: Option, @@ -48,12 +48,6 @@ pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult .map_err(|_| RbError::DbError("Couldn't query posts."))?) } -/// Insert a new post into the database. -/// -/// # Arguments -/// -/// * `conn` - reference to a database connection -/// * `new_post` - the new post object to insert pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult { Ok(insert_into(posts) @@ -64,15 +58,15 @@ pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult // TODO check for conflict? } -pub fn update(conn: &PgConnection, post: &PatchPost) -> RbResult +pub fn update(conn: &PgConnection, post_id: &Uuid, patch_post: &PatchPost) -> RbResult { - Ok(diesel::update(posts) - .set(post) + Ok(diesel::update(posts.filter(id.eq(post_id))) + .set(patch_post) .get_result::(conn) .map_err(|_| RbError::DbError("Couldn't update post."))?) } -pub fn delete(conn: &PgConnection, post_id: Uuid) -> RbResult<()> +pub fn delete(conn: &PgConnection, post_id: &Uuid) -> RbResult<()> { diesel::delete(posts.filter(id.eq(post_id))) .execute(conn) diff --git a/src/db/sections.rs b/src/db/sections.rs index b429c85..5c69352 100644 --- a/src/db/sections.rs +++ b/src/db/sections.rs @@ -1,7 +1,5 @@ -//! Handles all section-related database operations. - use diesel::{insert_into, prelude::*, Insertable, PgConnection, Queryable}; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use uuid::Uuid; use crate::{ @@ -9,8 +7,7 @@ use crate::{ schema::{sections, sections::dsl::*}, }; -/// Represents a section contained in the database. -#[derive(Queryable)] +#[derive(Queryable, Serialize)] pub struct Section { pub id: Uuid, @@ -20,7 +17,6 @@ pub struct Section pub has_titles: bool, } -/// A new section to be added into the database. #[derive(Deserialize, Insertable)] #[table_name = "sections"] #[serde(rename_all = "camelCase")] @@ -32,32 +28,49 @@ pub struct NewSection has_titles: Option, } -/// Returns all sections in the database. -/// -/// # Arguments -/// -/// * `conn` - reference to a database connection -pub fn all(conn: &PgConnection) -> RbResult> +#[derive(Deserialize, AsChangeset)] +#[table_name = "sections"] +#[serde(rename_all = "camelCase")] +pub struct PatchSection { - sections - .load::
(conn) - .map_err(|_| RbError::DbError("Couldn't get all sections")) + title: Option, + description: Option, + is_default: Option, + has_titles: Option, } -/// Inserts a new section into the database. -/// -/// # Arguments -/// -/// * `conn` - reference to a database connection -/// * `new_section` - the new section to be added -pub fn create(conn: &PgConnection, new_section: &NewSection) -> RbResult<()> +pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult> { - insert_into(sections) - .values(new_section) - .execute(conn) - .map_err(|_| RbError::DbError("Couldn't insert section."))?; + Ok(sections + .offset(offset_.into()) + .limit(limit_.into()) + .load::
(conn) + .map_err(|_| RbError::DbError("Couldn't query sections."))?) +} + +pub fn create(conn: &PgConnection, new_post: &NewSection) -> RbResult
+{ + Ok(insert_into(sections) + .values(new_post) + .get_result::
(conn) + .map_err(|_| RbError::DbError("Couldn't insert section."))?) // TODO check for conflict? +} + +pub fn update(conn: &PgConnection, post_id: &Uuid, patch_post: &PatchSection) -> RbResult
+{ + Ok(diesel::update(sections.filter(id.eq(post_id))) + .set(patch_post) + .get_result::
(conn) + .map_err(|_| RbError::DbError("Couldn't update section."))?) +} + +pub fn delete(conn: &PgConnection, post_id: &Uuid) -> RbResult<()> +{ + diesel::delete(sections.filter(id.eq(post_id))) + .execute(conn) + .map_err(|_| RbError::DbError("Couldn't delete section."))?; Ok(()) }