public async Task Handle(RemoveServiceCommentCommand message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } var record = await _serviceRepository.Get(message.ServiceId); if (record == null) { return; } var serviceComment = record.Comments == null ? null : record.Comments.FirstOrDefault(f => f.Id == message.CommentId); if (serviceComment == null) { return; } record.RemoveComment(serviceComment); await _serviceRepository.Update(record); _eventPublisher.Publish(new ServiceCommentRemovedEvent { Id = message.CommentId, ShopId = record.ShopId, ServiceId = message.ServiceId, AverageScore = record.AverageScore, NbComments = record.Comments == null ? 0 : record.Comments.Count() }); }
public async Task <RemoveShopCommentValidationResult> Validate(RemoveServiceCommentCommand command) { if (command == null) { throw new ArgumentNullException(nameof(command)); } var shop = await _serviceRepository.Get(command.ServiceId); if (shop == null) { return(new RemoveShopCommentValidationResult(ErrorDescriptions.TheServiceDoesntExist)); } 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 <IActionResult> Execute(RemoveServiceCommentCommand 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()); }