public IHttpActionResult PostCohort(CohortModel cohort) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var dbCohort = new Cohort(); dbCohort.Update(cohort); db.Cohorts.Add(dbCohort); try { db.SaveChanges(); } catch (Exception) { throw new Exception("Unable to add the cohort to the database."); } cohort.CohortId = dbCohort.CohortId; return CreatedAtRoute("DefaultApi", new { id = cohort.CohortId }, cohort); }
public IHttpActionResult PutCohort(int id, CohortModel cohort) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != cohort.CohortId) { return BadRequest(); } var dbCohort = db.Cohorts.Find(id); dbCohort.Update(cohort); db.Entry(cohort).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CohortExists(id)) { return NotFound(); } else { throw new Exception("Unable to update cohort in the database."); } } return StatusCode(HttpStatusCode.NoContent); }
public void Update(CohortModel cohort) { Name = cohort.Name; StartDate = cohort.StartDate; EndDate = cohort.EndDate; }