diff --git a/API.md b/API.md index 8eb4081..2fdce6f 100644 --- a/API.md +++ b/API.md @@ -16,9 +16,9 @@ This file describes the API that the software adheres to. All routes are defined * GET `/posts?&` - get list of posts from the default feed given offset & limit * GET `/posts?&&` - get list of posts of a specific section * (A) POST `/posts` - create a new post -* GET `/posts/` - get a specific post -* (A) DELETE `/posts/` - delete a post -* (A) PATCH `/posts/` - patch a post +* GET `/posts/` - get a specific post +* (A) DELETE `/posts/` - delete a post +* (A) PATCH `/posts/` - patch a post ## Sections diff --git a/Cargo.lock b/Cargo.lock index b10ec77..b42398b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1109,6 +1109,7 @@ dependencies = [ "tokio-stream", "tokio-util", "ubyte", + "uuid", "version_check", "yansi", ] @@ -1155,6 +1156,7 @@ dependencies = [ "time 0.2.27", "tokio", "uncased", + "uuid", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index c66a3c5..53cc5fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ static = ["web", "docs"] [dependencies] # 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 rocket_sync_db_pools = { version = "0.1.0-rc.1", default_features = false, features = [ "diesel_postgres_pool" ] } # Used to (de)serialize JSON diff --git a/src/db/posts.rs b/src/db/posts.rs index 109da6d..019e9cc 100644 --- a/src/db/posts.rs +++ b/src/db/posts.rs @@ -44,7 +44,7 @@ pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult Ok(posts .offset(offset_.into()) .limit(limit_.into()) - .load::(conn) + .load(conn) .map_err(|_| RbError::DbError("Couldn't query posts."))?) } diff --git a/src/db/tokens.rs b/src/db/tokens.rs index f2226c3..620685c 100644 --- a/src/db/tokens.rs +++ b/src/db/tokens.rs @@ -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 RbResult +pub fn update( + conn: &PgConnection, + token_: &[u8], + patch_token: &PatchRefreshToken, +) -> RbResult { Ok(diesel::update(refresh_tokens.filter(token.eq(token_))) .set(patch_token) diff --git a/src/main.rs b/src/main.rs index bd32e6c..03d84d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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]) } diff --git a/src/posts.rs b/src/posts.rs index 35df7bb..c62dae7 100644 --- a/src/posts.rs +++ b/src/posts.rs @@ -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("/?&")] pub async fn get(conn: RbDbConn, offset: u32, limit: u32) -> RbResult>> @@ -16,11 +11,34 @@ pub async fn get(conn: RbDbConn, offset: u32, limit: u32) -> RbResult) - -> RbResult> +pub async fn create( + _admin: Admin, + conn: RbDbConn, + new_post: Json, +) -> RbResult> { Ok(Json( conn.run(move |c| db::posts::create(c, &new_post.into_inner())) .await?, )) } + +#[patch("/", data = "")] +pub async fn patch( + _admin: Admin, + conn: RbDbConn, + id: uuid::Uuid, + patch_post: Json, +) -> RbResult> +{ + Ok(Json( + conn.run(move |c| db::posts::update(c, &id, &patch_post.into_inner())) + .await?, + )) +} + +#[delete("/")] +pub async fn delete(_admin: Admin, conn: RbDbConn, id: uuid::Uuid) -> RbResult<()> +{ + Ok(conn.run(move |c| db::posts::delete(c, &id)).await?) +} diff --git a/src/sections.rs b/src/sections.rs index adc9a2d..5be2042 100644 --- a/src/sections.rs +++ b/src/sections.rs @@ -18,7 +18,8 @@ pub async fn create_section( new_section: Json, ) -> RbResult> { - 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?, + )) }