コード例 #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));
            }
        }