public async Task <BaseResponse <ProductDto> > Handle(UpdateProductCommand request, CancellationToken cancellationToken)
        {
            var response = new BaseResponse <ProductDto>();

            var product = await _productRespository.GetByIdAsync(request.Id);

            if (product == null)
            {
                response.AddError("Product not found.", "404");
                return(response);
            }

            product.Name        = request.Name;
            product.Description = request.Description;
            product.Price       = request.Price;

            if (request.CategoryId != product.Category.Id)
            {
                var category = await _categoryRepository.GetByIdAsync(request.CategoryId);

                if (category == null)
                {
                    response.AddError("Category not found.", "404");
                    return(response);
                }

                product.Category = category;
            }


            await _productRespository.UpdateAsync(request.Id, product);

            response.Data = _mapper.Map <ProductDto>(product);
            return(response);
        }
Ejemplo n.º 2
0
        public async Task <BaseResponse <ProductDto> > Handle(GetProductByIdRequest request, CancellationToken cancellationToken)
        {
            var response = new BaseResponse <ProductDto>();

            var product = await _productRespository.GetByIdAsync(request.Id);

            if (product == null)
            {
                response.AddError("Product not found.", "404");
                return(response);
            }

            response.Data = _mapper.Map <ProductDto>(product);
            return(response);
        }
Ejemplo n.º 3
0
        public async Task <BaseResponse <bool> > Handle(DeleteProductCommand request, CancellationToken cancellationToken)
        {
            var response = new BaseResponse <bool>();
            var product  = await _productRespository.GetByIdAsync(request.Id);

            if (product == null)
            {
                response.AddError("Product not found.", "404");
                return(response);
            }

            await _productRespository.SafeDeleteAsync(product);

            response.Data = true;

            return(response);
        }