public async Task HandleAsync(AddProductImageCommand command, CancellationToken cancellationToken = default) { var aggregateProduct = await _repo.GetAsync(command.Id, cancellationToken : cancellationToken); if (aggregateProduct == null) { throw new CannotFindException("No data on this Id"); } var imageUploader = new CloudinaryImageUploader(); foreach (var imageString in command.ImagesString) { var bytesImg = Convert.FromBase64String(imageString.Substring(imageString.IndexOf(',') + 1)); var img = await imageUploader.Upload(Guid.NewGuid().ToString(), bytesImg, 500, 450); command.Images.Add(new Image() { ImageUrl = img.SecureUrl.AbsoluteUri }); } aggregateProduct.AddProductImage(command); await _repo.SaveAsync(aggregateProduct, cancellationToken); }
public async Task HandleAsync(DeleteProductImageCommand command, CancellationToken cancellationToken = default) { var aggregateProduct = await _repo.GetAsync(command.Id, cancellationToken : cancellationToken); if (aggregateProduct == null) { throw new CannotFindException("No data on this Id"); } var cloudinary = new CloudinaryImageUploader(); var result = await cloudinary.DeleteByTag(command.PublicId) as DelResResult; if (result?.DeletedCounts?.Count > 0) { aggregateProduct.DeleteProductImage(command); await _repo.SaveAsync(aggregateProduct, cancellationToken); } else { throw new Exception($"Can not find images: {result}"); } }
public async Task HandleAsync(CreateProductCommand command, CancellationToken cancellationToken = new CancellationToken()) { var imageUploader = new CloudinaryImageUploader(); foreach (var imageString in command.ImagesString) { var bytesImg = Convert.FromBase64String(imageString.Substring(imageString.IndexOf(',') + 1)); var img = await imageUploader.Upload(Guid.NewGuid().ToString(), bytesImg, 500, 450); command.Images.Add(new Image() { ImageUrl = img.SecureUrl.AbsoluteUri }); } if (command.Images.Count > 0) { command.Images[0].IsMainImage = true; } var aggregateProduct = new Product(command); await _repo.SaveAsync(aggregateProduct, cancellationToken); }