예제 #1
0
        public IHttpActionResult AddStudentToCourse(int id, AddStudentViewModel model)
        {
            if (ModelState.IsValid)
            {

                try
                {
                    var result = _service.AddStudentToCourse(id, model);

                    return Content(HttpStatusCode.Created, result);

                }
                catch(AppObjectNotFoundException)
                {
                    return StatusCode(HttpStatusCode.NotFound);
                }

            }
            else
            {
                return StatusCode(HttpStatusCode.PreconditionFailed);
            }
        }
        public StudentDTO AddStudentToCourse(int id, AddStudentViewModel model)
        {
            var course = (from x in _db.Courses
                          where x.ID == id
                          select x).SingleOrDefault();
            if(course == null)
            {
                throw new AppObjectNotFoundException();
            }

            var person = (from x in _db.Persons
                          where x.SSN == model.SSN
                          select x).SingleOrDefault();

            if(person == null)
            {
                throw new AppObjectNotFoundException();
            }

            var courseStudent = new CourseStudents
            {
                PersonID = person.ID,
                CourseID = course.ID

            };

            _db.CourseStudents.Add(courseStudent);
            _db.SaveChanges();

            var returnValue = new StudentDTO
            {
                Name = person.Name,
                SSN = person.SSN
            };

            return returnValue;
        }