public async Task <ActionResult <int> > Create(CategoryCreateDTOin cat) { try { int newId = await categoryService.CreateNewAsync(cat, IsAdmin, UserId); return(newId); } catch (InvalidOperationException ex) { return(BadRequest(new { reason = ex.Message })); } }
public async Task <int> CreateNewAsync(CategoryCreateDTOin cat, bool isAdmin, string userId) { if (cat.AuthorId != userId && !isAdmin) { throw new InvalidOperationException("User is not authorized to create category!"); } if (await IsNameUsedAsync(cat.Name)) { throw new InvalidOperationException($"Category name {cat.Name} is already used!"); } Category newCat = mapper.Map <Category>(cat); await this.categoryRepo.AddAssync(newCat); await this.categoryRepo.SaveChangesAsync(); return(newCat.Id); }