public async Task<IHttpActionResult> PutClass(UpdateClassBindingModel uclassvm) { if(!ModelState.IsValid) { return BadRequest(ModelState); } try { await _classManager.UpdateClass(uclassvm); } catch(Exception ex) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message)); } return StatusCode(HttpStatusCode.NoContent); }
/// <summary> /// Updates a class using details in the given model /// </summary> /// <param name="ucbm">Model containing details of the class to update</param> public async Task UpdateClass(UpdateClassBindingModel ucbm) { // Find the class and update its properties var @class = await _db.Classes.FindAsync(ucbm.Id); if(@class == null) { throw new Exception("No class with id: " + ucbm.Id); } // Update attributes @class.Title = ucbm.Title; @class.Prefix = ucbm.Prefix; @class.CourseNumber = ucbm.CourseNumber; @class.Section = ucbm.Section; @class.GradeDistribution = ucbm.GradeDistribution; _db.Entry(@class).State = EntityState.Modified; try { await _db.SaveChangesAsync(); } catch(DbUpdateConcurrencyException) { // Deleted before update maybe? if(!ClassExists(@class.Id)) { throw new Exception("No class with id: " + ucbm.Id); } } }