Wrote part of posts db boilerplate
This commit is contained in:
parent
0da8eb127c
commit
f6e9039b59
2 changed files with 51 additions and 29 deletions
|
|
@ -1,5 +1,6 @@
|
|||
use chrono::NaiveDate;
|
||||
use diesel::{insert_into, prelude::*, Insertable, PgConnection, Queryable};
|
||||
use serde::Deserialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
|
|
@ -24,19 +25,27 @@ pub struct NewPost
|
|||
pub section_id: Uuid,
|
||||
pub title: Option<String>,
|
||||
pub publish_date: NaiveDate,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
/// Returns all posts in the database; should be used with care as this method could quickly return
|
||||
/// a large amount of data.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `conn` - a reference to a database connection
|
||||
pub fn all(conn: &PgConnection) -> RbResult<Vec<Post>>
|
||||
#[derive(Deserialize, AsChangeset)]
|
||||
#[table_name = "posts"]
|
||||
pub struct PatchPost
|
||||
{
|
||||
posts
|
||||
pub id: Option<Uuid>,
|
||||
pub section_id: Option<Uuid>,
|
||||
pub title: Option<String>,
|
||||
pub publish_date: Option<NaiveDate>,
|
||||
pub content: Option<String>,
|
||||
}
|
||||
|
||||
pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult<Vec<Post>>
|
||||
{
|
||||
Ok(posts
|
||||
.offset(offset_.into())
|
||||
.limit(limit_.into())
|
||||
.load::<Post>(conn)
|
||||
.map_err(|_| RbError::DbError("Couldn't get all posts."))
|
||||
.map_err(|_| RbError::DbError("Couldn't query posts."))?)
|
||||
}
|
||||
|
||||
/// Insert a new post into the database.
|
||||
|
|
@ -45,14 +54,29 @@ pub fn all(conn: &PgConnection) -> RbResult<Vec<Post>>
|
|||
///
|
||||
/// * `conn` - reference to a database connection
|
||||
/// * `new_post` - the new post object to insert
|
||||
pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult<()>
|
||||
pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult<Post>
|
||||
{
|
||||
insert_into(posts)
|
||||
Ok(insert_into(posts)
|
||||
.values(new_post)
|
||||
.execute(conn)
|
||||
.map_err(|_| RbError::DbError("Couldn't insert post."))?;
|
||||
.get_result::<Post>(conn)
|
||||
.map_err(|_| RbError::DbError("Couldn't insert post."))?)
|
||||
|
||||
// TODO check for conflict?
|
||||
}
|
||||
|
||||
pub fn update(conn: &PgConnection, post: &PatchPost) -> RbResult<Post>
|
||||
{
|
||||
Ok(diesel::update(posts)
|
||||
.set(post)
|
||||
.get_result::<Post>(conn)
|
||||
.map_err(|_| RbError::DbError("Couldn't update post."))?)
|
||||
}
|
||||
|
||||
pub fn delete(conn: &PgConnection, post_id: Uuid) -> RbResult<()>
|
||||
{
|
||||
diesel::delete(posts.filter(id.eq(post_id)))
|
||||
.execute(conn)
|
||||
.map_err(|_| RbError::DbError("Couldn't delete post."))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue