public async Task<IHttpActionResult> PutStudent(string id, Student student) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != student.username) { return BadRequest(); } db.Entry(student).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StudentExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> PostStudent(Student student) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.students.Add(student); try { await db.SaveChangesAsync(); } catch (DbUpdateException) { if (StudentExists(student.username)) { return Conflict(); } else { throw; } } return CreatedAtRoute("DefaultApi", new { id = student.username }, student); }
private void UpdateStudyGroupStudent(List<StudyGroup> studyGroups,Student student) { List<StudyGroup> oldStudyGroups = student.studyGroups.ToList(); if (oldStudyGroups.Count > 0) { // removes all old relations between student and studygroup foreach (StudyGroup sg in oldStudyGroups) { student.studyGroups.Remove(sg); } db.Entry(student).State = EntityState.Modified; db.SaveChanges(); /* db.students.Attach(student); var entry1 = db.Entry(student); entry1.Property(e => e.studyGroups).IsModified = true; db.SaveChanges(); */ } if (studyGroups.Count == 1 && studyGroups[0].id == "none") { return; } // creates new relations between the student and the studygroups provided foreach (var study in studyGroups) { string id = study.id; StudyGroup sg = db.studyGroup.First(x => x.id.Equals(id)); if (sg != null) { student.studyGroups.Add(sg); } } /* db.students.Attach(student); var entry2 = db.Entry(student); entry2.Property(e => e.studyGroups).IsModified = true; db.SaveChanges(); */ db.Entry(student).State = EntityState.Modified; db.SaveChanges(); }
private void InsertOrUpdateDeviceStudent(List<Device> devices, Student student) { // can only be one device atm, so hardcoding it... Device newDevice = devices[0]; try { Device d = db.devices.First(x => x.id.Equals(newDevice.id)); if (d != null) { d.Student = student; d.deviceType = newDevice.deviceType; d.token = newDevice.token; db.Entry(d).State = EntityState.Modified; db.SaveChanges(); } else { newDevice.Student = student; db.devices.Add(newDevice); db.SaveChanges(); } } catch { newDevice.Student = student; db.devices.Add(newDevice); db.SaveChanges(); } }
private void PopulateStudent2() { Student student = new Student() { username = "******", name = "Test tester", email = "*****@*****.**" }; StudyGroup idrett = db.studyGroup.First(x => x.id.Equals("idrettsfag")); // Device htc = db.devices.First(x => x.id.Equals("HT451WM08832")); // student.Devices.Add(htc); student.studyGroups.Add(idrett); db.students.Add(student); //htc.Student = student; db.SaveChanges(); }
// Populates the Student table private void PopulateStudent() { Student student = new Student() { username = "******", name = "Viktor Setervang", email = "*****@*****.**" }; StudyGroup data = db.studyGroup.First(x => x.id.Equals("datateknologi")); StudyGroup idrett = db.studyGroup.First(x => x.id.Equals("idrettsfag")); Device htc = db.devices.First(x => x.id.Equals("HT451WM08832")); student.Devices.Add(htc); student.studyGroups.Add(data); student.studyGroups.Add(idrett); db.students.Add(student); //htc.Student = student; db.SaveChanges(); }
/// <summary> /// Creates a new user based on a external access token. /// </summary> /// <param name="provider"></param> /// <param name="verifiedAccessToken"></param> /// <param name="externalAccessToken"></param> /// <returns></returns> private async Task<IHttpActionResult> CreateNewUserFromExternalAccesToken(string provider, ParsedExternalAccessToken verifiedAccessToken, string externalAccessToken) { RegisterExternalBindingModel model = new RegisterExternalBindingModel() { UserName = verifiedAccessToken.email, // this is null Provider = provider, ExternalAccessToken = externalAccessToken }; Student student = new Student(); student.username = verifiedAccessToken.email; student.email = verifiedAccessToken.email; KompetansetorgetServerContext db = new KompetansetorgetServerContext(); db.students.Add(student); db.SaveChanges(); return await RegisterExternal(model); }