public async Task <IActionResult> AddWorkspaceCategoryAsync(WorkspaceCategoryEditModel model) { try { var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value; if (userId == null) { throw new AppException("UC1001"); } var workspaceId = User.Claims.FirstOrDefault(c => c.Type == "WorkspaceId")?.Value; if (string.IsNullOrWhiteSpace(workspaceId)) { throw new AppException("UC1001"); } var categoryNameExists = await _categoryService.CheckCategoryNameExistsAsync(model.Name, int.Parse(workspaceId)); if (categoryNameExists) { throw new AppException("UC1001"); } var category = new Category { Name = model.Name, WorkspaceId = int.Parse(workspaceId) }; var createdCategory = await _categoryService.CreateWorkspaceCategoryAsync(category, int.Parse(workspaceId)); return(Ok(new { id = createdCategory.Id, name = createdCategory.Name, created = createdCategory.Created })); } catch (Exception ex) { return(BadRequest(new { ex.Message })); } }
public async Task <IActionResult> UpdateWorkspaceCategoryAsync(int id, WorkspaceCategoryEditModel model) { try { var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value; if (userId == null) { throw new AppException("UC1001"); } var workspaceId = User.Claims.FirstOrDefault(c => c.Type == "WorkspaceId")?.Value; if (string.IsNullOrWhiteSpace(workspaceId)) { throw new AppException("UC1001"); } var category = await _categoryService.GetCategoryByWorkspaceIdAsync(id, int.Parse(workspaceId)); if (category == null) { throw new AppException("UC1001"); } category.Name = model.Name; var updatedCategory = await _categoryService.UpdateWorkspaceCategoryAsync(category, int.Parse(workspaceId)); return(Ok(new { id = updatedCategory.Id, email = updatedCategory.Name, registered = updatedCategory.Created })); } catch (Exception ex) { return(BadRequest(new { ex.Message })); } }