public async Task <RemoveShopCommentValidationResult> Validate(RemoveProductCommentCommand command) { if (command == null) { throw new ArgumentNullException(nameof(command)); } var shop = await _productRepository.Get(command.ProductId); if (shop == null) { return(new RemoveShopCommentValidationResult(ErrorDescriptions.TheProductDoesntExist)); } var comment = shop.Comments == null ? null : shop.Comments.FirstOrDefault(c => c.Id == command.CommentId); if (comment == null) { return(new RemoveShopCommentValidationResult(ErrorDescriptions.TheCommentDoesntExist)); } if (comment.Subject != command.Subject) { return(new RemoveShopCommentValidationResult(ErrorDescriptions.TheCommentCannotBeRemovedByYou)); } return(new RemoveShopCommentValidationResult()); }
public async Task Handle(RemoveProductCommentCommand message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } var record = await _productRepository.Get(message.ProductId); if (record == null) { return; } var productComment = record.Comments == null ? null : record.Comments.FirstOrDefault(f => f.Id == message.CommentId); if (productComment == null) { return; } record.RemoveComment(productComment); await _productRepository.Update(record); _eventPublisher.Publish(new ProductCommentRemovedEvent { Id = message.CommentId, ProductId = message.ProductId, AverageScore = record.AverageScore, ShopId = record.ShopId, CommonId = message.CommonId, NbComments = record.Comments == null ? 0 : record.Comments.Count() }); }
public async Task <IActionResult> Execute(RemoveProductCommentCommand command) { if (command == null) { throw new ArgumentNullException(nameof(command)); } var validationResult = await _validator.Validate(command); if (!validationResult.IsValid) { var error = _responseBuilder.GetError(ErrorCodes.Server, validationResult.Message); return(_controllerHelper.BuildResponse(System.Net.HttpStatusCode.BadRequest, error)); } _commandSender.Send(command); return(new OkResult()); }