public async Task <IActionResult> Delete([FromBody] FlaggedIdea flaggedIdea) { /// if the FlaggedIdeas record with the same id is not found if (!DbContext.FlaggedIdeas.Any(f => f.Id == flaggedIdea.Id)) { gAppConst.Error(ref ErrorsList, "flagged Idea not found"); return(BadRequest(ErrorsList)); } /// else the FlaggedIdeas is found /// now delete the FlaggedIdeas record DbContext.FlaggedIdeas.Remove(flaggedIdea); try { /// save the changes to the database await DbContext.SaveChangesAsync().ConfigureAwait(false); /// return 200 Ok status return(Ok(flaggedIdea)); } catch (Exception) { /// Add the error below to the error list and return bad FlaggedIdeas gAppConst.Error(ref ErrorsList, $"Server Error. Please Contact Administrator."); return(BadRequest(ErrorsList)); } }
public async Task <IActionResult> Post([FromBody] FlaggedIdea newflaggedIdea) { /// if model validation failed if (!TryValidateModel(newflaggedIdea)) { gAppConst.ExtractErrors(ModelState, ref ErrorsList); /// return bad request with all the errors return(BadRequest(ErrorsList)); } /// check the database if the same user has already posted flag with the same type if (DbContext.FlaggedIdeas.Any(f => f.Type == newflaggedIdea.Type && f.Users.Id == newflaggedIdea.Users.Id)) { /// extract the errors and return bad request containing the errors gAppConst.Error(ref ErrorsList, "Request already received."); return(BadRequest(ErrorsList)); } /// else gFlaggedIdeas object is made without any errors /// Add the new gFlaggedIdeas to the EF context await DbContext.FlaggedIdeas.AddAsync(newflaggedIdea).ConfigureAwait(false); try { /// 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", newflaggedIdea)); } 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)); } }