public async Task <Response <GetAllProductsViewModel> > Handle(UpdateProductCommand command, CancellationToken cancellationToken)
            {
                var product = await _productRepository.GetByIdAsync(command.Id);

                if (product == null)
                {
                    throw new ApiException($"Product Not Found.");
                }
                else
                {
                    product.Name              = command.Name;
                    product.Price             = command.Price;
                    product.Description       = command.Description;
                    product.Image             = command.Image;
                    product.ProductCategoryId = command.ProductCategoryId;
                    product.Barcode           = command.Barcode;
                    await _productRepository.UpdateAsync(product);

                    var result = await _unitOfWork.Commit(cancellationToken);

                    if (result > 0)
                    {
                        var productsViewModel = _mapper.Map <GetAllProductsViewModel>(product);
                        var productCategory   = await _productCategoryRepository.GetByIdAsync(productsViewModel.ProductCategoryId);

                        var productCategoryViewModel = _mapper.Map <GetAllProductCategoryViewModel>(productCategory);
                        productsViewModel.ProductCategory = productCategoryViewModel;
                        return(new Response <GetAllProductsViewModel>(productsViewModel));
                    }

                    return(new Response <GetAllProductsViewModel>(null));
                }
            }
            public async Task <Response <ProductCategory> > Handle(GetProductCategoryByIdQuery query, CancellationToken cancellationToken)
            {
                var product = await _productCategoryRepository.GetByIdAsync(query.Id);

                if (product == null)
                {
                    throw new NotFoundException(nameof(ProductCategory), query.Id);
                }
                return(new Response <ProductCategory>(product));
            }
Ejemplo n.º 3
0
            public async Task <Response <int> > Handle(DeleteProductCategoryByIdCommand command, CancellationToken cancellationToken)
            {
                var category = await _productCategoryRepository.GetByIdAsync(command.Id);

                if (category == null)
                {
                    throw new ApiException($"Product Group Not Found.");
                }
                await _productCategoryRepository.DeleteAsync(category);

                await _unitOfWork.Commit(cancellationToken);

                return(new Response <int>(category.Id));
            }
            public async Task <Response <int> > Handle(UpdateProductCategoryCommand command, CancellationToken cancellationToken)
            {
                var category = await _productCategoryRepository.GetByIdAsync(command.Id);

                if (category == null)
                {
                    throw new ApiException($"Product Category Not Found.");
                }
                else
                {
                    category.Name = command.Name;
                    category.Tax  = command.Tax;
                    await _productCategoryRepository.UpdateAsync(category);

                    await _unitOfWork.Commit(cancellationToken);

                    return(new Response <int>(category.Id));
                }
            }
            public async Task <Response <int> > Handle(DeleteProductCategoryByIdCommand command, CancellationToken cancellationToken)
            {
                var category = await _productCategoryRepository.GetByIdAsync(command.Id);

                if (category == null)
                {
                    throw new ApiException($"Product category Not Found.");
                }
                if (command.Physical)
                {
                    await _productCategoryRepository.DeleteAsync(category);
                }
                else //Update status IsDeleted
                {
                    category.IsDeleted = true;
                    await _productCategoryRepository.UpdateAsync(category);
                }

                await _unitOfWork.Commit(cancellationToken);

                return(new Response <int>(category.Id));
            }