Some more db boilerplate

develop
Jef Roosens 2021-09-26 19:02:17 +02:00
parent f6e9039b59
commit 769d7a32de
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
2 changed files with 45 additions and 38 deletions

View File

@ -18,8 +18,9 @@ pub struct Post
pub content: String, pub content: String,
} }
#[derive(Insertable)] #[derive(Deserialize, Insertable)]
#[table_name = "posts"] #[table_name = "posts"]
#[serde(rename_all = "camelCase")]
pub struct NewPost pub struct NewPost
{ {
pub section_id: Uuid, pub section_id: Uuid,
@ -32,7 +33,6 @@ pub struct NewPost
#[table_name = "posts"] #[table_name = "posts"]
pub struct PatchPost pub struct PatchPost
{ {
pub id: Option<Uuid>,
pub section_id: Option<Uuid>, pub section_id: Option<Uuid>,
pub title: Option<String>, pub title: Option<String>,
pub publish_date: Option<NaiveDate>, pub publish_date: Option<NaiveDate>,
@ -48,12 +48,6 @@ pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult<Vec<Post>
.map_err(|_| RbError::DbError("Couldn't query posts."))?) .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<Post> pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult<Post>
{ {
Ok(insert_into(posts) Ok(insert_into(posts)
@ -64,15 +58,15 @@ pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult<Post>
// TODO check for conflict? // TODO check for conflict?
} }
pub fn update(conn: &PgConnection, post: &PatchPost) -> RbResult<Post> pub fn update(conn: &PgConnection, post_id: &Uuid, patch_post: &PatchPost) -> RbResult<Post>
{ {
Ok(diesel::update(posts) Ok(diesel::update(posts.filter(id.eq(post_id)))
.set(post) .set(patch_post)
.get_result::<Post>(conn) .get_result::<Post>(conn)
.map_err(|_| RbError::DbError("Couldn't update post."))?) .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))) diesel::delete(posts.filter(id.eq(post_id)))
.execute(conn) .execute(conn)

View File

@ -1,7 +1,5 @@
//! Handles all section-related database operations.
use diesel::{insert_into, prelude::*, Insertable, PgConnection, Queryable}; use diesel::{insert_into, prelude::*, Insertable, PgConnection, Queryable};
use serde::Deserialize; use serde::{Deserialize, Serialize};
use uuid::Uuid; use uuid::Uuid;
use crate::{ use crate::{
@ -9,8 +7,7 @@ use crate::{
schema::{sections, sections::dsl::*}, schema::{sections, sections::dsl::*},
}; };
/// Represents a section contained in the database. #[derive(Queryable, Serialize)]
#[derive(Queryable)]
pub struct Section pub struct Section
{ {
pub id: Uuid, pub id: Uuid,
@ -20,7 +17,6 @@ pub struct Section
pub has_titles: bool, pub has_titles: bool,
} }
/// A new section to be added into the database.
#[derive(Deserialize, Insertable)] #[derive(Deserialize, Insertable)]
#[table_name = "sections"] #[table_name = "sections"]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@ -32,32 +28,49 @@ pub struct NewSection
has_titles: Option<bool>, has_titles: Option<bool>,
} }
/// Returns all sections in the database. #[derive(Deserialize, AsChangeset)]
/// #[table_name = "sections"]
/// # Arguments #[serde(rename_all = "camelCase")]
/// pub struct PatchSection
/// * `conn` - reference to a database connection
pub fn all(conn: &PgConnection) -> RbResult<Vec<Section>>
{ {
sections title: Option<String>,
.load::<Section>(conn) description: Option<String>,
.map_err(|_| RbError::DbError("Couldn't get all sections")) is_default: Option<bool>,
has_titles: Option<bool>,
} }
/// Inserts a new section into the database. pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult<Vec<Section>>
///
/// # Arguments
///
/// * `conn` - reference to a database connection
/// * `new_section` - the new section to be added
pub fn create(conn: &PgConnection, new_section: &NewSection) -> RbResult<()>
{ {
insert_into(sections) Ok(sections
.values(new_section) .offset(offset_.into())
.execute(conn) .limit(limit_.into())
.map_err(|_| RbError::DbError("Couldn't insert section."))?; .load::<Section>(conn)
.map_err(|_| RbError::DbError("Couldn't query sections."))?)
}
pub fn create(conn: &PgConnection, new_post: &NewSection) -> RbResult<Section>
{
Ok(insert_into(sections)
.values(new_post)
.get_result::<Section>(conn)
.map_err(|_| RbError::DbError("Couldn't insert section."))?)
// TODO check for conflict? // TODO check for conflict?
}
pub fn update(conn: &PgConnection, post_id: &Uuid, patch_post: &PatchSection) -> RbResult<Section>
{
Ok(diesel::update(sections.filter(id.eq(post_id)))
.set(patch_post)
.get_result::<Section>(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(()) Ok(())
} }