public async Task <Category> UpdateAsync(int id, InsertUpdateCategoryCommand command) { command.Name = command.Name.Trim(); var category = await _categoryRepository.GetAsync(id); if (category == null) { throw new EntityNotFoundException("Category"); } if (category.Name != command.Name) { var existingCategory = await _categoryRepository.GetByNameAsync(command.Name); if (existingCategory != null) { throw new BusinessException($"Category {command.Name} already exists"); } } category.Name = command.Name; category.Description = command.Description; await _categoryRepository.SaveChangesAsync(); return(category); }
public async Task <Category> InsertAsync(InsertUpdateCategoryCommand command) { command.Name = command.Name.Trim(); var existingCategory = await _categoryRepository.GetByNameAsync(command.Name); if (existingCategory != null) { throw new BusinessException($"Category {command.Name} already exists"); } var category = new Category { Name = command.Name, Description = command.Description, }; _categoryRepository.Add(category); await _categoryRepository.SaveChangesAsync(); return(category); }
public async Task <IActionResult> Put(int id, InsertUpdateCategoryCommand command) { await _categoryService.UpdateAsync(id, command); return(NoContent()); }
public async Task <ActionResult <CreatedEntityDto> > Post(InsertUpdateCategoryCommand command) { var category = await _categoryService.InsertAsync(command); return(new CreatedEntityDto(category.Id)); }