public async Task EditProductAsync(EditProductRequest request, CancellationToken ct) { request.Validate(); if (request.Price == 0) { throw new Exception("O preço do produto não pode ser R$0,00"); } var product = request.ConvertToEntity(); var productregistred = await _productRepository.GetProductByIdAsync(product.ProductId, ct); if (productregistred == null || productregistred.ProductId == Guid.Empty) { throw new Exception("Produto não encotrado para o ID fornecido"); } if (request.File != null) { var urlImage = await ManagerImage.SaveFileAsync(request.File); product.UrlImage = urlImage; ManagerImage.DeleteFile(productregistred.UrlImage); } else { product.UrlImage = productregistred.UrlImage; } await _productRepository.EditProductAsync(product, ct); }
public async Task<ProductResponse> CreateProductAsync(ProductRequest request, CancellationToken ct) { request.Validate(); var pathImage = await ManagerImage.SaveFileAsync(request.File); if (request.Price == 0) { throw new Exception("O preço do produto não pode ser R$0,00"); } var product = request.ConvertToEntity(); product.UrlImage = pathImage; product.Validate(); await _productRepository.CreateProductAsync(product, ct); if (product.ProductId == Guid.Empty) { throw new Exception("Erro ao registrar produto"); } return _mapper.Map<ProductResponse>(product); }