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/v1/posts.rs

83 lines
1.9 KiB
Rust

use rb::{
errors::{RbOption, RbResult},
guards::Admin,
};
use rb_blog::db;
use rocket::serde::json::Json;
use crate::RbDbConn;
/// Get one or more posts.
#[get("/?<offset>&<limit>")]
pub async fn get(conn: RbDbConn, offset: u32, limit: u32) -> RbResult<Json<Vec<db::Post>>>
{
Ok(Json(
conn.run(move |c| db::posts::get(c, offset, limit)).await?,
))
}
/// Create a new post.
#[post("/", data = "<new_post>")]
pub async fn create(
_admin: Admin,
conn: RbDbConn,
new_post: Json<db::NewPost>,
) -> RbResult<Json<db::Post>>
{
Ok(Json(
conn.run(move |c| db::posts::create(c, &new_post.into_inner()))
.await?,
))
}
/// Get a post given its ID.
#[get("/<id>")]
pub async fn find(conn: RbDbConn, id: uuid::Uuid) -> RbOption<Json<db::Post>>
{
Ok(conn
.run(move |c| db::posts::find(c, &id))
.await?
.and_then(|p| Some(Json(p))))
}
/// Patch a post given its ID.
#[patch("/<id>", data = "<patch_post>")]
pub async fn patch(
_admin: Admin,
conn: RbDbConn,
id: uuid::Uuid,
patch_post: Json<db::PatchPost>,
) -> RbResult<Json<db::Post>>
{
Ok(Json(
conn.run(move |c| db::posts::update(c, &id, &patch_post.into_inner()))
.await?,
))
}
/// Delete a post 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 crate::rocket;
// use rocket::local::blocking::Client;
// use rocket::http::Status;
// #[test]
// fn test_create_get() {
// let client = Client::tracked(rocket()).expect("valid rocket instance");
// let data = db::NewPost {
// id: Uuid::parse_str("936DA01F9ABD4d9d80C702AF85C822A8").unwrap(),
// };
// let mut res = cli
// }
// }