Ejemplo n.º 1
0
        public IActionResult CreateProduct([FromBody] DTOCreateProduct body)
        {
            try
            {
                var validator        = new CreateProductValidation();
                var rusultValidation = validator.Validate(body);
                if (!rusultValidation.IsValid)
                {
                    return(BadRequest(rusultValidation.Errors));
                }

                try
                {
                    var newProduct = _createProductService.Execute(body);

                    if (newProduct != null)
                    {
                        var dto = _mapper.Map <DTOProduct>(newProduct);
                        return(Created($"{ControllerContext.HttpContext.Request.Path.Value}", dto));
                    }

                    return(BadRequest("Não foi possivel realizar o cadastro tente novamente."));
                }
                catch (ValidationOnServiceException ex)
                {
                    return(BadRequest(ex.Message));
                }
            }
            catch
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, ErroMessage));
            }
        }
Ejemplo n.º 2
0
        public Product Execute(DTOCreateProduct createProduct)
        {
            if (string.IsNullOrEmpty(createProduct.Name) || string.IsNullOrEmpty(createProduct.Description))
            {
                throw new ValidationOnServiceException("Um ou mais campos estão invalidos.");
            }

            var checkIfCategoryExists = _categoryRepository.GetById(createProduct.CategoryId);

            if (checkIfCategoryExists == null)
            {
                throw new ValidationOnServiceException("A categoria informada não existe.");
            }

            var checkIfProductExists = _productRepository.GetByName(createProduct.Name);

            if (checkIfProductExists != null)
            {
                throw new ValidationOnServiceException("Nome do produto já está em uso.");
            }

            var complementaryData = new ComplementaryProductData
            {
                WarrantyTime = createProduct.WarrantyTime,
                Weight       = createProduct.Weight
            };


            var photos = GetUploadedPhotos(createProduct.Photos);

            var newProduct = new Product
            {
                Name        = createProduct.Name,
                Description = createProduct.Description,
                CategoryId  = checkIfCategoryExists.Id,
                Price       = createProduct.Price,
                Quantity    = createProduct.Quantity,
                ComplementaryProductData = complementaryData,
                Photos = photos.Select(x => new ProductPhoto
                {
                    Name     = x.Name,
                    Url      = x.Url,
                    MimiType = x.Mime
                }).ToList()
            };

            var createdUser = _productRepository.Insert(newProduct);

            _unitOfWork.Save();

            return(createdUser);
        }