This repository has been archived on 2021-10-25. You can view files and clone it, but cannot push or open issues/pull-requests.
rusty-bever/src/db/posts.rs

86 lines
2.1 KiB
Rust
Raw Normal View History

2021-09-13 22:15:38 +02:00
use chrono::NaiveDate;
2021-09-13 17:35:06 +02:00
use diesel::{insert_into, prelude::*, Insertable, PgConnection, Queryable};
2021-09-28 10:16:20 +02:00
use serde::{Deserialize, Serialize};
2021-09-13 17:35:06 +02:00
use uuid::Uuid;
use crate::{
2021-10-12 17:10:51 +02:00
errors::{RbError, RbOption, RbResult},
2021-09-13 17:35:06 +02:00
schema::{posts, posts::dsl::*},
};
2021-09-28 10:16:20 +02:00
#[derive(Queryable, Serialize)]
2021-09-13 17:35:06 +02:00
pub struct Post
{
pub id: Uuid,
pub section_id: Uuid,
pub title: Option<String>,
pub publish_date: NaiveDate,
pub content: String,
}
2021-09-26 19:02:17 +02:00
#[derive(Deserialize, Insertable)]
2021-09-13 17:35:06 +02:00
#[table_name = "posts"]
2021-09-26 19:02:17 +02:00
#[serde(rename_all = "camelCase")]
2021-09-13 17:35:06 +02:00
pub struct NewPost
{
pub section_id: Uuid,
pub title: Option<String>,
pub publish_date: NaiveDate,
2021-09-26 18:36:15 +02:00
pub content: String,
2021-09-13 17:35:06 +02:00
}
2021-09-26 18:36:15 +02:00
#[derive(Deserialize, AsChangeset)]
#[table_name = "posts"]
pub struct PatchPost
{
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>>
2021-09-13 17:35:06 +02:00
{
2021-09-26 18:36:15 +02:00
Ok(posts
.offset(offset_.into())
.limit(limit_.into())
2021-10-11 19:47:44 +02:00
.load(conn)
2021-09-26 18:36:15 +02:00
.map_err(|_| RbError::DbError("Couldn't query posts."))?)
2021-09-13 17:35:06 +02:00
}
2021-10-12 16:41:14 +02:00
pub fn find(conn: &PgConnection, id_: &Uuid) -> RbOption<Post>
{
match posts.find(id_).first(conn) {
Ok(val) => Ok(Some(val)),
Err(diesel::NotFound) => Ok(None),
_ => Err(RbError::DbError("Couldn't find post.")),
}
}
2021-09-26 18:36:15 +02:00
pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult<Post>
2021-09-13 17:35:06 +02:00
{
2021-09-26 18:36:15 +02:00
Ok(insert_into(posts)
2021-09-13 17:35:06 +02:00
.values(new_post)
2021-10-10 15:46:19 +02:00
.get_result(conn)
2021-09-26 18:36:15 +02:00
.map_err(|_| RbError::DbError("Couldn't insert post."))?)
2021-09-13 17:35:06 +02:00
// TODO check for conflict?
2021-09-26 18:36:15 +02:00
}
2021-09-26 19:02:17 +02:00
pub fn update(conn: &PgConnection, post_id: &Uuid, patch_post: &PatchPost) -> RbResult<Post>
2021-09-26 18:36:15 +02:00
{
2021-09-26 19:02:17 +02:00
Ok(diesel::update(posts.filter(id.eq(post_id)))
.set(patch_post)
2021-10-10 15:46:19 +02:00
.get_result(conn)
2021-09-26 18:36:15 +02:00
.map_err(|_| RbError::DbError("Couldn't update post."))?)
}
2021-09-26 19:02:17 +02:00
pub fn delete(conn: &PgConnection, post_id: &Uuid) -> RbResult<()>
2021-09-26 18:36:15 +02:00
{
diesel::delete(posts.filter(id.eq(post_id)))
.execute(conn)
.map_err(|_| RbError::DbError("Couldn't delete post."))?;
2021-09-13 17:35:06 +02:00
Ok(())
}