public void StudentCannotEnrolWhenFullyEnrolled()
        {
            Student student = new Student { Username = "******" };
            db.Students.Add(student);

            Subject subject1 = new Subject { SubjectNumber = "1", Name = "SubjectOne", MaxEnrolment = 2 };
            Subject subject2 = new Subject { SubjectNumber = "2", Name = "SubjectTwo", MaxEnrolment = 2 };
            Subject subject3 = new Subject { SubjectNumber = "3", Name = "SubjectThree", MaxEnrolment = 2 };
            Subject subject4 = new Subject { SubjectNumber = "4", Name = "SubjectFour", MaxEnrolment = 2 };
            Subject subject5 = new Subject { SubjectNumber = "5", Name = "SubjectFive", MaxEnrolment = 2 };

            db.Subjects.Add(subject1);
            db.Subjects.Add(subject2);
            db.Subjects.Add(subject3);
            db.Subjects.Add(subject4);
            db.Subjects.Add(subject5);

            db.SaveChanges();

            Student dbStudent = db.Students.FirstOrDefault(s => s.Username == "11012592");

            Subject dbSubject1 = db.Subjects.FirstOrDefault(s => s.SubjectNumber == "1");
            Subject dbSubject2 = db.Subjects.FirstOrDefault(s => s.SubjectNumber == "2");
            Subject dbSubject3 = db.Subjects.FirstOrDefault(s => s.SubjectNumber == "3");
            Subject dbSubject4 = db.Subjects.FirstOrDefault(s => s.SubjectNumber == "4");
            Subject dbSubject5 = db.Subjects.FirstOrDefault(s => s.SubjectNumber == "5");

            _sc.Enrol(dbStudent.Id, dbSubject1.Id);
            _sc.Enrol(dbStudent.Id, dbSubject2.Id);
            _sc.Enrol(dbStudent.Id, dbSubject3.Id);
            _sc.Enrol(dbStudent.Id, dbSubject4.Id);
            _sc.Enrol(dbStudent.Id, dbSubject5.Id);

            Assert.AreEqual(4, db.Students.FirstOrDefault(s => s.Username == "11012592").EnrolledSubjects.Count);
        }
        public ActionResult Create(Subject subject, List<int> SelectedPrerequisites)
        {
            bool subjectInContext = (db.Subjects.FirstOrDefault(s => s.SubjectNumber == subject.SubjectNumber) != null);
            ViewBag.allSubjects = new SelectList(db.Subjects.Where(s => s.Active), "Id", "Name");
            if (SelectedPrerequisites != null && SelectedPrerequisites.Count > 0)
            {
                //Get a list of subject where the id is contained within the list of selectedPrerequisites
                List<Subject> Prerequisites = db.Subjects.Where(m => SelectedPrerequisites.Contains(m.Id)).ToList();
                Prerequisites.ForEach(m => subject.Prerequisites.Add(m));
            }
            if (!subjectInContext)
            {
                subject.Active = true;
                if (ModelState.IsValid)
                {
                    db.Subjects.Add(subject);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            else
            {
                ModelState.AddModelError(string.Empty, "The subject already exist in the system");
            }

            return View(subject);
        }
        public void SubjectMaxEnrollmentNotReached()
        {
            Subject subject = new Subject() { Active = true, Name = "Apple", MaxEnrolment = 3, SubjectNumber = "1000", Description = "i am an apple" };

            Assert.AreEqual(0, subject.EnrolledStudents.Count);
            Assert.IsFalse(subject.MaxEnrolmentIsReached());

            subject.EnrolledStudents.Add(_students[0]);
            Assert.AreEqual(1, subject.EnrolledStudents.Count);
            Assert.IsFalse(subject.MaxEnrolmentIsReached());
        }
        public void SubjectCreation()
        {
            Subject subject = new Subject() {Active = true, Name = "Apple", MaxEnrolment = 3, SubjectNumber = "1000", Description = "i am an apple"};

            Assert.AreEqual(true, subject.Active);
            Assert.AreEqual("Apple", subject.Name);
            Assert.AreEqual("1000", subject.SubjectNumber);
            Assert.AreEqual(3, subject.MaxEnrolment);
            Assert.AreEqual("i am an apple", subject.Description);
            Assert.AreEqual(0, subject.Prerequisites.Count);
            Assert.AreEqual(0, subject.EnrolledStudents.Count);
            Assert.AreEqual(2, _subjects.Find(s => s.Name == "Apple and Orange").Prerequisites.Count);
        }
        public void CheckModelDataTypeForResultCreate()
        {
            ResultController resultController = new ResultController(ref db);
            Subject subject = new Subject() { Active = true, MaxEnrolment = 3, Name = "thisIsTestSubject", SubjectNumber = "123", Description = "" };
            db.Subjects.Add(subject);
            db.SaveChanges();
            ViewResult resultViewResult = (ViewResult)resultController.Create(subject.Id);

            Subject addedSubject = (Subject)db.Subjects.FirstOrDefault(a => a.Name == "thisIsTestSubject");
            Assert.AreEqual(subject.MaxEnrolment, addedSubject.MaxEnrolment);
            Assert.AreEqual(subject.Name, addedSubject.Name);
            Assert.AreEqual(subject.SubjectNumber, addedSubject.SubjectNumber);
            Assert.AreEqual(subject.Description, addedSubject.Description);
        }
        public void SubjectMaxEnrollmentHasReached()
        {
            Subject subject = new Subject() { Active = true, Name = "Apple", MaxEnrolment = 3, SubjectNumber = "1000", Description = "i am an apple" };

            Assert.AreEqual(0, subject.EnrolledStudents.Count);
            Assert.IsFalse(subject.MaxEnrolmentIsReached());

            foreach (Student s in _students.FindAll(s => s.Id <= subject.MaxEnrolment))
            {
                subject.EnrolledStudents.Add(s);
            }

            Assert.AreEqual(3, subject.EnrolledStudents.Count);
            Assert.IsTrue(subject.MaxEnrolmentIsReached());
        }
        public void CheckModelDataTypeForResultCreatePostBack()
        {
            ResultController resultController = new ResultController(ref db);

            Subject subject = new Subject() { Active = true, MaxEnrolment = 3, Name = "thisIsTestSubject", SubjectNumber = "123", Description = "" };
            db.Subjects.Add(subject);
            Student enrollingStudent = new Student() { Username = "******", FullName="TestFullName" };
            db.Students.Add(enrollingStudent);
            subject.EnrolledStudents.Add(enrollingStudent);
            db.SaveChanges();

            List<Result> results = new List<Result>();
            Subject addedSubject = (Subject)db.Subjects.FirstOrDefault(a => a.Name == "thisIsTestSubject");

            //ViewResult resultViewResult = (ViewResult)resultController.Create(results, addedSubject.Id);
        }
        public void StudentHasEnrolled()
        {
            Student student = new Student { Username = "******" };
            db.Students.Add(student);
            Subject subject = new Subject { SubjectNumber = "1", Name = "SubjectOne", MaxEnrolment = 2 };
            db.Subjects.Add(subject);
            db.SaveChanges();

            Student dbStudent = db.Students.FirstOrDefault(s => s.Username == "11012592");
            Subject dbSubject = db.Subjects.FirstOrDefault(s => s.SubjectNumber == "1");

            _sc.Enrol(dbStudent.Id, dbSubject.Id);

            Assert.AreEqual(subject.SubjectNumber, dbStudent.EnrolledSubjects.FirstOrDefault(s => s.SubjectNumber == "1").SubjectNumber);
            Assert.AreEqual(subject.Name, dbStudent.EnrolledSubjects.FirstOrDefault(s => s.SubjectNumber == "1").Name);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Add a subject to student's enrolledSubject list
 /// </summary>
 /// <param name="subject"></param>
 public void EnrolSubject(Subject subject)
 {
     EnrolledSubjects.Add(subject);
 }
        public ActionResult Edit(Subject subject, List<int> PrerequisiteList, int id = 0)
        {
            List<Subject> SelectedPrerequisites = new List<Subject>();

            if (ModelState.IsValid)
            {
                db.Entry(subject).State = EntityState.Modified;
                db.Entry(subject).Collection(m => m.Prerequisites).Load();
                subject.Prerequisites.Clear();
                if (PrerequisiteList != null && PrerequisiteList.Count > 0)
                {
                    //Get a list of subject where the id is contained within the list of selectedPrerequisites
                    SelectedPrerequisites = db.Subjects.Where(m => PrerequisiteList.Contains(m.Id)).ToList();
                    SelectedPrerequisites.ForEach(m => subject.Prerequisites.Add(m));
                }
                try
                {
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var entry = ex.Entries.Single();
                    var databaseValues = (Subject)entry.GetDatabaseValues().ToObject();
                    var clientValues = (Subject)entry.Entity;

                    if (databaseValues.Active == false)
                    {
                        return RedirectToAction("Index");
                    }

                    /* Concurrency checking */
                    ModelState.AddModelError(string.Empty, "The record you attempted to edit "
                    + "was modified by another user after you got the original value. The "
                    + "edit operation was canceled and the current values in the database "
                    + "have been displayed. If you still want to edit this record, click "
                    + "the Save button again. Otherwise click the Back to List hyperlink.");

                    if (databaseValues.SubjectNumber != clientValues.SubjectNumber)
                    {
                        ModelState.AddModelError("SubjectNumber", "Current Subject Number: " + databaseValues.SubjectNumber);
                    }

                    if (databaseValues.Name != clientValues.Name)
                    {
                        ModelState.AddModelError("Name", "Current Value: " + databaseValues.Name);
                    }

                    if (databaseValues.MaxEnrolment != clientValues.MaxEnrolment)
                    {
                        ModelState.AddModelError("MaxEnrolment", "Current Max Enrolment: " + databaseValues.Name);
                    }
                    /* end concurrency checking */
                }
            }
            List<Subject> NonPrerequisites = db.Subjects.ToList().Except(SelectedPrerequisites).ToList();
            NonPrerequisites.Remove(db.Subjects.Find(id));
            ViewBag.SelectedPrerequisites = new SelectList(SelectedPrerequisites, "Id", "Name");
            ViewBag.NonPrerequisites = new SelectList(NonPrerequisites, "Id", "Name");
            return View(subject);
        }
 public ActionResult DeleteConfirmed(Subject subject)
 {
     try
     {
         subject.Active = false;
         db.Entry(subject).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     catch (DbUpdateConcurrencyException)
     {
         return RedirectToAction("Delete",
             new System.Web.Routing.RouteValueDictionary { { "concurrencyError", true } });
     }
 }