Exemple #1
0
        /// <summary>
        /// Method responsible for update data.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        /// <exception cref="Core.DomainObjects.Exceptions.NotFoundException"></exception>
        /// <exception cref="ApplicationException"></exception>
        public async Task <ApplicationDataResult <ProductDto> > UpdateASync(Guid id, AddOrUpdateProductDto data)
        {
            ValidationResult validationResult = await _validatorProduct.ValidateAsync(data);

            if (validationResult.IsValid is false)
            {
                return(FactoryFromValidationResult <ProductDto>(validationResult));
            }

            Product product = await _productRepository.FindAsync(id);

            if (product is null)
            {
                throw ExceptionsFactory.FactoryNotFoundException <Product>(id);
            }

            Product result = await _productRepository.FindAsync(new ProductFilter { Description = data.Description });

            if (result != null && result.Id != id)
            {
                throw ExceptionsFactory.FactoryApplicationException(ErrorMessages.ProductFound);
            }

            product.Update(data.Description, data.IsActive);

            _productRepository.Update(product);

            await _productRepository.UnitOfWork.CommitAsync();

            return(FactoryResultFromData(_mapper.Map <ProductDto>(product)));
        }