Get engagements path
This commit is contained in:
parent
e946dc456d
commit
efcdaf0cd2
3 changed files with 49 additions and 3 deletions
|
@ -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"
|
||||
}
|
|
@ -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)
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue