public Category Execute(DTOCreateCategory createCategory) { if (string.IsNullOrEmpty(createCategory.Name) || string.IsNullOrEmpty(createCategory.Description)) { throw new ValidationOnServiceException("Um ou mais campos estão invalidos."); } var checkIfCategoryExists = _categoryRepository.GetByName(createCategory.Name); if (checkIfCategoryExists != null) { throw new ValidationOnServiceException("Nome da categoria já está em uso."); } var newCategory = new Category { Name = createCategory.Name, Description = createCategory.Description, }; var createdUser = _categoryRepository.Insert(newCategory); _unitOfWork.Save(); return(createdUser); }
public IActionResult CreateCategory([FromBody] DTOCreateCategory body) { try { var validator = new CreateCategoryValidation(); var rusultValidation = validator.Validate(body); if (!rusultValidation.IsValid) { return(BadRequest(rusultValidation.Errors)); } try { var newCategory = _createCategoryService.Execute(body); if (newCategory != null) { var dto = _mapper.Map <DTOCategory>(newCategory); 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)); } }