public CourseDetailsDTO GetCourseById(int courseId) { var course = _db.Courses.SingleOrDefault(c => c.Id == courseId); if (course == null) { return(null); } var result = new CourseDetailsDTO { Id = course.Id, StartDate = course.StartDate, EndDate = course.EndDate, Name = _db.CourseTemplates.Where(t => t.Template == course.CourseTemplate) .Select(c => c.CourseName).FirstOrDefault(), Students = (from sr in _db.Enrollments where sr.CourseId == course.Id && sr.Status == "Enrolled" join s in _db.Students on sr.StudentSSN equals s.SSN select new StudentDTO { Name = s.Name, SSN = s.SSN }).ToList(), MaxStudents = _db.Courses.FirstOrDefault(ct => ct.Id == courseId).MaxStudents }; return(result); }
/// <summary> /// Adds new course to the Database /// </summary> /// <param name="c">The course you want to create</param> /// <returns>Returns CourseDetailsDTO of the newly created course</returns> public CourseDetailsDTO AddCourse(CourseViewModel c) { var addCourse = new Course { MaxStudents = c.MaxStudents, StartDate = c.StartDate, EndDate = c.EndDate, Semester = c.Semester, TemplateID = c.TemplateID, }; _db.Courses.Add(addCourse); _db.SaveChanges(); var getCourse = _db.Courses.SingleOrDefault(x => x.ID == addCourse.ID); var ret = new CourseDetailsDTO { EndDate = getCourse.EndDate, ID = getCourse.ID, Name = _db.CourseTemplates.SingleOrDefault(x => x.TemplateID == getCourse.TemplateID).Name, Semester = getCourse.Semester, StartDate = getCourse.StartDate, StudentCount = 0 }; return(ret); }
public CourseDetailsDTO GetCourseById(int courseId) { //var course = _db.Courses.SingleOrDefault(c => c.Id == courseId); var course = checkIfCourseExsists(courseId); var result = new CourseDetailsDTO { Id = course.Id, StartDate = course.StartDate, EndDate = course.EndDate, Name = _db.CourseTemplates.Where(t => t.Template == course.CourseTemplate) .Select(c => c.CourseName).FirstOrDefault(), MaxStudents = course.MaxStudents, Students = (from sr in _db.Enrollments where sr.CourseId == course.Id join s in _db.Students on sr.StudentSSN equals s.SSN where sr.NotRemoved select new StudentDTO { SSN = s.SSN, Name = s.Name }).ToList() }; return(result); }
public IActionResult CreateCourse([FromBody] CreateCourseDTO course) { try { CourseDetailsDTO newCourse = _service.CreateCourse(course); var location = Url.Link("GetCourseById", new { id = newCourse.ID }); return(Created(location, newCourse)); } catch (InvalidParametersException e) { return(BadRequest(e.Message)); } }
public IHttpActionResult GetCourse(int ID) { //CourseDetailDTO CourseDetailsDTO retCourse = _service.GetCourseByID(ID); if (retCourse == null) { return(NotFound()); } return(Ok(retCourse)); }
public IActionResult GetStudents(int id) { CourseDetailsDTO dto = _service.GetCourseByID(id); if (dto == null) { return(NotFound()); } else { return(Ok(_service.GetStudentsInCourse(id))); } }
public IActionResult EditCourse([FromBody] EditCourseViewModel toEdit, int id) { CourseDetailsDTO dto = _service.GetCourseByID(id); if (dto != null) { _service.EditCourse(toEdit, id); return(Ok()); } else { return(NotFound()); } }
public IActionResult GetCourseByID(int id) { //TODO: Should return a more detailed object describing that course CourseDetailsDTO dto = _service.GetCourseByID(id); if (dto != null) { return(Ok(dto)); } else { return(NotFound()); } }
public IActionResult PutCourseById(int id, [FromBody] CourseDetailsDTO course) { try { _service.PutCourseById(id, course.StartDate, course.EndDate, course.MaxStudents); return(Ok()); } catch (AppObjectNotFoundException) { return(NotFound("No course was found with that ID")); } catch (InvalidParametersException e) { return(BadRequest(e.Message)); } }
public IActionResult DeleteCourse(int id) { //TODO: should remove the given course CourseDetailsDTO dto = _service.GetCourseByID(id); if (dto != null) { _service.DeleteCourse(id); return(Ok()); } else { return(NotFound()); } }
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.PreconditionFailed); } }
public IHttpActionResult UpdateCourse(int id, UpdateCourseViewModel editedCourse) { if (!ModelState.IsValid || editedCourse.StartDate.Equals(DateTime.MinValue) || editedCourse.EndDate.Equals(DateTime.MinValue)) { throw new HttpResponseException(HttpStatusCode.PreconditionFailed); } try { CourseDetailsDTO course = _service.UpdateCourse(id, editedCourse); var location = Url.Link("GetCourse", new { id = course.ID }); return(Created(location, course)); } catch (CourseNotFoundException) { throw new HttpResponseException(HttpStatusCode.NotFound); } }
/// <summary> /// Get course by id /// Throws AppObjectNotFound if course does not exist /// </summary> /// <param name="ID">id of course example 1</param> /// <returns>The coruse in CourseDetailsDTO</returns> public CourseDetailsDTO GetCourseByID(int ID) { var result = _db.Courses.SingleOrDefault(x => x.ID == ID); if (result == null) { throw new AppObjectNotFoundException(); } else { var c = new CourseDetailsDTO(); c.ID = result.ID; c.Name = _db.CourseTemplates.SingleOrDefault(x => x.TemplateID == result.TemplateID).Name; c.StartDate = result.StartDate; c.EndDate = result.EndDate; c.StudentCount = 0; c.Semester = result.Semester; return(c); } }
public IActionResult DeleteFromCourse(int id, int ssnID) { string ssn = ssnID.ToString(); CourseDetailsDTO dto = _service.GetCourseByID(id); if (dto != null) { if (_service.DeleteFromCourse(id, ssn)) { return(StatusCode(204)); } else { //student not found in course by given ssn return(NotFound()); } } else { //course not found by given id return(NotFound()); } }
/// <summary> /// Gets the course with the given ID. /// </summary> /// <param name="ID">The ID of the course to get.</param> /// <returns>Returns null if the answer is empty. Otherwise the given course.</returns> public CourseDetailsDTO GetCourseByID(int ID) { CourseDetailsDTO retCourse = (from c in _context.Courses join ct in _context.CourseTemplates on c.TemplateID equals ct.TemplateID where c.ID == ID select new CourseDetailsDTO { ID = c.ID, Name = ct.Name, Semester = c.Semester, StartDate = c.StartDate, EndDate = c.EndDate, TemplateID = c.TemplateID, MaxStudents = c.MaxStudents }).SingleOrDefault(); if (retCourse == null) { return(null); } List <StudentDTO> studentList = (from s in _context.Students join se in _context.StudentEnrollment on s.ID equals se.StudentID where se.CourseID == retCourse.ID select new StudentDTO { ID = s.ID, SSN = s.SSN, Name = s.Name }).ToList(); retCourse.Students = studentList; return(retCourse); }