/// <summary> /// adds a new instance of a course to the database /// </summary> /// <param name="model"></param> /// <returns></returns> public CourseDTO AddNewCourse(CourseViewModel model) { //check if course already in database //add the course var newCourse = new Course { TemplateID = model.TemplateID, Semester = model.Semester, StartDate = model.StartDate, EndDate = model.EndDate, MaxStudents = model.MaxStudents }; _db.Courses.Add(newCourse); _db.SaveChanges(); //find the Name of the course var courseTemplate = (from ct in _db.CourseTemplates where ct.TemplateID == model.TemplateID select ct).SingleOrDefault(); var result = new CourseDTO { ID = newCourse.ID, Name = courseTemplate.Name, StartDate = newCourse.StartDate, StudentCount = 0 }; return result; }
public IHttpActionResult AddNewCourse(CourseViewModel model) { try { var result = _service.AddNewCourse(model); var location = Url.Link("GetCourseById", new { id = result.ID }); return Created(location, result); } catch (DuplicateEntryException) { return StatusCode(HttpStatusCode.BadRequest); }; }
public IHttpActionResult AddCourse(CourseViewModel newCourse) { if (!ModelState.IsValid) { throw new HttpResponseException(HttpStatusCode.PreconditionFailed); } try { CourseDetailsDTO course = _service.AddCourse(newCourse); // may throw exception var location = Url.Link("GetCourse", new { id = course.ID }); return Created(location, course); } catch (TemplateCourseNotFoundException) { throw new HttpResponseException(HttpStatusCode.NotFound); } }
/// <summary> /// This method adds a Course with the information from the CourseViewModel /// </summary> /// <param name="newCourse">Course containing all the information needed to add a new course</param> /// <returns></returns> public CourseDetailsDTO AddCourse(CourseViewModel newCourse) { // Check if the course exists var courseTemplate = _db.CourseTemplates.SingleOrDefault(x => x.TemplateID == newCourse.TemplateID); if (courseTemplate == null) { throw new TemplateCourseNotFoundException(); } Entities.Course course = new Entities.Course { ID = _db.Courses.Any() ? _db.Courses.Max(x => x.ID) + 1 : 1, TemplateID = courseTemplate.ID, Semester = newCourse.Semester, StartDate = newCourse.StartDate, EndDate = newCourse.EndDate, MaxStudents = newCourse.MaxStudents }; _db.Courses.Add(course); _db.SaveChanges(); return new CourseDetailsDTO { ID = course.ID, TemplateID = courseTemplate.TemplateID, Name = courseTemplate.Name, Description = courseTemplate.Description, Semester = course.Semester, StartDate = newCourse.StartDate, EndDate = newCourse.EndDate, StudentCount = _db.StudentEnrollment.Count(x => x.CourseID == course.ID && x.IsActive == true && x.IsOnWaitingList == false), Students = (from sr in _db.StudentEnrollment join s in _db.Students on sr.StudentID equals s.ID where sr.CourseID == course.ID && sr.IsActive == true && sr.IsOnWaitingList == false select new StudentDTO { SSN = s.SSN, Name = s.Name }).ToList() }; }
/// <summary> /// This method adds a Course with the information from the CourseViewModel /// </summary> /// <param name="newCourse">Course containing all the information needed to add a new course</param> /// <returns></returns> public CourseDetailsDTO AddCourse(CourseViewModel newCourse) { // Check if the course exists var courseTemplate = _db.CourseTemplates.SingleOrDefault(x => x.TemplateID == newCourse.CourseID); if (courseTemplate == null) { throw new TemplateCourseNotFoundException(); } Entities.Course course = new Entities.Course { ID = _db.Courses.Any() ? _db.Courses.Max(x => x.ID) + 1 : 1, TemplateID = courseTemplate.ID, Semester = newCourse.Semester, StartDate = newCourse.StartDate, EndDate = newCourse.EndDate }; _db.Courses.Add(course); _db.SaveChanges(); return new CourseDetailsDTO { ID = course.ID, TemplateID = courseTemplate.TemplateID, Name = courseTemplate.Name, Description = courseTemplate.Description, Semester = course.Semester, StartDate = newCourse.StartDate, EndDate = newCourse.EndDate, StudentCount = _db.StudentEnrollment.Count(x => x.CourseID == course.ID) }; }
public IHttpActionResult AddNewCourse(CourseViewModel model) { return StatusCode(HttpStatusCode.Created); }