public async Task <IActionResult> CreateNewCategory(string userId, [FromBody] CategoryForSavingDto categoryForSavingDto) { if (userId != User.FindFirst(ClaimTypes.NameIdentifier).Value) { return(Unauthorized()); } var category = _mapper.Map <Category>(categoryForSavingDto); _unitOfWork.Categories.Add(category); if (await _unitOfWork.Categories.SaveAll()) { return(CreatedAtAction(nameof(GetCategoryById), new { id = category.Id }, category)); } throw new Exception("Creating category failed on save."); }
public async Task <IActionResult> UpdateCategory(string userId, string categoryId, CategoryForSavingDto categoryForSavingDto) { if (userId != User.FindFirst(ClaimTypes.NameIdentifier).Value) { return(Unauthorized()); } var categoryFromRepo = await _unitOfWork.Categories.GetCategoryById(categoryId); if (categoryFromRepo == null) { return(BadRequest("Cannot find the category to update.")); } _mapper.Map(categoryForSavingDto, categoryFromRepo); if (await _unitOfWork.Categories.SaveAll()) { return(NoContent()); } throw new Exception($"Updating category {categoryId} failed on save."); }