forked from Chewing_Bever/rusty-bever
Some more db boilerplate
parent
f6e9039b59
commit
769d7a32de
|
@ -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<Uuid>,
|
||||
pub section_id: Option<Uuid>,
|
||||
pub title: Option<String>,
|
||||
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."))?)
|
||||
}
|
||||
|
||||
/// 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>
|
||||
{
|
||||
Ok(insert_into(posts)
|
||||
|
@ -64,15 +58,15 @@ pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult<Post>
|
|||
// 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)
|
||||
.set(post)
|
||||
Ok(diesel::update(posts.filter(id.eq(post_id)))
|
||||
.set(patch_post)
|
||||
.get_result::<Post>(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)
|
||||
|
|
|
@ -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<bool>,
|
||||
}
|
||||
|
||||
/// Returns all sections in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `conn` - reference to a database connection
|
||||
pub fn all(conn: &PgConnection) -> RbResult<Vec<Section>>
|
||||
#[derive(Deserialize, AsChangeset)]
|
||||
#[table_name = "sections"]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PatchSection
|
||||
{
|
||||
sections
|
||||
.load::<Section>(conn)
|
||||
.map_err(|_| RbError::DbError("Couldn't get all sections"))
|
||||
title: Option<String>,
|
||||
description: Option<String>,
|
||||
is_default: Option<bool>,
|
||||
has_titles: Option<bool>,
|
||||
}
|
||||
|
||||
/// 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<Vec<Section>>
|
||||
{
|
||||
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::<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?
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue