public void CanAddPreference()
        {
            Professor p = new ProfessorProvider(session).GetProfessorByID(19);
            Course c = new CourseProvider(session).GetCourseByID(6);
            var prefID = new PreferencesId(p, c);

            var pref = new Preference(prefID) { Weight = 3 };
            provider.AddPreference(pref);
            Preference testPref = provider.GetPreferenceByProfIdAndCourseId(19, 6);
            Assert.AreEqual(c, testPref.Id.Course);
            Assert.AreEqual(p, testPref.Id.Professor);
            //Assert.IsNotNull(testPref);
            //provider.AddPreference(pref);
        }
        public object Create(string Id)
        {
            ViewData["Title"] = "Create Preference";
            ViewData["Message"] = "Create a Preference";
            if (string.IsNullOrEmpty(Id)) {
                return RedirectToAction("Index", "Professor");

            }
            int profId;
            try {
                profId = Convert.ToInt32(Id);
            } catch (Exception)   //not an integer
            {
                return RedirectToAction("Index", "Professor");
            }
            Professor professor = new ProfessorProvider().GetProfessorByID(profId);
            if (professor == null) {
                return RedirectToAction("Index", "Professor");
            }
            var ProfPrefs = (new PreferencesProvider().GetAllPreferencesByProfId(profId));
            var settings = new Settings();
            int lookFor = settings.NumberOfPreferencesPerProfessor;
            while (true)
            {
                int lookFor1 = lookFor;
                if (ProfPrefs.Where(s => s.Weight == lookFor1).Count() == 0)
                {
                    break;
                }
                lookFor--;
                if (lookFor == 0)
                {
                    break;
                }
            }
            int weight = lookFor;
            var preference = new Preference
                             	{
                             		Id = { Professor = professor },
                             		Weight = weight
                             	};
            PrepareCoursesList(preference);
            ViewData["ProfId"] = preference.Id.Professor.ProfId;
            return View(preference);
        }
        public object Create([Bind(Prefix = "")]Course course, string ProfessorList)
        {
            ViewData["Title"] = "Create Course";
            ViewData["Message"] = "Create a Course";
            //To DO
            //UpdateModel(course);  // to do

            // redisplay form immediately if there are input format errors
            if (!ModelState.IsValid)
            {
                ModelState.CopyValidationExceptions();
                PrepareProfessorList(course);
                return View("Create", course);
            }

            try
            {
                if (ProfessorList != null)
                    if (!ProfessorList.Equals("0"))
                    {
                        var profp = new ProfessorProvider();
                        Professor prof = profp.GetProfessorByID(Convert.ToInt32(ProfessorList));
                        course.AssignedProfessor = prof;
                    }
                new CourseProvider().AddCourse(course);
                ViewData["Message"] = "Course created";
                return View("Saved");
            }
            catch (RuleViolationException vex)
            {
                ViewData.ModelState.CopyValidationExceptions(vex);
                PrepareProfessorList(course);
                return View("Create", course);
            }
            catch (Exception err)
            {
                ViewData.ModelState.CopyValidationExceptions(err, "course");
                PrepareProfessorList(course);
                return View("Create");
            }
        }
        public object Edit([Bind(Prefix = "")] Course course, string id, string ProfessorList)
        {
            ViewData["Title"] = "Edit Course";
            ViewData["Message"] = "Edit Course Details!";
            var cp = new CourseProvider();
            int courseId;
            try
            {
                courseId = Convert.ToInt32(id);
            }
            catch (Exception)   //not an integer
            {
                return RedirectToAction("Index", "Course");
            }

            course = cp.GetCourseByID(courseId);
            // redisplay form immediately if there are input format errors
            if (!ModelState.IsValid)
            {
                ModelState.CopyValidationExceptions();
                PrepareProfessorList(course);
                return View("Edit", course);
            }
            try {
                UpdateModel(course, new[] { "CourseName", "Monday", "Tuesday", "Wednesday",
                                            "Thursday", "Friday", "Saturday", "Sunday",
                                            "StartHour", "StartMinute", "EndHour", "EndMinute"});
                if (!ProfessorList.Equals("0")) {
                    Professor prof = new ProfessorProvider().GetProfessorByID(Convert.ToInt32(ProfessorList));
                    course.AssignedProfessor = prof;
                } else {
                    course.AssignedProfessor = null;
                }
                cp.UpdateCourse(course);
                return View("Saved");
            } catch (RuleViolationException vex) {
                ViewData.ModelState.CopyValidationExceptions(vex);
                PrepareProfessorList(course);
                return View(course);
            }
            catch (Exception err) {
                ViewData.ModelState.CopyValidationExceptions(err, "course");
                PrepareProfessorList(course);
                return View(course);
            }
        }
 public object Create([Bind(Prefix = "")]Preference preference, string CourseList, string ProfId)
 {
     ViewData["Title"] = "Create Preference";
     ViewData["Message"] = "Create a Preference";
     try
     {
         Course course = (new CourseProvider()).GetCourseByID(Convert.ToInt32(CourseList));
         Professor professor = new ProfessorProvider().GetProfessorByID(Convert.ToInt32(ProfId));
         preference.Id = new PreferencesId {Professor = professor, Course = course};
         new PreferencesProvider().AddPreference(preference);
         ViewData["Message"] = "Preference created";
         ViewData["ProfId"] = preference.Id.Professor.ProfId;
         return View("Saved");
     }
     catch (RuleViolationException vex)
     {
         ViewData.ModelState.CopyValidationExceptions(vex);
         ViewData["ProfId"] = preference.Id.Professor.ProfId;
         PrepareCoursesList(preference);
         return View(preference);
     }
     catch (Exception err)
     {
         ViewData.ModelState.CopyValidationExceptions(err, "preference");
         ViewData["ProfId"] = preference.Id.Professor.ProfId;
         PrepareCoursesList(preference);
         return View(preference);
     }
 }