feat(application): add DeleteReviewCommand and delete_review use case
This commit is contained in:
@@ -29,3 +29,8 @@ pub struct RegisterCommand {
|
|||||||
pub email: String,
|
pub email: String,
|
||||||
pub password: String,
|
pub password: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct DeleteReviewCommand {
|
||||||
|
pub review_id: Uuid,
|
||||||
|
pub requesting_user_id: Uuid,
|
||||||
|
}
|
||||||
|
|||||||
27
crates/application/src/use_cases/delete_review.rs
Normal file
27
crates/application/src/use_cases/delete_review.rs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
use domain::{errors::DomainError, value_objects::{ReviewId, UserId}};
|
||||||
|
use crate::{commands::DeleteReviewCommand, context::AppContext};
|
||||||
|
|
||||||
|
pub async fn execute(ctx: &AppContext, cmd: DeleteReviewCommand) -> Result<(), DomainError> {
|
||||||
|
let review_id = ReviewId::from_uuid(cmd.review_id);
|
||||||
|
let requesting_user_id = UserId::from_uuid(cmd.requesting_user_id);
|
||||||
|
|
||||||
|
let review = ctx
|
||||||
|
.repository
|
||||||
|
.get_review_by_id(&review_id)
|
||||||
|
.await?
|
||||||
|
.ok_or_else(|| DomainError::NotFound(format!("review {}", cmd.review_id)))?;
|
||||||
|
|
||||||
|
if review.user_id() != &requesting_user_id {
|
||||||
|
return Err(DomainError::Unauthorized("not your review".into()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let movie_id = review.movie_id().clone();
|
||||||
|
ctx.repository.delete_review(&review_id).await?;
|
||||||
|
|
||||||
|
let history = ctx.repository.get_review_history(&movie_id).await?;
|
||||||
|
if history.viewings().is_empty() {
|
||||||
|
ctx.repository.delete_movie(&movie_id).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
pub mod delete_review;
|
||||||
pub mod get_diary;
|
pub mod get_diary;
|
||||||
pub mod get_review_history;
|
pub mod get_review_history;
|
||||||
pub mod log_review;
|
pub mod log_review;
|
||||||
|
|||||||
Reference in New Issue
Block a user