public void Insert(CoursePriority coursePriority)
        {
            if (coursePriority == null)
            {
                _logger.Error("ArgumentNullException coursePriority");
                throw new ArgumentNullException("coursePriority");
            }
            _logger.Debug(string.Format(_culture, "Insert course with code {0} and profile id {1}", coursePriority.Code, coursePriority.ProfileId));

            var courses  = GetAllCoursesFromProfile(coursePriority.ProfileId);
            var profiles = GetAllProfiles();

            coursePriority.Id = GenerateId(profiles);

            if (courses.Any(c => c.Code == coursePriority.Code))
            {
                string errorMessage = string.Format(_culture, "Course with the code {0} already exists", coursePriority.Code);
                _logger.Error("ArgumentException: " + errorMessage);
                throw new ArgumentException(errorMessage);
            }

            courses.Add(coursePriority);
            profiles.FirstOrDefault(profile => profile.Id == coursePriority.ProfileId).Courses = courses;

            WriteAllProfilesToFile(profiles);
            _logger.Debug(string.Format(_culture, "Course inserted with code {0} and profile id {1} and generated id {2}", coursePriority.Code, coursePriority.ProfileId, coursePriority.Id));
        }
        private static List <OpleidingsplanGenerator.Models.Course> ConvertCourses(IEnumerable <Integration.Course> courses, CourseProfile profile, Collection <RestEducationPlanCourse> restCourses)
        {
            _logger.Debug("ConvertCourses");
            List <OpleidingsplanGenerator.Models.Course> coursesToPlan = new List <OpleidingsplanGenerator.Models.Course>();

            foreach (var course in courses)
            {
                OpleidingsplanGenerator.Models.Course courseToPlan = Mapper.Map <OpleidingsplanGenerator.Models.Course>(course);

                if (profile != null)
                {
                    CoursePriority coursePriority = profile.Courses.FirstOrDefault(profileCourse => profileCourse.Code == course.Code);
                    if (coursePriority != null)
                    {
                        courseToPlan.Priority = coursePriority.Priority;
                    }
                }

                RestEducationPlanCourse restCourse = restCourses.FirstOrDefault(profileCourse => profileCourse.Code == course.Code);
                if (restCourse != null && restCourse.Priority != 0)
                {
                    courseToPlan.Priority = restCourse.Priority;
                }

                coursesToPlan.Add(courseToPlan);
            }

            return(coursesToPlan);
        }
        public void Update(CoursePriority coursePriority)
        {
            if (coursePriority == null)
            {
                _logger.Error("ArgumentNullException coursePriority");
                throw new ArgumentNullException("coursePriority");
            }

            _logger.Debug(string.Format(_culture, "Update course with id {0}, code {1} and profile id {2}", coursePriority.Id, coursePriority.Code, coursePriority.ProfileId));

            var            courses        = GetAllCoursesFromProfile(coursePriority.ProfileId);
            CoursePriority courseToUpdate = courses.FirstOrDefault(p => p.Id == coursePriority.Id);

            if (courseToUpdate == null)
            {
                string errorMessage = string.Format(_culture, "No course found with id {0}", coursePriority.Id);
                _logger.Error("ArgumentException: " + errorMessage);
                throw new ArgumentException(errorMessage);
            }

            int index = courses.IndexOf(courseToUpdate);

            courses[index] = coursePriority;

            var profiles = GetAllProfiles();

            profiles.FirstOrDefault(profile => profile.Id == coursePriority.ProfileId).Courses = courses;

            WriteAllProfilesToFile(profiles);
            _logger.Debug(string.Format(_culture, "Course updated with id {0}, code {1} and profile id {2}", courseToUpdate.Id, courseToUpdate.Code, courseToUpdate.ProfileId));
        }
        public void Update_CoursePriority()
        {
            // Arrange
            ICourseDataMapper dataMapper = new CourseJsonDataMapper(_profilePath);
            CoursePriority    course     = new CoursePriority
            {
                Id        = 1,
                ProfileId = 1,
                Code      = "WINDOWDEV",
                Priority  = 10,
            };

            // Act
            dataMapper.Update(course);

            // Assert
            IDataMapper <CourseProfile> dataMapperProfile = new ProfileJsonDataMapper(_profilePath);
            var profiles = dataMapperProfile.FindAll();

            Assert.AreEqual(3, profiles.Count());
            Assert.AreEqual(31, profiles.ElementAt(0).Courses.Count());
            Assert.AreEqual(30, profiles.ElementAt(1).Courses.Count());
            Assert.AreEqual(27, profiles.ElementAt(2).Courses.Count());
            Assert.AreEqual(10, profiles.ElementAt(0).Courses.First(c => c.Id == 1).Priority);
            Assert.AreEqual("WINDOWDEV", profiles.ElementAt(0).Courses.First(c => c.Id == 1).Code);
        }
        public void Insert_NewCoursePriorityAdded()
        {
            // Arrange
            ICourseDataMapper dataMapper = new CourseJsonDataMapper(_profilePath);
            CoursePriority    course     = new CoursePriority
            {
                ProfileId = 1,
                Code      = "WINDOWDEV",
                Priority  = 5,
            };

            // Act
            dataMapper.Insert(course);

            // Assert
            IDataMapper <CourseProfile> dataMapperProfile = new ProfileJsonDataMapper(_profilePath);
            var profiles = dataMapperProfile.FindAll();

            Assert.AreEqual(3, profiles.Count());
            Assert.AreEqual(32, profiles.ElementAt(0).Courses.Count());
            Assert.AreEqual(30, profiles.ElementAt(1).Courses.Count());
            Assert.AreEqual(27, profiles.ElementAt(2).Courses.Count());
            Assert.AreEqual(5, profiles.ElementAt(0).Courses.First(c => c.Id == 90).Priority);
            Assert.AreEqual("WINDOWDEV", profiles.ElementAt(0).Courses.First(c => c.Id == 90).Code);
        }
 // DELETE: api/Course/course
 public void Delete(CoursePriority course)
 {
     if (ModelState.IsValid)
     {
         _courseManager.Delete(course);
     }
     else
     {
         _logger.Warn(string.Format(_culture, "Delete course {0} modelstate not valid", course.Code));
     }
 }
        public void Delete_WithNotExistingCourseInProfile_ExceptionThrowed()
        {
            // Arrange
            ICourseDataMapper dataMapper = new CourseJsonDataMapper(_profilePath);
            CoursePriority    course     = new CoursePriority
            {
                ProfileId = 1,
                Code      = "NODOTNET"
            };

            // Act
            dataMapper.Delete(course);

            // Assert ArgumentException
        }
        public void Insert_ExistingProfile_ExceptionThrowed()
        {
            // Arrange
            ICourseDataMapper dataMapper = new CourseJsonDataMapper(_profilePath);
            CoursePriority    course     = new CoursePriority
            {
                ProfileId = 1,
                Code      = "NETFOUNB",
                Priority  = 5,
            };

            // Act
            dataMapper.Insert(course);

            // Assert ArgumentException
        }
        public void Insert_WithCorruptedFile_ExceptionThrowed()
        {
            // Arrange
            ICourseDataMapper dataMapper = new CourseJsonDataMapper("../../Data/corrupted.json");
            CoursePriority    course     = new CoursePriority
            {
                ProfileId = 1,
                Code      = "WINDOWDEV",
                Priority  = 5,
            };

            // Act
            dataMapper.Insert(course);

            // Assert FileNotFoundException
        }
        public void Insert_WithNotExistingPath_ExceptionThrowed()
        {
            // Arrange
            ICourseDataMapper dataMapper = new CourseJsonDataMapper("noPath");
            CoursePriority    course     = new CoursePriority
            {
                ProfileId = 1,
                Code      = "WINDOWDEV",
                Priority  = 5,
            };

            // Act
            dataMapper.Insert(course);

            // Assert FileNotFoundException
        }
        public void Update_WithNotExistingCourse_ExceptionThrowed()
        {
            // Arrange
            ICourseDataMapper dataMapper = new CourseJsonDataMapper(_profilePath);
            CoursePriority    course     = new CoursePriority
            {
                Id        = 99,
                ProfileId = 1,
                Code      = "WINDOWDEV",
                Priority  = 10,
            };

            // Act
            dataMapper.Update(course);

            // Assert ArgumentException
        }
        public void Delete_CoursePriority()
        {
            // Arrange
            ICourseDataMapper dataMapper = new CourseJsonDataMapper(_profilePath);
            CoursePriority    course     = new CoursePriority
            {
                Id        = 1,
                ProfileId = 1,
                Code      = "WINDEV"
            };

            // Act
            dataMapper.Delete(course);

            // Assert
            IDataMapper <CourseProfile> dataMapperProfile = new ProfileJsonDataMapper(_profilePath);
            var profiles = dataMapperProfile.FindAll();

            Assert.AreEqual(3, profiles.Count());
            Assert.AreEqual(30, profiles.ElementAt(0).Courses.Count());
            Assert.AreEqual(30, profiles.ElementAt(1).Courses.Count());
            Assert.AreEqual(27, profiles.ElementAt(2).Courses.Count());
        }
        public void Delete(CoursePriority coursePriority)
        {
            if (coursePriority == null)
            {
                _logger.Error("ArgumentNullException coursePriority");
                throw new ArgumentNullException("coursePriority");
            }
            _logger.Debug(string.Format(_culture, "Delete course with id {0}, code {1} and profile id {2}", coursePriority.Id, coursePriority.Code, coursePriority.ProfileId));

            var courses = GetAllCoursesFromProfile(coursePriority.ProfileId);

            if (courses == null)
            {
                string errorMessage = string.Format(_culture, "No profile found with id {0}", coursePriority.ProfileId);
                _logger.Error("ArgumentException: " + errorMessage);
                throw new ArgumentException(errorMessage);
            }

            CoursePriority courseToDelete = courses.FirstOrDefault(c => c.Id == coursePriority.Id);

            if (courseToDelete == null)
            {
                string errorMessage = string.Format(_culture, "No course found with code {0} in profile {1}", coursePriority.Code, coursePriority.ProfileId);
                _logger.Error("ArgumentException: " + errorMessage);
                throw new ArgumentException(errorMessage);
            }

            courses.Remove(courseToDelete);

            var profiles = GetAllProfiles();

            profiles.FirstOrDefault(profile => profile.Id == coursePriority.ProfileId).Courses = courses;

            WriteAllProfilesToFile(profiles);
            _logger.Debug(string.Format(_culture, "Course deleted with id {0}, code {1} and profile id {2}", courseToDelete.Id, courseToDelete.Code, courseToDelete.ProfileId));
        }
 public void Delete(CoursePriority course)
 {
     _courseDataMapper.Delete(course);
 }
 public void Update(CoursePriority course)
 {
     _courseDataMapper.Update(course);
 }
 public void Insert(CoursePriority course)
 {
     _courseDataMapper.Insert(course);
 }