diff --git a/src/db/mod.rs b/src/db/mod.rs index 9c831dd..19cb419 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,5 +1,8 @@ pub mod tokens; pub mod users; +pub mod sections; +pub mod posts; pub use tokens::{NewRefreshToken, RefreshToken}; pub use users::{NewUser, User}; +pub use sections::{Section, NewSection}; diff --git a/src/db/posts.rs b/src/db/posts.rs new file mode 100644 index 0000000..0493405 --- /dev/null +++ b/src/db/posts.rs @@ -0,0 +1,44 @@ +use diesel::{insert_into, prelude::*, Insertable, PgConnection, Queryable}; +use uuid::Uuid; +use chrono::NaiveDate; + +use crate::{ + errors::{RbError, RbResult}, + schema::{posts, posts::dsl::*}, +}; + +#[derive(Queryable)] +pub struct Post +{ + pub id: Uuid, + pub section_id: Uuid, + pub title: Option, + pub publish_date: NaiveDate, + pub content: String, +} + +#[derive(Insertable)] +#[table_name = "posts"] +pub struct NewPost +{ + pub section_id: Uuid, + pub title: Option, + pub publish_date: NaiveDate, +} + +pub fn all(conn: &PgConnection) -> RbResult> +{ + posts.load::(conn).map_err(|_| RbError::DbError("Couldn't get all posts.")) +} + +pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult<()> +{ + insert_into(posts) + .values(new_post) + .execute(conn) + .map_err(|_| RbError::DbError("Couldn't insert post."))?; + + // TODO check for conflict? + + Ok(()) +} diff --git a/src/db/sections.rs b/src/db/sections.rs new file mode 100644 index 0000000..cff2736 --- /dev/null +++ b/src/db/sections.rs @@ -0,0 +1,44 @@ +use diesel::{insert_into, prelude::*, Insertable, PgConnection, Queryable}; +use uuid::Uuid; + +use crate::{ + errors::{RbError, RbResult}, + schema::{sections, sections::dsl::*}, +}; + +#[derive(Queryable)] +pub struct Section +{ + pub id: Uuid, + pub title: String, + pub description: Option, + pub is_default: bool, + pub has_titles: bool, +} + +#[derive(Insertable)] +#[table_name = "sections"] +pub struct NewSection +{ + title: String, + description: Option, + is_default: bool, + has_titles: bool, +} + +pub fn all(conn: &PgConnection) -> RbResult> +{ + sections.load::
(conn).map_err(|_| RbError::DbError("Couldn't get all sections")) +} + +pub fn create(conn: &PgConnection, new_section: &NewSection) -> RbResult<()> +{ + insert_into(sections) + .values(new_section) + .execute(conn) + .map_err(|_| RbError::DbError("Couldn't insert section."))?; + + // TODO check for conflict? + + Ok(()) +} diff --git a/src/db/tokens.rs b/src/db/tokens.rs index 8940721..97dd02d 100644 --- a/src/db/tokens.rs +++ b/src/db/tokens.rs @@ -36,7 +36,7 @@ pub fn create(conn: &PgConnection, new_refresh_token: &NewRefreshToken) -> RbRes insert_into(refresh_tokens) .values(new_refresh_token) .execute(conn) - .map_err(|_| RbError::Custom("Couldn't insert refresh token."))?; + .map_err(|_| RbError::DbError("Couldn't insert refresh token."))?; // TODO check for conflict? @@ -53,7 +53,7 @@ pub fn find_with_user( .inner_join(crate::schema::users::dsl::users) .filter(token.eq(token_val)) .first::<(RefreshToken, super::users::User)>(conn) - .map_err(|_| RbError::Custom("Couldn't get refresh token & user.")) + .map_err(|_| RbError::DbError("Couldn't get refresh token & user.")) .ok() }