From efcdaf0cd2f3c971cdfd052c8d10195fab3a0775 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 22 Mar 2024 22:03:26 +0100 Subject: [PATCH] Get engagements path --- ...984a7fd212436b51011e20f0ba54b209251fc.json | 22 +++++++++++++++ server/src/main.rs | 3 ++- server/src/routes/post.rs | 27 +++++++++++++++++-- 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 server/.sqlx/query-7a112e169de3c28912597e6e9d9984a7fd212436b51011e20f0ba54b209251fc.json diff --git a/server/.sqlx/query-7a112e169de3c28912597e6e9d9984a7fd212436b51011e20f0ba54b209251fc.json b/server/.sqlx/query-7a112e169de3c28912597e6e9d9984a7fd212436b51011e20f0ba54b209251fc.json new file mode 100644 index 0000000..4ad9d1a --- /dev/null +++ b/server/.sqlx/query-7a112e169de3c28912597e6e9d9984a7fd212436b51011e20f0ba54b209251fc.json @@ -0,0 +1,22 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT COUNT(*) FROM engagements WHERE post_id = $1", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "count", + "type_info": "Int8" + } + ], + "parameters": { + "Left": [ + "Int8" + ] + }, + "nullable": [ + null + ] + }, + "hash": "7a112e169de3c28912597e6e9d9984a7fd212436b51011e20f0ba54b209251fc" +} diff --git a/server/src/main.rs b/server/src/main.rs index 1a5aaec..c2ca493 100755 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -19,7 +19,7 @@ use state::CaptchaState; use state::ServerState; use util::hex_string; -use crate::routes::engage_post; +use crate::routes::{engage_post, get_engagements}; #[actix_web::main] async fn main() -> std::io::Result<()> { @@ -69,6 +69,7 @@ async fn main() -> std::io::Result<()> { .service(new_comment) .service(get_comments) .service(engage_post) + .service(get_engagements) .service(post_by_id) .service(login) .service(register) diff --git a/server/src/routes/post.rs b/server/src/routes/post.rs index f6a5901..fa97264 100755 --- a/server/src/routes/post.rs +++ b/server/src/routes/post.rs @@ -91,12 +91,35 @@ pub async fn engage_post( "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(); + ).execute(&state.pool).await; + + match q { + Ok(_) => (), + Err(e) => { + info!("Error engaging post: {}", e); + return Ok(HttpResponse::InternalServerError().json("Error")); + } + } return Ok(HttpResponse::Ok().json("Engaged")); } +#[get("/posts/{id}/engage")] +pub async fn get_engagements(path: Path, state: Data) -> Result { + let id = path.into_inner(); + let q = sqlx::query!("SELECT COUNT(*) FROM engagements WHERE post_id = $1", id) + .fetch_one(&state.pool) + .await; + + match q { + Ok(count) => Ok(HttpResponse::Ok().json(count.count)), + Err(e) => { + info!("Error getting engagements: {}", e); + Ok(HttpResponse::InternalServerError().json("Error")) + } + } +} + #[get("posts/{id}")] pub async fn post_by_id(path: Path, state: Data) -> Result { let id = path.into_inner();