use chrono::NaiveDate; use diesel::{insert_into, prelude::*, Insertable, PgConnection, Queryable}; use serde::{Deserialize, Serialize}; use uuid::Uuid; use crate::{ errors::{RbError, RbOption, RbResult}, schema::{posts, posts::dsl::*}, }; #[derive(Queryable, Serialize)] pub struct Post { pub id: Uuid, pub section_id: Uuid, pub title: Option, pub publish_date: NaiveDate, pub content: String, } #[derive(Deserialize, Insertable)] #[table_name = "posts"] #[serde(rename_all = "camelCase")] pub struct NewPost { pub section_id: Uuid, pub title: Option, pub publish_date: NaiveDate, pub content: String, } #[derive(Deserialize, AsChangeset)] #[table_name = "posts"] pub struct PatchPost { pub section_id: Option, pub title: Option, pub publish_date: Option, pub content: Option, } pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult> { Ok(posts .offset(offset_.into()) .limit(limit_.into()) .load(conn) .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) .values(new_post) .get_result(conn) .map_err(|_| RbError::DbError("Couldn't insert post."))?) // TODO check for conflict? } pub fn update(conn: &PgConnection, post_id: &Uuid, patch_post: &PatchPost) -> RbResult { 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<()> { diesel::delete(posts.filter(id.eq(post_id))) .execute(conn) .map_err(|_| RbError::DbError("Couldn't delete post."))?; Ok(()) }