Get engagements path

This commit is contained in:
Imbus 2024-03-22 22:03:26 +01:00
parent e946dc456d
commit efcdaf0cd2
3 changed files with 49 additions and 3 deletions

View file

@ -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<i64>, state: Data<ServerState>) -> Result<impl Responder> {
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<i64>, state: Data<ServerState>) -> Result<impl Responder> {
let id = path.into_inner();