Esempio n. 1
0
        public async Task <ProductsResponse> SaveAsync(Products products)
        {
            try
            {
                await _productRespository.AddAsync(products);

                await _unitOfWork.CompleteAsync();

                return(new ProductsResponse(products));
            }
            catch (Exception ex)
            {
                return(new ProductsResponse($"An error occurred when saving the products:{ex.Message}"));
            }
        }
Esempio n. 2
0
        public async Task <BaseResponse <ProductDto> > Handle(CreateProductCommand request, CancellationToken cancellationToken)
        {
            var response = new BaseResponse <ProductDto>();
            var category = await _categoryRepository.GetByIdAsync(request.CategoryId);

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

            var product = _mapper.Map <Product>(request);

            product.Category  = category;
            product.CreatedAt = DateTime.Now;
            product.IsActive  = true;
            product.IsDeleted = false;

            await _productRespository.AddAsync(product);

            response.Data = _mapper.Map <ProductDto>(product);

            return(response);
        }