Ejemplo n.º 1
0
        /// <summary>
        /// Returns a single course with the specified ID
        /// If no course is found then a course not found exception is thrown
        /// </summary>
        /// <param name="courseModel"></param>
        /// <returns></returns>
        public CourseInstanceDTO GetCourseInstanceByID(int ID)
        {
            var course = _courseInstances.All().SingleOrDefault(x => x.ID == ID);
            var courseTemplate = _courseTemplates.All().SingleOrDefault(x => x.CourseID == course.CourseID);

            if (course == null || courseTemplate == null)
            {
                throw new AppObjectNotFoundException();
            }

            var returnValue = new CourseInstanceDTO
            {
                Name = courseTemplate.Name,
                CourseInstanceID = course.ID,
                MainTeacher = "",
                TemplateID = courseTemplate.CourseID
            };

            return returnValue;
        }
		/// <summary>
		/// You should write tests for this function. You will also need to
		/// modify it, such that it will correctly return the name of the main
		/// teacher of each course.
		/// </summary>
		/// <param name="semester"></param>
		/// <returns></returns>
		public List<CourseInstanceDTO> GetCourseInstancesBySemester(string semester = null)
		{
			if (string.IsNullOrEmpty(semester))
			{
				semester = "20153";
			}

            var coursesLeftQuery = (from c in _courseInstances.All()
                                   join ct in _courseTemplates.All() on c.CourseID equals ct.CourseID
                                   where c.SemesterID == semester
                                   select new
                                   {
                                       ID = c.ID,
                                       CourseID = c.CourseID,
                                       SemesterID = c.SemesterID,
                                       Name = ct.Name
                                    }).ToList();

            var teachersRightQuery = (from tr in _teacherRegistrations.All()
                                      join p in _persons.All() on tr.SSN equals p.SSN
                                      where tr.Type == TeacherType.MainTeacher
                                      select new
                                      {
                                          CourseInstanceID = tr.CourseInstanceID,
                                          SSN = tr.SSN,
                                          Type = tr.Type,
                                          Name = p.Name
                                      }).ToList();

            List<CourseInstanceDTO> result = new List<CourseInstanceDTO>();
            foreach(var c in coursesLeftQuery)
            {
                var dto = new CourseInstanceDTO
                {
                    Name = c.Name,
                    TemplateID = c.CourseID,
                    CourseInstanceID = c.ID,
                    MainTeacher = ""
                };

                foreach (var tr in teachersRightQuery)
                {
                   if(c.ID == tr.CourseInstanceID && tr.Type == TeacherType.MainTeacher)
                   {
                        dto.MainTeacher = tr.Name;
                   }
                }
                result.Add(dto);
            }
            return result;
		}