This repository has been archived on 2023-07-04. You can view files and clone it, but cannot push or open issues/pull-requests.
blog/src/db/sections.rs

110 lines
3.2 KiB
Rust
Raw Normal View History

2021-11-23 09:32:08 +01:00
use diesel::{insert_into, prelude::*, Insertable, PgConnection, Queryable};
2021-11-23 20:21:29 +01:00
use rb::errors::{RbError, RbOption, RbResult};
2021-11-23 09:32:08 +01:00
use serde::{Deserialize, Serialize};
use uuid::Uuid;
2021-11-23 17:49:31 +01:00
use crate::schema::{sections, sections::dsl::*};
2021-11-23 09:32:08 +01:00
2021-11-28 21:45:29 +01:00
/// A section inside the database.
2021-11-23 09:32:08 +01:00
#[derive(Queryable, Serialize)]
pub struct Section
{
pub id: Uuid,
pub title: String,
pub shortname: String,
pub description: Option<String>,
pub is_default: bool,
pub has_titles: bool,
}
2021-11-28 21:45:29 +01:00
/// A new section to add. Any `Option` values will get replaced by their default value in the
/// database.
2021-11-24 18:52:02 +01:00
#[derive(Serialize, Deserialize, Insertable)]
2021-11-23 09:32:08 +01:00
#[table_name = "sections"]
#[serde(rename_all = "camelCase")]
pub struct NewSection
{
2021-11-24 18:52:02 +01:00
pub title: String,
2021-11-23 09:32:08 +01:00
pub shortname: String,
2021-11-24 18:52:02 +01:00
pub description: Option<String>,
pub is_default: Option<bool>,
pub has_titles: Option<bool>,
2021-11-23 09:32:08 +01:00
}
2021-11-28 21:45:29 +01:00
/// A patch to apply to a section.
2021-11-23 09:32:08 +01:00
#[derive(Deserialize, AsChangeset)]
#[table_name = "sections"]
#[serde(rename_all = "camelCase")]
pub struct PatchSection
{
title: Option<String>,
shortname: Option<String>,
description: Option<String>,
is_default: Option<bool>,
has_titles: Option<bool>,
}
2021-11-28 21:45:29 +01:00
/// Get an amount of sections from the database, given an offset & limit. The maximum value for
/// `limit_` is determined by `super::MAX_SECTIONS`.
2021-11-23 09:32:08 +01:00
pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult<Vec<Section>>
{
Ok(sections
.offset(offset_.into())
2021-11-28 21:45:29 +01:00
.limit(std::cmp::min(limit_, super::MAX_SECTIONS).into())
2021-11-23 09:32:08 +01:00
.load(conn)
.map_err(|_| RbError::DbError("Couldn't query sections."))?)
}
2021-11-28 21:45:29 +01:00
/// Try to find a section given its shortname.
2021-11-23 20:21:29 +01:00
pub fn find_with_shortname(conn: &PgConnection, shortname_: &str) -> RbOption<Section>
{
match sections.filter(shortname.eq(shortname_)).first(conn) {
Ok(val) => Ok(Some(val)),
Err(diesel::NotFound) => Ok(None),
_ => Err(RbError::DbError("Couldn't find section.")),
}
}
2021-11-28 21:45:29 +01:00
/// Create a new section.
2021-11-23 20:21:29 +01:00
pub fn create(conn: &PgConnection, new_section: &NewSection) -> RbResult<Section>
2021-11-23 09:32:08 +01:00
{
Ok(insert_into(sections)
2021-11-23 20:21:29 +01:00
.values(new_section)
2021-11-23 09:32:08 +01:00
.get_result(conn)
.map_err(|_| RbError::DbError("Couldn't insert section."))?)
// TODO check for conflict?
}
2021-11-28 21:45:29 +01:00
/// Update a section given its ID.
2021-11-23 09:32:08 +01:00
pub fn update(conn: &PgConnection, post_id: &Uuid, patch_post: &PatchSection) -> RbResult<Section>
{
Ok(diesel::update(sections.filter(id.eq(post_id)))
.set(patch_post)
.get_result(conn)
.map_err(|_| RbError::DbError("Couldn't update section."))?)
}
2021-11-28 21:45:29 +01:00
// Update a section given its shortname.
2021-11-23 20:21:29 +01:00
pub fn update_with_shortname(
conn: &PgConnection,
shortname_: &str,
patch_section: &PatchSection,
) -> RbResult<Section>
{
Ok(diesel::update(sections.filter(shortname.eq(shortname_)))
.set(patch_section)
.get_result(conn)
.map_err(|_| RbError::DbError("Couldn't update section with shortname."))?)
}
2021-11-28 21:45:29 +01:00
/// Delete a section given its ID.
2021-11-23 09:32:08 +01:00
pub fn delete(conn: &PgConnection, post_id: &Uuid) -> RbResult<()>
{
diesel::delete(sections.filter(id.eq(post_id)))
.execute(conn)
.map_err(|_| RbError::DbError("Couldn't delete section."))?;
Ok(())
}