Wrote some database boilerplate
parent
211e31a008
commit
8534090f0f
|
@ -1,5 +1,8 @@
|
||||||
pub mod tokens;
|
pub mod tokens;
|
||||||
pub mod users;
|
pub mod users;
|
||||||
|
pub mod sections;
|
||||||
|
pub mod posts;
|
||||||
|
|
||||||
pub use tokens::{NewRefreshToken, RefreshToken};
|
pub use tokens::{NewRefreshToken, RefreshToken};
|
||||||
pub use users::{NewUser, User};
|
pub use users::{NewUser, User};
|
||||||
|
pub use sections::{Section, NewSection};
|
||||||
|
|
|
@ -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<String>,
|
||||||
|
pub publish_date: NaiveDate,
|
||||||
|
pub content: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Insertable)]
|
||||||
|
#[table_name = "posts"]
|
||||||
|
pub struct NewPost
|
||||||
|
{
|
||||||
|
pub section_id: Uuid,
|
||||||
|
pub title: Option<String>,
|
||||||
|
pub publish_date: NaiveDate,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn all(conn: &PgConnection) -> RbResult<Vec<Post>>
|
||||||
|
{
|
||||||
|
posts.load::<Post>(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(())
|
||||||
|
}
|
|
@ -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<String>,
|
||||||
|
pub is_default: bool,
|
||||||
|
pub has_titles: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Insertable)]
|
||||||
|
#[table_name = "sections"]
|
||||||
|
pub struct NewSection
|
||||||
|
{
|
||||||
|
title: String,
|
||||||
|
description: Option<String>,
|
||||||
|
is_default: bool,
|
||||||
|
has_titles: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn all(conn: &PgConnection) -> RbResult<Vec<Section>>
|
||||||
|
{
|
||||||
|
sections.load::<Section>(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(())
|
||||||
|
}
|
|
@ -36,7 +36,7 @@ pub fn create(conn: &PgConnection, new_refresh_token: &NewRefreshToken) -> RbRes
|
||||||
insert_into(refresh_tokens)
|
insert_into(refresh_tokens)
|
||||||
.values(new_refresh_token)
|
.values(new_refresh_token)
|
||||||
.execute(conn)
|
.execute(conn)
|
||||||
.map_err(|_| RbError::Custom("Couldn't insert refresh token."))?;
|
.map_err(|_| RbError::DbError("Couldn't insert refresh token."))?;
|
||||||
|
|
||||||
// TODO check for conflict?
|
// TODO check for conflict?
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ pub fn find_with_user(
|
||||||
.inner_join(crate::schema::users::dsl::users)
|
.inner_join(crate::schema::users::dsl::users)
|
||||||
.filter(token.eq(token_val))
|
.filter(token.eq(token_val))
|
||||||
.first::<(RefreshToken, super::users::User)>(conn)
|
.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()
|
.ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue