public IHttpActionResult AddStudent(int id,AddStudentViewModel student) { AddStudentDTO newStudent = new AddStudentDTO(); newStudent.TemplateID = student.TemplateID; newStudent.Semester = student.Semester; newStudent.SSN = student.SSN; try { _context.AddStudentToCourse(newStudent); var location = Url.Link("StudentInCourse", new { id = id }); return Created(location, student.SSN); } catch(Exception ex) { if (ex is KeyNotFoundException) return NotFound(); else return InternalServerError(); } }
public void AddStudentToCourse(AddStudentDTO dto) { var course = (from c in _db.Courses join t in _db.CourseTemplate on c.TemplateID equals t.TemplateID where c.Semester == dto.Semester select c).SingleOrDefault(); if (course == null) throw new KeyNotFoundException(); var student = _db.Student.SingleOrDefault(x => x.SSN == dto.SSN); if (student == null) { throw new KeyNotFoundException(); } Entities.StudentInCourse toInsert = new Entities.StudentInCourse(); toInsert.CourseID = course.ID; toInsert.StudentID = student.SSN; _db.StudentInCourse.Add(toInsert); _db.SaveChanges(); }