private IActionResult SaveVoluntaryMethodologyAsync(VoluntaryMethodology item) { //Check that Value/Name is unique if (_context.VoluntaryMethodology.AsNoTracking().FirstOrDefault(x => x.Value == item.Value && x.VoluntaryMethodologyId != item.VoluntaryMethodologyId) != null) { return(BadRequest("Duplicate entry/value found.")); } var exiting = _context.VoluntaryMethodology.FirstOrDefault(x => x.VoluntaryMethodologyId == item.VoluntaryMethodologyId); if (exiting == null) { //ADD HelperExtensions.ClearIdentityValue(ref item); HelperExtensions.ClearNullableInts(ref item); _context.VoluntaryMethodology.Add(item); return(Created(item)); } else { //UPDATE _context.Entry(exiting).CurrentValues.SetValues(item); return(Updated(exiting)); } }
public async Task <IActionResult> Post([FromBody] Question question) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var exiting = _context.Questions.FirstOrDefault(x => x.Id == question.Id || (x.Key == question.Key && x.Goal.Id == question.Goal.Id)); if (exiting == null) { //ADD HelperExtensions.ClearIdentityValue(question); HelperExtensions.ClearNullableInts(question); _context.Questions.Add(question); await _context.SaveChangesAsync(); return(Created(question)); } else { //UPDATE //_context.Entry(exiting).CurrentValues.SetValues(question); if (exiting.Value != question.Value) { exiting.Value = question.Value; await _context.SaveChangesAsync(); } return(Updated(exiting)); } }
private IActionResult SavePersonAsync(Person item) { //Check that Value/Name is unique if (_context.Person.AsNoTracking().FirstOrDefault(x => x.EmailAddress == item.EmailAddress && x.PersonId != item.PersonId) != null) { return(BadRequest("Duplicate entry/value found.")); } var exiting = _context.Person.FirstOrDefault(x => x.PersonId == item.PersonId); if (exiting == null) { //ADD HelperExtensions.ClearIdentityValue(ref item); HelperExtensions.ClearNullableInts(ref item); _context.Person.Add(item); return(Created(item)); } else { //UPDATE _context.Entry(exiting).CurrentValues.SetValues(item); return(Updated(exiting)); } }
public async Task <IActionResult> Post([FromBody] Goal goal) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var exiting = _context.Goals.FirstOrDefault(x => x.Id == goal.Id); if (exiting == null) { if (!HelperExtensions.CheckGoalCreateValues(goal)) { return(BadRequest(new MissingFieldException("CreateUserId value required"))); } //ADD GOAL HelperExtensions.ClearNullableInts(goal); _context.Goals.Add(goal); await _context.SaveChangesAsync(); return(Created(goal)); } else { if (!HelperExtensions.CheckGoalUpdateValues(goal)) { return(BadRequest(new MissingFieldException("LastUpdateUserId value required"))); } //UPDATE goal.CreateDate = exiting.CreateDate; goal.CreateUser = exiting.CreateUser; _context.Entry(exiting).CurrentValues.SetValues(goal); await _context.SaveChangesAsync(); //ADD/UPDATE QUESTIONS var qc = new QuestionsController(_context); foreach (var question in goal.Questions) { question.Goal = exiting; await qc.Post(question); } return(Updated(exiting)); } }