private string ValidationForTeacher(Teacher teacher)
        {
            string message = "";

            if (teacher.TeacherName == "")
            {
                message += "Teacher Name is not given";
            }

            if (teacher.TeacherAddress == "")
            {
                message += "Teacher Address is not given";
            }

            if (teacher.TeacherEmail == "")
            {
                message += "Teacher email is not given";
            }
            else
            {
                if (!IsMailAddressValid(teacher.TeacherEmail))
                {
                    message += "Invalid email address given";
                }
                else
                {
                    // there is case, story says email must be unique
                    // but it is not said that unique within the system or not

                    if (db.Teachers.Count(t => t.TeacherEmail == teacher.TeacherEmail) != 0)
                    {
                        message += "This email is already exists";
                    }
                }
            }

            if (teacher.TeacherContactNo == "")
            {
                message += "Contact No. not given";
            }

            if (teacher.DesignationId < 0)
            {
                message += "Invalid Designation, choose correct designation";
            }

            if (teacher.DepartmentId < 0)
            {
                message += "Invalid Department, choose correct department";
            }

            if (teacher.TeacherCreditToBeTaken < 0)
            {
                message += "Credit to be taken must be non-negative number";
            }

            return message;
        }
        public ActionResult Create(Teacher teacher)
        {
            string message = ValidationForTeacher(teacher);
            if (message != "")
            {
                ViewBag.DesignationId = new SelectList(db.Designations, "DesignationId", "DesignationName", teacher.DesignationId);
                ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentCode", teacher.DepartmentId);
                ViewBag.Message = message;
                return View(teacher);
            }

            teacher.TeacherRemainingCredit = teacher.TeacherCreditToBeTaken;
            if (ModelState.IsValid)
            {
                db.Teachers.Add(teacher);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.DesignationId = new SelectList(db.Designations, "DesignationId", "DesignationName", teacher.DesignationId);
            ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentCode", teacher.DepartmentId);
            return View(teacher);
        }
Esempio n. 3
0
        public ActionResult CourseAssign(Department aDepartment, Course aCourse, Teacher aTeacher)
        {
            string message = ValidationForCourseAssign(aDepartment, aCourse, aTeacher);
            if (message != "")
            {
                ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentName");
                ViewBag.TeacherId = new SelectList("", "", "TeacherName");
                ViewBag.CourseId = new SelectList("", "", "CourseName");

                ViewBag.Message = message;
                return View();
            }

            aTeacher = db.Teachers.Find(aTeacher.TeacherId);
            aCourse = db.Courses.Find(aCourse.CourseId);

            // assiging teacher to this course
            aCourse.Teacher = aTeacher;

            // update remaining credit for teacher
            aTeacher.TeacherRemainingCredit -= aCourse.CourseCredit;

            //save data to database
            if (!ModelState.IsValid)
            {
                db.Entry(aCourse).State = EntityState.Modified;
                db.SaveChanges();

                db.Entry(aTeacher).State = EntityState.Modified;
                db.SaveChanges();

                return RedirectToAction("Index");
            }

            return View();
        }
Esempio n. 4
0
        private string ValidationForCourseAssign(Department aDepartment, Course aCourse, Teacher aTeacher)
        {
            string message = "";

            if (aDepartment.DepartmentId == 0 || aCourse.CourseId == 0 || aTeacher.TeacherId == 0)
            {
                message += "Incomplete form submit, any field is not yet set ";
            }

            // is teacher and course is not from the same department

            aDepartment = db.Departments.Find(aDepartment.DepartmentId);
            aTeacher = db.Teachers.Find(aTeacher.TeacherId);
            aCourse = db.Courses.Find(aCourse.CourseId);

            if (aDepartment.DepartmentId != aTeacher.DepartmentId || aCourse.DepartmentId != aTeacher.DepartmentId)
            {
                ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "DepartmentName");
                ViewBag.TeacherId = new SelectList(db.Teachers, "TeacherId", "TeacherName");
                ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "CourseName");

                if (aDepartment.DepartmentId != aCourse.DepartmentId)
                {
                    message += "Your Course is not from the selected department";

                }
                else if (aCourse.DepartmentId != aTeacher.DepartmentId)
                {
                    message += "Your Teacher is not from the selected department";
                }

            }

            // if course is already assigned

            if (aCourse.Teacher != null)
            {
                message += "Course Already Assigned";
            }

            return message;
        }