public IActionResult Delete(int id) { var returnResult = new ContributorActionResult(false, new List <string>(), null); try { using (var db = new BTAContext()) { if (db.Contributor.Any(x => x.ContributorId == id) == false) { returnResult.Success = false; returnResult.StatusMessages.Add(string.Format("Unable to locate contributor for id: {0}", id)); returnResult.Data = null; } else { var resultContributor = db.Contributor.Single(x => x.ContributorId == id); db.Remove(resultContributor); db.SaveChanges(); returnResult.Success = true; returnResult.Data = resultContributor; returnResult.StatusMessages.Add("Successfully deleted contributor."); } } } catch (Exception e) { Logger.LogError(e.ToString()); returnResult.Success = false; returnResult.StatusMessages.Add("An exception occurred while attempting to delete the contributor."); returnResult.Data = null; } return(StatusCode(StatusCodes.Status200OK, returnResult)); }
public IActionResult Post([FromBody] Contributor contributor) { var returnResult = new ContributorActionResult(false, new List <string>(), null); if (contributor != null) { try { using (var db = new BTAContext()) { var contrib = new Contributor() { FirstName = contributor.FirstName, MiddleName = contributor.MiddleName, LastName = contributor.LastName, AddressId = contributor.AddressId }; var resultContributor = db.Contributor.Add(contrib); db.SaveChanges(); var entity = resultContributor.Entity; if (entity != null) { returnResult.Success = true; returnResult.StatusMessages.Add("Successfully added contributor."); returnResult.Data = entity; } else { returnResult.Success = false; returnResult.StatusMessages.Add("Failed to add contributor."); returnResult.Data = entity; } } } catch (Exception e) { string errMsg = "An exception occurred while attempting to add a contributor."; Logger.LogError(e, errMsg); returnResult.Success = false; returnResult.StatusMessages.Add(errMsg); returnResult.Data = null; } } else { returnResult.Success = false; returnResult.StatusMessages.Add("Empty contributor posted for add."); returnResult.Data = null; } return(StatusCode(StatusCodes.Status200OK, returnResult)); }
public IActionResult Put([FromBody] Contributor contributor) { var returnResult = new ContributorActionResult(false, new List <string>(), null); if (contributor != null) { try { using (var db = new BTAContext()) { var resultContributor = db.Contributor.SingleOrDefault(x => x.ContributorId == contributor.ContributorId); if (resultContributor != null) { resultContributor.FirstName = contributor.FirstName; resultContributor.MiddleName = contributor.MiddleName; resultContributor.LastName = contributor.LastName; resultContributor.AddressId = contributor.AddressId; db.SaveChanges(); returnResult.Success = true; returnResult.Data = resultContributor; returnResult.StatusMessages.Add("Successfully updated contributor."); } else { string errMsg = string.Format("Unable to locate contributor for id: {0}", contributor.ContributorId); Logger.LogError(errMsg, null); returnResult.Success = false; returnResult.StatusMessages.Add(errMsg); returnResult.Data = null; } } } catch (Exception e) { string excMsg = "An exception occurred while attempting to update a contributor."; Logger.LogError(e, excMsg); returnResult.Success = false; returnResult.StatusMessages.Add(excMsg); returnResult.Data = null; } } else { returnResult.Success = false; returnResult.StatusMessages.Add("Empty contributor posted for update."); returnResult.Data = null; } return(StatusCode(StatusCodes.Status200OK, returnResult)); }