private void ReplaceAuctionImage(UserReplaceAuctionImageCommand request, CancellationToken cancellationToken, CorrelationId correlationId) { var auction = _auctionRepository.FindAuction(request.AuctionId); if (auction == null) { throw new CommandException($"Cannot find auction {request.AuctionId}"); } var file = File.ReadAllBytes(request.TempPath); File.Delete(request.TempPath); var img = new AuctionImageRepresentation(new AuctionImageMetadata(request.Extension), file); var newImg = _auctionImageService.AddAuctionImage(img); auction.ReplaceImage(newImg, request.ImgNum); _auctionRepository.UpdateAuction(auction); try { _eventBusService.Publish(auction.PendingEvents, correlationId, request); } catch (Exception) { _auctionImageService.RemoveAuctionImage(newImg); throw; } }
private void AddImage(UserAddAuctionImageCommand request, CancellationToken cancellationToken, CorrelationId correlationId) { var auction = _auctionRepository.FindAuction(request.AuctionId); if (auction == null) { throw new CommandException($"Cannot find auction {request.AuctionId}"); } if (!auction.Owner.UserId.Equals(request.SignedInUser.UserId)) { throw new CommandException( $"User {request.SignedInUser.UserId} cannot modify auction ${auction.AggregateId}"); } var file = File.ReadAllBytes(request.TempPath); File.Delete(request.TempPath); var img = new AuctionImageRepresentation(new AuctionImageMetadata(request.Extension), file); var newImg = _auctionImageService.AddAuctionImage(img); auction.AddImage(newImg); _auctionRepository.UpdateAuction(auction); try { _eventBusService.Publish(auction.PendingEvents, correlationId, request); } catch (Exception e) { _logger.LogWarning(e, "Error while trying to publish events"); _auctionImageService.RemoveAuctionImage(newImg); throw; } }