public async Task <ActionResult> EditDetails(CategoryEditDetailsDTOin category) { try { await categoryService.EditDetailsAsync(category, IsAdmin, UserId); return(NoContent()); } catch (InvalidOperationException ex) { return(BadRequest(new { reason = ex.Message })); } }
public async Task EditDetailsAsync(CategoryEditDetailsDTOin cat, bool isAdmin, string userId) { var categoryFd = await categoryRepo.All().FirstOrDefaultAsync(x => !x.IsDeleted && x.Id == cat.Id); if (categoryFd is null) { throw new InvalidOperationException("Category not found"); } if (categoryFd.AuthorId != userId && !isAdmin) { throw new InvalidOperationException("User not authorized to edit!"); } categoryFd.Name = cat.Name; categoryFd.ParentCategoryId = cat.ParentCategoryId; categoryFd.Description = cat.Description; categoryFd.DateOfLastEdit = DateTime.UtcNow; await categoryRepo.SaveChangesAsync(); }