Exemplo n.º 1
0
        public ActionResult SaveNewSubject(TeacherSubjectViewModel viewModel)
        {
            subjectService = new SubjectService();

            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index", "Subject"));
            }

            // check if teacher who creates the suubject has already a subject allocated
            if (subjectService.CheckIfTeacherHasSubject(viewModel.Subject.TeacherId))
            {
                // find if there are non-allocated teachers and give the subject to the 1st of them
                List <Teacher> nonAllocatedTeachers = subjectService.GetNonAllocatedTeachers();

                // if there's no non-allocated teachers available redirect to...
                if (nonAllocatedTeachers.Count() == 0)
                {
                    return(View("NoTeachersAvailable"));
                }

                // if there's at leaste one, give the subject to the first of them
                Teacher chosenTeacher = nonAllocatedTeachers.ToArray()[0];

                // create the subject and save it
                subjectService.CreateNewSubjectAndSaveIt(viewModel, chosenTeacher);

                return(RedirectToAction("Index"));
            }

            subjectService.CreateNewSubjectAndSaveIt(viewModel);

            return(RedirectToAction("Index"));
        }