Пример #1
0
        /// <summary>
        /// You should implement this function, such that all tests will pass.
        /// Method that adds a teacher to a course with a given id.
        /// The required attributes are given with a view model class.
        /// </summary>
        /// <param name="courseInstanceID">The ID of the course instance which the teacher will be registered to.</param>
        /// <param name="model">The data which indicates which person should be added as a teacher, and in what role.</param>
        /// <returns>Should return basic information about the person.</returns>
        public PersonDTO AddTeacherToCourse(int courseInstanceID, AddTeacherViewModel model)
        {
            var course = _courseInstances.All().SingleOrDefault(x => x.ID == courseInstanceID);
            var teacher = _persons.All().SingleOrDefault(x => x.SSN == model.SSN);
            if(course == null || teacher == null)
            {
                throw new AppObjectNotFoundException();
            }

            var teacherInCourse = _teacherRegistrations.All().SingleOrDefault(x => x.CourseInstanceID == courseInstanceID && x.SSN == model.SSN);
            if(teacherInCourse != null)
            {
                throw new AppValidationException("PERSON_ALREADY_REGISTERED_TEACHER_IN_COURSE");
            }

            var mainTeacher = _teacherRegistrations.All().SingleOrDefault(x => x.CourseInstanceID == courseInstanceID && x.Type == TeacherType.MainTeacher);
            if (mainTeacher != null && model.Type == TeacherType.MainTeacher)
            {
                throw new AppValidationException("COURSE_ALREADY_HAS_A_MAIN_TEACHER");
            }

            TeacherRegistration tr = new TeacherRegistration
            {
                CourseInstanceID = courseInstanceID,
                SSN = teacher.SSN,
                Type = model.Type
            };

            _teacherRegistrations.Add(tr);
            _uow.Save();

            PersonDTO result = new PersonDTO
            {
                Name = teacher.Name,
                SSN = teacher.SSN
            };

            return result;
        }
        /// <summary>
        /// You should implement this function, such that all tests will pass.
        /// </summary>
        /// <param name="courseInstanceID">The ID of the course instance which the teacher will be registered to.</param>
        /// <param name="model">The data which indicates which person should be added as a teacher, and in what role.</param>
        /// <returns>Should return basic information about the person.</returns>
        public PersonDTO AddTeacherToCourse(int courseInstanceID, AddTeacherViewModel model)
        {
            // 1. Validation
            var courseInstance = _courseInstances.All().SingleOrDefault(x => x.ID == courseInstanceID);

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

            var person = _persons.All().SingleOrDefault(x => x.SSN == model.SSN);

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

            var registration = _teacherRegistrations.All().SingleOrDefault(x => x.SSN == model.SSN);

            if(registration != null)
            {
                throw new AppValidationException("PERSON_ALREADY_REGISTERED_TEACHER_IN_COURSE");
            }

            // Check if model's type is a main teacher, if so and there already is a main teacher
            // we throw an error.
            if(model.Type == TeacherType.MainTeacher)
            {
                var teacherRegistrations = _teacherRegistrations.All().Where(x => x.CourseInstanceID == courseInstance.ID).ToList();

                foreach(TeacherRegistration teacher in teacherRegistrations)
                {
                    if(teacher.Type == TeacherType.MainTeacher)
                    {
                        throw new AppValidationException("COURSE_ALREADY_HAS_A_MAIN_TEACHER");
                    }
                }
            }

            // Everything seems to be in order, create the entity model and
            // assign the teacher to the course
            var registrationEntity = new TeacherRegistration
            {
                CourseInstanceID = courseInstanceID,
                SSN = model.SSN,
                Type = model.Type
            };

            _teacherRegistrations.Add(registrationEntity);
            _uow.Save();

            // Creating the return object
            var personDTO = new PersonDTO
            {
                Name = person.Name,
                SSN = person.SSN
            };

            return personDTO;
        }
        /// <summary>
        /// You should implement this function, such that all tests will pass.
        /// </summary>
        /// <param name="courseInstanceID">The ID of the course instance which the teacher will be registered to.</param>
        /// <param name="model">The data which indicates which person should be added as a teacher, and in what role.</param>
        /// <returns>Should return basic information about the person.</returns>
        public PersonDTO AddTeacherToCourse(int courseInstanceID, AddTeacherViewModel model)
        {
            // Check if courseID is correct
            var courseInstance = (from c in _courseInstances.All()
                where c.ID == courseInstanceID
                select c).SingleOrDefault();

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

            // Check if teacher SSN is in the system
            var teacher = (from p in _persons.All()
                where p.SSN == model.SSN
                select p).SingleOrDefault();

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

            // Get the list of teachers assigned for this course
            var teachersForCourse = (from tr in _teacherRegistrations.All()
                                     where tr.CourseInstanceID == courseInstanceID
                                     select tr).ToList();

            // If the teacher being registered is a main teacher
            // and the course already has a main teacher registered;
            // throw an exception
            if (model.Type == TeacherType.MainTeacher &&
                teachersForCourse.Any(t => t.Type == TeacherType.MainTeacher))
            {
                throw new AppValidationException("COURSE_ALREADY_HAS_A_MAIN_TEACHER");
            }

            // If the teacher being registered is already registered as a
            // teacher in the course; throw an exception
            if (teachersForCourse.Any(t => t.SSN == model.SSN))
            {
                throw new AppValidationException("PERSON_ALREADY_REGISTERED_TEACHER_IN_COURSE");
            }

            // If all seems to be OK, register the teacher to the course
            var teacherRegistration = new TeacherRegistration
            {
                SSN = model.SSN,
                CourseInstanceID = courseInstanceID,
                Type = model.Type
            };

            _teacherRegistrations.Add(teacherRegistration);
            _uow.Save();

            // Display the teacher that was added to the course
            var personDTO = new PersonDTO
            {
                Name = teacher.Name,
                SSN = teacher.SSN
            };

            return personDTO;
        }
		/// <summary>
		/// You should implement this function, such that all tests will pass.
		/// </summary>
		/// <param name="courseInstanceID">The ID of the course instance which the teacher will be registered to.</param>
		/// <param name="model">The data which indicates which person should be added as a teacher, and in what role.</param>
		/// <returns>Should return basic information about the person.</returns>
		public PersonDTO AddTeacherToCourse(int courseInstanceID, AddTeacherViewModel model)
		{
            var course = (from c in _courseInstances.All()
                          where c.ID == courseInstanceID
                          select c).SingleOrDefault();
            // Make sure that the course exists
            if (course == null)
            {
                throw new AppObjectNotFoundException();
            }

            var person = (from p in _persons.All()
                          where p.SSN == model.SSN
                          select p).SingleOrDefault();
            // Make sure that the person exists
            if (person == null)
            {
                throw new AppObjectNotFoundException();
            }

            if(model.Type == TeacherType.MainTeacher)
            {
                // Check if the course aldready has a main teacher
                var teacherReg = (from tr in _teacherRegistrations.All()
                                  where tr.CourseInstanceID == course.ID
                                        && tr.Type == TeacherType.MainTeacher
                                  select tr).SingleOrDefault();
                if(teacherReg != null)
                {
                    throw new AppValidationException("COURSE_ALREADY_HAS_A_MAIN_TEACHER");
                }
            }

            // Check if the person is already a teacher in the course
            var alreadyTeacher = (from tr in _teacherRegistrations.All()
                                  where tr.CourseInstanceID == course.ID
                                        && tr.SSN == person.SSN
                                  select tr).SingleOrDefault();
            if(alreadyTeacher != null)
            {
                throw new AppValidationException("PERSON_ALREADY_REGISTERED_TEACHER_IN_COURSE");
            }

            // Create TeacerRegistration entity
            var teacherRegistration = new TeacherRegistration
            {
                SSN                 = person.SSN,
                CourseInstanceID    = course.ID,
                Type                = model.Type
            };

            // Add to database
            _teacherRegistrations.Add(teacherRegistration);
            _uow.Save();

            var result = new PersonDTO
            {
                SSN = model.SSN,
                Name = person.Name
            };

            return result;
		}