From d93dc7fd89535c49ceb7fef368612e9c4bbaaf16 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 22 Mar 2024 21:28:20 +0100 Subject: [PATCH] Engage endpoint --- client-solid/src/Util/api.ts | 15 ++++++++ ...e9ab89e1a31a78a5be0c6d38d91e3a197af41.json | 15 ++++++++ server/migrations/0005_engagements_table.sql | 7 ++++ server/src/routes/post.rs | 36 +++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 server/.sqlx/query-2374223a11247bf75811f4cc846e9ab89e1a31a78a5be0c6d38d91e3a197af41.json create mode 100644 server/migrations/0005_engagements_table.sql diff --git a/client-solid/src/Util/api.ts b/client-solid/src/Util/api.ts index 3284805..7dcd384 100644 --- a/client-solid/src/Util/api.ts +++ b/client-solid/src/Util/api.ts @@ -126,3 +126,18 @@ export async function submitLogin( if (response.ok) return await response.json(); } +/** + * Engage with a post. + * @param postId The id of the post to engage with. + * @param token The token of the user engaging with the post. + * @returns {Promise} A promise that resolves to a Response object. + */ +export async function engage(postId: string, token: string): Promise { + return await fetch(`/api/posts/${postId}/engage`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + }); +} diff --git a/server/.sqlx/query-2374223a11247bf75811f4cc846e9ab89e1a31a78a5be0c6d38d91e3a197af41.json b/server/.sqlx/query-2374223a11247bf75811f4cc846e9ab89e1a31a78a5be0c6d38d91e3a197af41.json new file mode 100644 index 0000000..9287b22 --- /dev/null +++ b/server/.sqlx/query-2374223a11247bf75811f4cc846e9ab89e1a31a78a5be0c6d38d91e3a197af41.json @@ -0,0 +1,15 @@ +{ + "db_name": "PostgreSQL", + "query": "INSERT INTO engagements (post_id, user_id) VALUES ($1, (SELECT id FROM users WHERE username = $2))", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8", + "Text" + ] + }, + "nullable": [] + }, + "hash": "2374223a11247bf75811f4cc846e9ab89e1a31a78a5be0c6d38d91e3a197af41" +} diff --git a/server/migrations/0005_engagements_table.sql b/server/migrations/0005_engagements_table.sql new file mode 100644 index 0000000..9b65696 --- /dev/null +++ b/server/migrations/0005_engagements_table.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS engagements ( + user_id BIGINT NOT NULL, + post_id BIGINT NOT NULL, + FOREIGN KEY (user_id) REFERENCES users (id), + FOREIGN KEY (post_id) REFERENCES posts (id), + PRIMARY KEY (user_id, post_id) +); \ No newline at end of file diff --git a/server/src/routes/post.rs b/server/src/routes/post.rs index b54f49c..522c21a 100755 --- a/server/src/routes/post.rs +++ b/server/src/routes/post.rs @@ -4,6 +4,7 @@ use crate::types::{NewPost, PostQueryParams}; use crate::ServerState; use actix_web::web::{Data, Path, Query}; +use actix_web::HttpRequest; use actix_web::{get, post, web::Json, HttpResponse, Responder, Result}; use log::info; @@ -59,6 +60,41 @@ pub async fn new_post( }; } +#[post("/posts/{id}/engage")] +pub async fn engage_post( + path: Path, + state: Data, + auth: Data, + req: HttpRequest, +) -> Result { + // Token from header + let token = req + .headers() + .get("Authorization") + .unwrap() + .to_str() + .unwrap(); + + let claims = auth.decode(token); + + if let Err(e) = claims { + info!("Error validating token: {}", e); + return Ok(HttpResponse::BadRequest().json("Error")); + } + + let post_id = path.into_inner(); + let username = claims.unwrap().sub; + + let q = sqlx::query!( + "INSERT INTO engagements (post_id, user_id) VALUES ($1, (SELECT id FROM users WHERE username = $2))", + post_id, + username + ); + q.execute(&state.pool).await.unwrap(); + + return Ok(HttpResponse::Ok().json("Engaged")); +} + #[get("posts/{id}")] pub async fn post_by_id(path: Path, state: Data) -> Result { let id = path.into_inner();