Skip to content

Commit

Permalink
47 user delete rating
Browse files Browse the repository at this point in the history
  • Loading branch information
smart--petea committed Apr 13, 2024
1 parent 9b130dd commit 72ce432
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/db/rating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,13 @@ pub async fn update(pool: &PgPool, rating: models::Rating) -> Result<models::Rat
SET
comment=$1,
rate=$2,
hidden=$3,
updated_at=NOW() at time zone 'utc'
WHERE id = $3
WHERE id = $4
"#,
rating.comment,
rating.rate,
rating.hidden,
rating.id
)
.execute(pool)
Expand Down
18 changes: 15 additions & 3 deletions src/routes/rating/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,35 @@ use crate::helpers::JsonResponse;
use crate::models;
use actix_web::{delete, web, Responder, Result};
use sqlx::PgPool;
use std::sync::Arc;

#[tracing::instrument(name = "User delete rating.")]
#[delete("/{id}")]
pub async fn user_delete_handler(
user: web::ReqData<Arc<models::User>>,
path: web::Path<(i32,)>,
pg_pool: web::Data<PgPool>,
) -> Result<impl Responder> {
let rate_id = path.0;
let rating = db::rating::fetch(pg_pool.get_ref(), rate_id)
let mut rating = db::rating::fetch(pg_pool.get_ref(), rate_id)
.await
.map_err(|_err| JsonResponse::<models::Rating>::build().internal_server_error(""))
.and_then(|rating| {
match rating {
Some(rating) if rating.hidden == Some(false) => { Ok(rating) },
Some(rating) if rating.user_id == user.id && rating.hidden == Some(false) => Ok(rating),
_ => Err(JsonResponse::<models::Rating>::build().not_found("not found"))
}
})?;

Ok(JsonResponse::build().set_item(rating).ok("OK"))
rating.hidden.insert(true);

db::rating::update(pg_pool.get_ref(), rating)
.await
.map(|rating| {
JsonResponse::<models::Rating>::build().ok("success")
})
.map_err(|err| {
tracing::error!("Failed to execute query: {:?}", err);
JsonResponse::<models::Rating>::build().internal_server_error("Rating not update")
})
}
2 changes: 1 addition & 1 deletion src/routes/rating/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub async fn user_edit_handler(
.map_err(|_err| JsonResponse::<models::Rating>::build().internal_server_error(""))
.and_then(|rating| {
match rating {
Some(rating) if rating.user_id == user.id=> Ok(rating),
Some(rating) if rating.user_id == user.id && rating.hidden == Some(false) => Ok(rating),
_ => Err(JsonResponse::<models::Rating>::build().not_found("not found"))
}
})?;
Expand Down

0 comments on commit 72ce432

Please sign in to comment.