/// <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;
        }
Пример #2
0
        public IHttpActionResult AddTeacher(int id, AddTeacherViewModel model)
        {
            //var result = _service.AddTeacherToCourse(id, model);
            //return Created("byID", result);

            return Ok("Inside AddTeacher - Requires authorization");
        }
Пример #3
0
 /// <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 = _courseInstances.All().SingleOrDefault(x => x.ID == courseInstanceID);
     if (course == null)
     {
         throw new AppObjectNotFoundException(ErrorCodes.INVALID_COURSEINSTANCEID);
     }
     var teacher = _persons.All().SingleOrDefault(x => x.SSN == model.SSN);
     if (teacher == null)
     {
         throw  new AppObjectNotFoundException(ErrorCodes.TEACHER_IS_NOT_FOUND_IN_PERSON);
     }
     var alreadyHasMain =
         _teacherRegistrations.All().SingleOrDefault(x => x.ID == courseInstanceID && x.Type == TeacherType.MainTeacher);
     if (alreadyHasMain != null)
     {
         throw new AppObjectNotFoundException(ErrorCodes.COURSE_ALREADY_HAS_A_MAIN_TEACHER);
     }
     var adding = new TeacherRegistration
     {
         Type = model.Type,
         CourseInstanceID = courseInstanceID,
         SSN = model.SSN
     };
     _teacherRegistrations.Add(adding);
     _uow.Save();
     // TODO: implement this logic!
     return new PersonDTO
     {
         Name = teacher.Name,
         SSN = teacher.SSN
     };
 }
        /// <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)
        {
            // TODO: implement this logic!
            var course = (from c in _courseInstances.All()
                          where c.ID == courseInstanceID
                          select c).SingleOrDefault();

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

            var person = (from p in _persons.All()
                          where p.SSN == model.SSN
                          select new PersonDTO
                          {
                              Name = p.Name,
                              SSN = p.SSN
                          }).SingleOrDefault();

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

            if (model.Type == TeacherType.MainTeacher)
            {
                var mainTeacher = (from t in _teacherRegistrations.All()
                                   where t.CourseInstanceID == courseInstanceID
                                   where t.Type == TeacherType.MainTeacher
                                   select t).SingleOrDefault();

                if (mainTeacher != null)
                {
                    throw new AppValidationException("COURSE_ALREADY_HAS_A_MAIN_TEACHER");
                }
            }

            var teacherRegistration = (from t in _teacherRegistrations.All()
                                       where t.CourseInstanceID == courseInstanceID
                                       where t.SSN == model.SSN
                                       select t).SingleOrDefault();
            if(teacherRegistration != null)
            {
                throw new AppValidationException("PERSON_ALREADY_REGISTERED_TEACHER_IN_COURSE");
            }

            teacherRegistration = new TeacherRegistration()
            {
                CourseInstanceID = courseInstanceID,
                SSN = model.SSN,
                Type = TeacherType.MainTeacher
            };

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

            return person;
        }
        /// <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)
        {
            // Verify that the course exists.
            var course = _courseInstances.All().SingleOrDefault(x => x.ID == courseInstanceID);
            if(course == null)
            {
                throw new AppObjectNotFoundException();
            }
            // Verify that the person exists.
            var person = _persons.All().SingleOrDefault(x => x.SSN == model.SSN);
            if (person == null) {
                throw new AppObjectNotFoundException();
            }

            // Check if person is already registered as a teacher in the course
            var alreadyRegistered = _teacherRegistrations.All().SingleOrDefault(x => x.CourseInstanceID == courseInstanceID && x.SSN == person.SSN);
            if (alreadyRegistered != null) {
                throw new AppValidationException("PERSON_ALREADY_REGISTERED_TEACHER_IN_COURSE");
            }

            // Check if adding a main teacher or assistant teacher
            if (model.Type == TeacherType.MainTeacher)
            {
                // If we find no main teacher for this course, then we add the person as the new main teacher.
                var teacher = _teacherRegistrations.All().SingleOrDefault(x => x.CourseInstanceID == courseInstanceID && x.Type == TeacherType.MainTeacher);
                if (teacher == null)
                {
                    var addMainTeacher = new TeacherRegistration
                    {
                        SSN = person.SSN,
                        CourseInstanceID = courseInstanceID,
                        Type = model.Type
                    };
                    _teacherRegistrations.Add(addMainTeacher);
                    _uow.Save();
                }
                else
                {
                    throw new AppValidationException("COURSE_ALREADY_HAS_A_MAIN_TEACHER");
                }
            }
            else // We're adding an assistant teacher.
            {
                var addAssistantTeacher = new TeacherRegistration
                {
                    SSN = person.SSN,
                    CourseInstanceID = courseInstanceID,
                    Type = model.Type
                };
                _teacherRegistrations.Add(addAssistantTeacher);
                _uow.Save();
            }

            return new PersonDTO
            {
                SSN = person.SSN,
                Name = person.Name
            };
        }
		/// <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 = _courseInstances.All().SingleOrDefault(x => x.ID == courseInstanceID);
			if (course == null)
			{
				throw new AppObjectNotFoundException(ErrorCodes.INVALID_COURSEINSTANCEID);
			}

			// TODO: implement this logic!
			return null;
		}
Пример #7
0
        public void AddTeacher_InvalidCourse()
        {
            // Arrange:
            var model = new AddTeacherViewModel
            {
                SSN  = SSN_GUNNA,
                Type = TeacherType.AssistantTeacher
            };
            // Note: the method uses test data defined in [TestInitialize]

            // Act:
            _service.AddTeacherToCourse(INVALID_COURSEID, model);
        }
Пример #8
0
        public void AddTeacher_AlreadyWithMainTeacher()
        {
            // Arrange:
            var model = new AddTeacherViewModel
            {
                SSN  = SSN_GUNNA,
                Type = TeacherType.MainTeacher
            };
            // Note: the method uses test data defined in [TestInitialize]

            // Act:
            _service.AddTeacherToCourse(COURSEID_VEFT_20153, model);
        }
Пример #9
0
		/// <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 = _courseInstances.All().SingleOrDefault(c => c.ID == courseInstanceID);

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

            var teacher = _persons.All().SingleOrDefault(t => t.SSN == model.SSN);

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


            var isCurrentTeacher = _teacherRegistrations.All().SingleOrDefault(tr => tr.CourseInstanceID == courseInstanceID && tr.SSN == model.SSN);

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

            var currentMainTeacherOfCourse = _teacherRegistrations.All().SingleOrDefault(tr => tr.CourseInstanceID == courseInstanceID && tr.Type == TeacherType.MainTeacher);

            if (currentMainTeacherOfCourse != null)
            {
                throw new AppValidationException("COURSE_ALREADY_HAS_A_MAIN_TEACHER");
            }

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

            _teacherRegistrations.Add(teacherRegistration);
            _uow.Save();
            return new PersonDTO { Name = teacher.Name, SSN = teacher.SSN };
		}
Пример #10
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;
        }
Пример #11
0
		public IHttpActionResult AddTeacher(int id, AddTeacherViewModel model)
		{
			var result = _service.AddTeacherToCourse(id, model);
			return Created("TODO", 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)
        {
            // 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;
        }
Пример #13
0
        public void AddTeacher_InvalidTeacher()
        {
            // Arrange:
            var model = new AddTeacherViewModel
            {
                SSN  = INVALID_SSN,
                Type = TeacherType.MainTeacher
            };
            // Note: the method uses test data defined in [TestInitialize]

            // Act:
            _service.AddTeacherToCourse(COURSEID_VEFT_20163, model);
        }
Пример #14
0
        public void AddTeacher_WithValidTeacherAndCourse()
        {
            // Arrange:
            var model = new AddTeacherViewModel
            {
                SSN  = SSN_GUNNA,
                Type = TeacherType.MainTeacher
            };
            var prevCount = _teacherRegistrations.Count;
            // Note: the method uses test data defined in [TestInitialize]

            // Act:
            var dto = _service.AddTeacherToCourse(COURSEID_VEFT_20163, model);

            // Assert:

            // Check that the dto object is correctly populated:
            Assert.AreEqual(SSN_GUNNA, dto.SSN);
            Assert.AreEqual(NAME_GUNNA, dto.Name);

            // Ensure that a new entity object has been created:
            var currentCount = _teacherRegistrations.Count;
            Assert.AreEqual(prevCount + 1, currentCount);

            // Get access to the entity object and assert that
            // the properties have been set:
            var newEntity = _teacherRegistrations.Last();
            Assert.AreEqual(COURSEID_VEFT_20163, newEntity.CourseInstanceID);
            Assert.AreEqual(SSN_GUNNA, newEntity.SSN);
            Assert.AreEqual(TeacherType.MainTeacher, newEntity.Type);

            // Ensure that the Unit Of Work object has been instructed
            // to save the new entity object:
            Assert.IsTrue(_mockUnitOfWork.GetSaveCallCount() > 0);
        }
Пример #15
0
        public void AddTeacher_PersonAlreadyRegisteredAsTeacherInCourse()
        {
            // Arrange:
            var model = new AddTeacherViewModel
            {
                SSN  = SSN_DABS,
                Type = TeacherType.AssistantTeacher
            };
            // Note: the method uses test data defined in [TestInitialize]

            // Act:
            _service.AddTeacherToCourse(COURSEID_VEFT_20153, model);
        }
		/// <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;
		}