Api/Netina.Core/EntityServices/CommentHandlers/ConfirmCommentCommandHandle...

37 lines
1.5 KiB
C#

using Netina.Domain.Entities.Comments;
namespace Netina.Core.EntityServices.CommentHandlers;
public class ConfirmCommentCommandHandler(IRepositoryWrapper repositoryWrapper)
: IRequestHandler<ConfirmCommentCommand, Guid>
{
public async Task<Guid> Handle(ConfirmCommentCommand request, CancellationToken cancellationToken)
{
var review = await repositoryWrapper.SetRepository<Comment>().TableNoTracking
.FirstOrDefaultAsync(r => r.Id == request.Id, cancellationToken);
if (review == null)
throw new AppException("Comment not found", ApiResultStatusCode.NotFound);
review.ConfirmReview();
var productComment = await repositoryWrapper.SetRepository<ProductComment>()
.TableNoTracking.FirstOrDefaultAsync(f => f.Id == request.Id, cancellationToken);
if (productComment != null)
{
var product = await repositoryWrapper.SetRepository<Product>()
.TableNoTracking
.FirstOrDefaultAsync(p => p.Id == productComment.ProductId, cancellationToken);
if (product == null)
throw new AppException("Product not found", ApiResultStatusCode.NotFound);
product.AddRate(productComment.Rate);
repositoryWrapper.SetRepository<Product>().Update(product);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
}
repositoryWrapper.SetRepository<Comment>().Update(review);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
return review.Id;
}
}