100 lines
2.4 KiB
Rust
100 lines
2.4 KiB
Rust
//! This module handles management of site sections (aka blogs).
|
|
|
|
use rb::{
|
|
errors::{RbOption, RbResult},
|
|
guards::Admin,
|
|
};
|
|
use rb_blog::db;
|
|
use rocket::serde::json::Json;
|
|
|
|
use crate::RbDbConn;
|
|
|
|
/// Get multiple sections given an offset & a limit. The limit is bound by
|
|
/// `rb_blog::db::MAX_SECTIONS`.
|
|
#[get("/?<offset>&<limit>")]
|
|
pub async fn get(conn: RbDbConn, offset: u32, limit: u32) -> RbResult<Json<Vec<db::Section>>>
|
|
{
|
|
Ok(Json(
|
|
conn.run(move |c| db::sections::get(c, offset, limit))
|
|
.await?,
|
|
))
|
|
}
|
|
|
|
/// Create a new section.
|
|
#[post("/", data = "<new_section>")]
|
|
pub async fn create(
|
|
_admin: Admin,
|
|
conn: RbDbConn,
|
|
new_section: Json<db::NewSection>,
|
|
) -> RbResult<Json<db::Section>>
|
|
{
|
|
Ok(Json(
|
|
conn.run(move |c| db::sections::create(c, &new_section.into_inner()))
|
|
.await?,
|
|
))
|
|
}
|
|
|
|
/// Get a section by its shortname.
|
|
#[get("/<shortname>")]
|
|
pub async fn find(conn: RbDbConn, shortname: String) -> RbOption<Json<db::Section>>
|
|
{
|
|
Ok(conn
|
|
.run(move |c| db::sections::find_with_shortname(c, &shortname))
|
|
.await?.map(|p| Json(p)))
|
|
}
|
|
|
|
/// Patch a section given its shortname.
|
|
#[patch("/<shortname>", data = "<patch_section>")]
|
|
pub async fn patch(
|
|
_admin: Admin,
|
|
conn: RbDbConn,
|
|
shortname: String,
|
|
patch_section: Json<db::PatchSection>,
|
|
) -> RbResult<Json<db::Section>>
|
|
{
|
|
Ok(Json(
|
|
conn.run(move |c| {
|
|
db::sections::update_with_shortname(c, &shortname, &patch_section.into_inner())
|
|
})
|
|
.await?,
|
|
))
|
|
}
|
|
|
|
/// Delete a section given its ID.
|
|
#[delete("/<id>")]
|
|
pub async fn delete(_admin: Admin, conn: RbDbConn, id: uuid::Uuid) -> RbResult<()>
|
|
{
|
|
Ok(conn.run(move |c| db::posts::delete(c, &id)).await?)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests
|
|
{
|
|
use rocket::{http::Status, local::blocking::Client};
|
|
|
|
use super::*;
|
|
use crate::rocket;
|
|
|
|
#[test]
|
|
fn test_create_get()
|
|
{
|
|
let client = Client::tracked(rocket()).expect("valid rocket instance");
|
|
|
|
let data = db::NewSection {
|
|
title: String::from("Some Cool Title"),
|
|
shortname: String::from("test"),
|
|
description: None,
|
|
is_default: None,
|
|
has_titles: None,
|
|
};
|
|
|
|
let res = client
|
|
.post("/v1/sections")
|
|
.json(&data)
|
|
.header(crate::auth_header())
|
|
.dispatch();
|
|
|
|
assert_eq!(res.status(), Status::Ok);
|
|
}
|
|
}
|