public IActionResult AddModule([Bind("StudentId,ModuleId,Grade")] ModuleViewModel m)
        {
            if (ModelState.IsValid)
            {
                var sm = svc.AddStudentToModule(m.StudentId, m.ModuleId);
                svc.UpdateStudentModuleGrade(m.StudentId, m.ModuleId, m.Grade);
                svc.RecalculateStudentGrade(m.StudentId);
                //if (sm != null)
                //{
                //    svc.UpdateStudentModuleGrade(m.StudentId, m.ModuleId, m.Grade);
                //    svc.RecalculateStudentGrade(m.StudentId);
                //}
                //else
                //{
                //    ModelState.AddModelError("ModuleId", "Module is already taken by Student");
                //    m.Modules = new SelectList(svc.GetModules(),"Id","Title");
                //    return View(m);
                //}
                return(RedirectToAction(nameof(Details), new { Id = m.StudentId }));
            }

            m.Modules = new SelectList(svc.GetModules(), "Id", "Title");

            // redisplay the form for editing
            return(View(m));
        }
        public IActionResult CreateModule([Bind("StudentId, ModuleId, Mark")] StudentModuleViewModel sm)
        {
            if (ModelState.IsValid)
            {
                svc.AddStudentToModule(sm.StudentId, sm.ModuleId);
                svc.UpdateStudentModuleMark(sm.StudentId, sm.ModuleId, sm.Mark);
                return(RedirectToAction(nameof(Details), new { Id = sm.StudentId }));
            }
            // redisplay the form for editing
            // note - we must re-create the selectlist and update view model Modules property
            //        this is because the form does not retain the select list values when posted to server
            var modules = svc.GetAvailableModulesForStudent(sm.StudentId);

            sm.Modules = new SelectList(modules, "Id", "Title");
            return(View(sm));
        }
Пример #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("\n========== Running Tests ==============\n");

            // create the service and initialise to ensure the database is empty
            var svc = new StudentService();

            svc.Initialise();

            // Create some students
            var s1 = svc.AddStudent("Homer", "Computing", 44, 45.0);
            var s2 = svc.AddStudent("Marge", "Engineering", 40, 68.0);
            var s3 = svc.AddStudent("Bart", "Sleeping", 16, 39.0);
            var s4 = svc.AddStudent("Lisa", "Computing", 13, 86.0);

            // create some modules
            var m1 = svc.AddModule("Programming");
            var m2 = svc.AddModule("Maths");
            var m3 = svc.AddModule("English");

            // Add ticket for Homer
            var t1 = svc.CreateTicket(s1.Id, "Bart you little ...");

            // Add ticket for Bart
            var t2 = svc.CreateTicket(s3.Id, "Go to Skinners office");

            // Homer is taking programming
            svc.AddStudentToModule(s1.Id, m1.Id);

            // Marge is taking maths
            svc.AddStudentToModule(s2.Id, m2.Id);

            // Bart is taking English
            svc.AddStudentToModule(s3.Id, m3.Id);

            // Lisa is taking Programming Maths and English
            svc.AddStudentToModule(s4.Id, m1.Id);
            svc.AddStudentToModule(s4.Id, m2.Id);
            svc.AddStudentToModule(s4.Id, m3.Id);


            // Call some service methods to test their operation
            // use the test method defined below to print result of test

            // 1. call GetAllStudents and verify it returns 4 students
            Test(svc.GetAllStudents().Count == 4, "GetAllStudents()");

            // 2. call GetStudent to return Homer and verify that he has 1
            // ticket and his course is Computing
            var homer = svc.GetStudent(s1.Id);

            Test((homer.ActiveTicketCount == 1) &&
                 (homer.Course == "Computing"), "GetStudent()");

            // 3. Verify that Lisa is taking 3 modules and has no tickets
            var lisa = svc.GetStudent(s4.Id);

            Test(lisa.StudentModules.Count == 3 && lisa.ActiveTicketCount == 0
                 , "GetStudentLisa"
                 );

            // 4. Using the CloseTicket service method, close Homers ticket and then verify that its closed
            var t = svc.CloseTicket(t1.Id);

            Test(t.Active == false, "CloseTicket()");

            var fob = 4.ToString();

            Console.WriteLine(fob);
        }