2023-11-28 01:26:19 +01:00
|
|
|
use crate::db::{db_get_comments, db_new_comment};
|
2023-12-22 22:00:49 +01:00
|
|
|
use crate::jwt::Authentication;
|
2023-11-28 02:19:49 +01:00
|
|
|
use crate::types::{CommentQueryParams, NewComment};
|
2023-11-28 01:26:19 +01:00
|
|
|
use crate::ServerState;
|
|
|
|
|
|
|
|
use actix_web::get;
|
2024-03-23 00:23:29 +01:00
|
|
|
use actix_web::web::{Data, Path, Query};
|
2023-11-28 01:26:19 +01:00
|
|
|
use actix_web::{post, web::Json, HttpResponse, Responder, Result};
|
|
|
|
use log::info;
|
|
|
|
|
|
|
|
#[get("/comments")]
|
|
|
|
pub async fn get_comments(
|
2023-11-28 02:19:49 +01:00
|
|
|
comment_filter: Query<CommentQueryParams>,
|
2023-11-28 01:26:19 +01:00
|
|
|
state: Data<ServerState>,
|
|
|
|
) -> Result<impl Responder> {
|
2023-11-28 02:19:49 +01:00
|
|
|
let post_id = comment_filter.post_id;
|
|
|
|
let limit = comment_filter.limit.unwrap_or(10);
|
|
|
|
let offset = comment_filter.offset.unwrap_or(0);
|
2023-11-28 01:26:19 +01:00
|
|
|
|
|
|
|
info!(
|
|
|
|
"Getting comments for post {} with limit {} and offset {}",
|
|
|
|
post_id, limit, offset
|
|
|
|
);
|
|
|
|
|
|
|
|
let comments = db_get_comments(&state.pool, post_id, limit, offset).await;
|
|
|
|
|
2024-03-06 08:05:23 +01:00
|
|
|
if comments.is_empty() {
|
|
|
|
info!("No comments found for post {}", post_id);
|
|
|
|
return Ok(HttpResponse::NotFound().json("No comments found"));
|
|
|
|
}
|
|
|
|
|
2023-11-28 01:26:19 +01:00
|
|
|
Ok(HttpResponse::Ok().json(comments))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/comments")]
|
|
|
|
pub async fn new_comment(
|
|
|
|
data: Json<NewComment>,
|
|
|
|
state: Data<ServerState>,
|
2023-12-22 22:00:49 +01:00
|
|
|
auth: Data<Authentication>,
|
2023-11-28 01:26:19 +01:00
|
|
|
) -> Result<impl Responder> {
|
2023-12-22 22:00:49 +01:00
|
|
|
let user_claims = auth.decode(&data.user_token);
|
2023-11-28 01:26:19 +01:00
|
|
|
|
|
|
|
// Bail if the token is invalid
|
|
|
|
if let Err(e) = user_claims {
|
|
|
|
info!("Error validating token: {}", e);
|
|
|
|
return Ok(HttpResponse::BadRequest().json("Error"));
|
|
|
|
}
|
|
|
|
|
|
|
|
let claims = user_claims.unwrap();
|
|
|
|
|
|
|
|
let content = data.content.clone();
|
|
|
|
let username = claims.sub.clone();
|
|
|
|
|
|
|
|
// This one is avoidable if we just store the user id in the token
|
|
|
|
let userid = sqlx::query!("SELECT id FROM users WHERE username = $1", username)
|
|
|
|
.fetch_one(&state.pool)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.id;
|
|
|
|
|
2024-03-05 21:03:37 +01:00
|
|
|
info!(
|
|
|
|
"User {:?}, with id {:?} is creating a new comment",
|
|
|
|
&claims.sub, userid
|
|
|
|
);
|
|
|
|
|
|
|
|
info!("Creating a new comment {:?}", &data);
|
|
|
|
|
2024-03-06 08:05:23 +01:00
|
|
|
let success = db_new_comment(&state.pool, data.parent_post_id, userid, &content).await;
|
2023-11-28 01:26:19 +01:00
|
|
|
|
|
|
|
match success {
|
2024-03-05 21:03:37 +01:00
|
|
|
true => {
|
|
|
|
info!("User {:?} created a new comment", &claims.sub);
|
|
|
|
Ok(HttpResponse::Ok().json("Successfully created comment"))
|
|
|
|
}
|
|
|
|
false => {
|
|
|
|
info!("User {:?} failed to create a new comment", &claims.sub);
|
|
|
|
Ok(HttpResponse::BadRequest().json("Failed to create comment"))
|
|
|
|
}
|
2023-11-28 01:26:19 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-23 00:23:29 +01:00
|
|
|
|
|
|
|
#[get("/posts/{id}/comments/count")]
|
|
|
|
pub async fn get_comment_count(
|
|
|
|
path: Path<i64>,
|
|
|
|
state: Data<ServerState>,
|
|
|
|
) -> Result<impl Responder> {
|
|
|
|
let post_id = path.into_inner();
|
|
|
|
|
|
|
|
let count = sqlx::query!(
|
|
|
|
"SELECT COUNT(*) FROM comments WHERE parent_post_id = $1",
|
|
|
|
post_id
|
|
|
|
)
|
|
|
|
.fetch_one(&state.pool)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.count;
|
|
|
|
|
|
|
|
return Ok(HttpResponse::Ok().json(count));
|
|
|
|
}
|