public async Task<IHttpActionResult> PutCours(int id, Cours cours) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != cours.Id) { return BadRequest(); } var dbCours = db.Courses.FirstOrDefault(o => o.Id == cours.Id); var once = true; foreach(var student in cours.Students) { var s = db.Students.FirstOrDefault(o => o.Id == student.Id); if (once) { dbCours.Students.Clear(); once = false; } dbCours.Students.Add(s); } dbCours.Name = cours.Name; dbCours.Level = cours.Level; dbCours.Code = cours.Code; //dbCours.Students = cours.Students; db.Entry(dbCours).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CoursExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> PostCours(Cours cours) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Courses.Add(cours); await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new { id = cours.Id }, cours); }