Some routes for managing posts

This commit is contained in:
Jef Roosens 2021-10-11 19:47:44 +02:00
parent 449c20fac2
commit d013bd60bd
Signed by untrusted user: Jef Roosens
GPG key ID: B580B976584B5F30
8 changed files with 44 additions and 18 deletions

View file

@ -44,7 +44,7 @@ pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult<Vec<Post>
Ok(posts
.offset(offset_.into())
.limit(limit_.into())
.load::<Post>(conn)
.load(conn)
.map_err(|_| RbError::DbError("Couldn't query posts."))?)
}

View file

@ -1,8 +1,8 @@
//! Handles refresh token-related database operations.
use diesel::{insert_into, prelude::*, Insertable, PgConnection, Queryable};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use serde::{Serialize, Deserialize};
use crate::{
errors::{RbError, RbResult},
@ -56,7 +56,11 @@ pub fn create(conn: &PgConnection, new_token: &NewRefreshToken) -> RbResult<Refr
// TODO check for conflict?
}
pub fn update(conn: &PgConnection, token_: &[u8], patch_token: &PatchRefreshToken) -> RbResult<RefreshToken>
pub fn update(
conn: &PgConnection,
token_: &[u8],
patch_token: &PatchRefreshToken,
) -> RbResult<RefreshToken>
{
Ok(diesel::update(refresh_tokens.filter(token.eq(token_)))
.set(patch_token)

View file

@ -112,4 +112,5 @@ fn rocket() -> _
routes![admin::create_user, admin::get_user_info],
)
.mount("/api/sections", routes![sections::create_section])
.mount("/api/posts", routes![posts::get, posts::create])
}

View file

@ -1,11 +1,6 @@
use rocket::serde::json::Json;
use crate::{
db,
errors::RbResult,
guards::Admin,
RbDbConn,
};
use crate::{db, errors::RbResult, guards::Admin, RbDbConn};
#[get("/?<offset>&<limit>")]
pub async fn get(conn: RbDbConn, offset: u32, limit: u32) -> RbResult<Json<Vec<db::Post>>>
@ -16,11 +11,34 @@ pub async fn get(conn: RbDbConn, offset: u32, limit: u32) -> RbResult<Json<Vec<d
}
#[post("/", data = "<new_post>")]
pub async fn create(_admin: Admin, conn: RbDbConn, new_post: Json<db::NewPost>)
-> RbResult<Json<db::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?,
))
}
#[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("/<id>")]
pub async fn delete(_admin: Admin, conn: RbDbConn, id: uuid::Uuid) -> RbResult<()>
{
Ok(conn.run(move |c| db::posts::delete(c, &id)).await?)
}

View file

@ -18,7 +18,8 @@ pub async fn create_section(
new_section: Json<db::NewSection>,
) -> RbResult<Json<db::Section>>
{
Ok(Json(conn
.run(move |c| db::sections::create(c, &new_section.into_inner()))
.await?))
Ok(Json(
conn.run(move |c| db::sections::create(c, &new_section.into_inner()))
.await?,
))
}