Some routes for managing posts

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

6
API.md
View File

@ -16,9 +16,9 @@ This file describes the API that the software adheres to. All routes are defined
* GET `/posts?<offset>&<limit>` - get list of posts from the default feed given offset & limit * GET `/posts?<offset>&<limit>` - get list of posts from the default feed given offset & limit
* GET `/posts?<section_id_or_shortname>&<offset>&<limit>` - get list of posts of a specific section * GET `/posts?<section_id_or_shortname>&<offset>&<limit>` - get list of posts of a specific section
* (A) POST `/posts` - create a new post * (A) POST `/posts` - create a new post
* GET `/posts/<id_or_shortname>` - get a specific post * GET `/posts/<id>` - get a specific post
* (A) DELETE `/posts/<id_or_shortname>` - delete a post * (A) DELETE `/posts/<id>` - delete a post
* (A) PATCH `/posts/<id_or_shortname>` - patch a post * (A) PATCH `/posts/<id>` - patch a post
## Sections ## Sections

2
Cargo.lock generated
View File

@ -1109,6 +1109,7 @@ dependencies = [
"tokio-stream", "tokio-stream",
"tokio-util", "tokio-util",
"ubyte", "ubyte",
"uuid",
"version_check", "version_check",
"yansi", "yansi",
] ]
@ -1155,6 +1156,7 @@ dependencies = [
"time 0.2.27", "time 0.2.27",
"tokio", "tokio",
"uncased", "uncased",
"uuid",
] ]
[[package]] [[package]]

View File

@ -17,7 +17,7 @@ static = ["web", "docs"]
[dependencies] [dependencies]
# Backend web framework # Backend web framework
rocket = { version = "0.5.0-rc.1", features = [ "json" ] } rocket = { version = "0.5.0-rc.1", features = [ "json", "uuid" ] }
# Used to provide Rocket routes with database connections # Used to provide Rocket routes with database connections
rocket_sync_db_pools = { version = "0.1.0-rc.1", default_features = false, features = [ "diesel_postgres_pool" ] } rocket_sync_db_pools = { version = "0.1.0-rc.1", default_features = false, features = [ "diesel_postgres_pool" ] }
# Used to (de)serialize JSON # Used to (de)serialize JSON

View File

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

View File

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

View File

@ -112,4 +112,5 @@ fn rocket() -> _
routes![admin::create_user, admin::get_user_info], routes![admin::create_user, admin::get_user_info],
) )
.mount("/api/sections", routes![sections::create_section]) .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 rocket::serde::json::Json;
use crate::{ use crate::{db, errors::RbResult, guards::Admin, RbDbConn};
db,
errors::RbResult,
guards::Admin,
RbDbConn,
};
#[get("/?<offset>&<limit>")] #[get("/?<offset>&<limit>")]
pub async fn get(conn: RbDbConn, offset: u32, limit: u32) -> RbResult<Json<Vec<db::Post>>> 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>")] #[post("/", data = "<new_post>")]
pub async fn create(_admin: Admin, conn: RbDbConn, new_post: Json<db::NewPost>) pub async fn create(
-> RbResult<Json<db::Post>> _admin: Admin,
conn: RbDbConn,
new_post: Json<db::NewPost>,
) -> RbResult<Json<db::Post>>
{ {
Ok(Json( Ok(Json(
conn.run(move |c| db::posts::create(c, &new_post.into_inner())) conn.run(move |c| db::posts::create(c, &new_post.into_inner()))
.await?, .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>, new_section: Json<db::NewSection>,
) -> RbResult<Json<db::Section>> ) -> RbResult<Json<db::Section>>
{ {
Ok(Json(conn Ok(Json(
.run(move |c| db::sections::create(c, &new_section.into_inner())) conn.run(move |c| db::sections::create(c, &new_section.into_inner()))
.await?)) .await?,
))
} }