Engage endpoint
This commit is contained in:
parent
6fcbb92691
commit
d93dc7fd89
4 changed files with 73 additions and 0 deletions
|
|
@ -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<i64>,
|
||||
state: Data<ServerState>,
|
||||
auth: Data<Authentication>,
|
||||
req: HttpRequest,
|
||||
) -> Result<impl Responder> {
|
||||
// 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<i64>, state: Data<ServerState>) -> Result<impl Responder> {
|
||||
let id = path.into_inner();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue