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

@ -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"
}

View file

@ -19,7 +19,7 @@ use state::CaptchaState;
use state::ServerState; use state::ServerState;
use util::hex_string; use util::hex_string;
use crate::routes::engage_post; use crate::routes::{engage_post, get_engagements};
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
@ -69,6 +69,7 @@ async fn main() -> std::io::Result<()> {
.service(new_comment) .service(new_comment)
.service(get_comments) .service(get_comments)
.service(engage_post) .service(engage_post)
.service(get_engagements)
.service(post_by_id) .service(post_by_id)
.service(login) .service(login)
.service(register) .service(register)

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))", "INSERT INTO engagements (post_id, user_id) VALUES ($1, (SELECT id FROM users WHERE username = $2))",
post_id, post_id,
username username
); ).execute(&state.pool).await;
q.execute(&state.pool).await.unwrap();
match q {
Ok(_) => (),
Err(e) => {
info!("Error engaging post: {}", e);
return Ok(HttpResponse::InternalServerError().json("Error"));
}
}
return Ok(HttpResponse::Ok().json("Engaged")); 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}")] #[get("posts/{id}")]
pub async fn post_by_id(path: Path<i64>, state: Data<ServerState>) -> Result<impl Responder> { pub async fn post_by_id(path: Path<i64>, state: Data<ServerState>) -> Result<impl Responder> {
let id = path.into_inner(); let id = path.into_inner();