public IHttpActionResult PutChoice(int id, Choice choice) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != choice.ChoiceId) { return BadRequest(); } db.Entry(choice).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ChoiceExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public IHttpActionResult PostChoice(Choice choice) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Choices.Add(choice); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = choice.ChoiceId }, choice); }
//check if the same user has made choices for the same year and term private bool multiPick(Choice choice) { if (choice != null) { var sameStudentYearTerm = db.Choices.Where(c => c.StudentId == choice.StudentId && c.YearTermId == choice.YearTermId).Count(); if (sameStudentYearTerm != 0) { return false; } } return true; }
// Check for non-duplicate options private bool validChoices(Choice choice) { var list = new List<int>(); list.Add((int)choice.FirstChoiceOptionId); list.Add((int)choice.SecondChoiceOptionId); list.Add((int)choice.ThirdChoiceOptionId); list.Add((int)choice.FourthChoiceOptionId); if (list.Count != list.Distinct().Count()) { return false; } else { return true; } }