コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        public async Task <IActionResult> Put(int id, InsertUpdateCategoryCommand command)
        {
            await _categoryService.UpdateAsync(id, command);

            return(NoContent());
        }
コード例 #4
0
        public async Task <ActionResult <CreatedEntityDto> > Post(InsertUpdateCategoryCommand command)
        {
            var category = await _categoryService.InsertAsync(command);

            return(new CreatedEntityDto(category.Id));
        }