//! 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("/?&")] pub async fn get(conn: RbDbConn, offset: u32, limit: u32) -> RbResult>> { Ok(Json( conn.run(move |c| db::sections::get(c, offset, limit)) .await?, )) } /// Create a new section. #[post("/", data = "")] pub async fn create( _admin: Admin, conn: RbDbConn, new_section: Json, ) -> RbResult> { Ok(Json( conn.run(move |c| db::sections::create(c, &new_section.into_inner())) .await?, )) } /// Get a section by its shortname. #[get("/")] pub async fn find(conn: RbDbConn, shortname: String) -> RbOption> { Ok(conn .run(move |c| db::sections::find_with_shortname(c, &shortname)) .await?.map(|p| Json(p))) } /// Patch a section given its shortname. #[patch("/", data = "")] pub async fn patch( _admin: Admin, conn: RbDbConn, shortname: String, patch_section: Json, ) -> RbResult> { 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("/")] 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); } }