[Authorize(gAppConst.AccessPolicies.LevelTwo)] /// Done public async Task <IActionResult> Delete([FromBody] gCategoryTag category) { try { /// if the Category record with the same id is not found if (!await DbContext.Categories.AnyAsync(d => d.Id == category.Id).ConfigureAwait(false)) { gAppConst.Error(ref ErrorsList, "Category not found"); return(BadRequest(ErrorsList)); } /// If the category is in use by any idea then do not allow delete if (await DbContext.CategoriesToIdeas.AnyAsync(c => c.CategoryId == category.Id).ConfigureAwait(false)) { gAppConst.Error(ref ErrorsList, "Category tag is in use by an idea."); return(BadRequest(ErrorsList)); } /// else the Category is found /// now delete the Category record DbContext.Categories.Remove(category); /// save the changes to the database await DbContext.SaveChangesAsync().ConfigureAwait(false); /// return 200 Ok status return(Ok($"Department '{category.Name}' was deleted")); } catch (Exception) { /// Add the error below to the error list gAppConst.Error(ref ErrorsList, $"Server Error. Please Contact Administrator."); return(BadRequest(ErrorsList)); } }
[Authorize(gAppConst.AccessPolicies.LevelTwo)] /// Done public async Task <IActionResult> Post([FromBody] gCategoryTag newCategory) { try { /// if model validation failed if (!TryValidateModel(newCategory)) { gAppConst.ExtractErrors(ModelState, ref ErrorsList); /// return bad request with all the errors return(BadRequest(ErrorsList)); } /// check the database to see if a department with the same name exists if (await DbContext.Categories.AnyAsync(d => d.Name == newCategory.Name).ConfigureAwait(false)) { /// extract the errors and return bad request containing the errors gAppConst.Error(ref ErrorsList, "Category already exists."); return(BadRequest(ErrorsList)); } /// else Category object is made without any errors /// Add the new Category to the EF context await DbContext.Categories.AddAsync(newCategory).ConfigureAwait(false); /// save the changes to the data base await DbContext.SaveChangesAsync().ConfigureAwait(false); /// return 201 created status with the new object /// and success message return(Created("Success", newCategory)); } catch (Exception) // DbUpdateException, DbUpdateConcurrencyException { /// Add the error below to the error list and return bad request gAppConst.Error(ref ErrorsList, "Server Error. Please Contact Administrator."); return(BadRequest(ErrorsList)); } }