public ActionResult UpdateStudent(MultipleModel.StudentIndexModelVM request)
        {
            var messageList = new List<string>();

            if (ModelState.IsValid)
            {
                using (var db = new ElectionDbContext())
                {
                    var getStudent = db.Students.Find(request.UpdateUser.StudentId);
                    if (getStudent != null)
                    {
                        if(!(db.Students.Where(s => s.StudentNumber == request.UpdateUser.StudentNumber).Any()) || (getStudent.StudentNumber == request.UpdateUser.StudentNumber))
                        {
                            getStudent.StudentNumber = request.UpdateUser.StudentNumber;
                            getStudent.LastName = request.UpdateUser.LastName;
                            getStudent.FirstName = request.UpdateUser.FirstName;
                            getStudent.MiddleInitial = request.UpdateUser.MiddleInitial;
                            getStudent.Gender = request.UpdateUser.Gender;
                            getStudent.UpdatedAt = DateTime.Now;

                            db.Entry(getStudent).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();

                            request.Error = false;
                            string message = "You have successfully updated student (" + getStudent.StudentNumber + ")";
                            messageList.Add(message);
                            request.Message = messageList;
                            TempData["StudentIndexTD"] = request;
                            return RedirectToAction("StudentIndex", new { id = request.SectionId });
                        }
                        else if (db.Students.Where(s => s.StudentNumber == request.UpdateUser.StudentNumber).Any())
                        {
                            request.Error = true;
                            string message = "The student number "+ request.UpdateUser.StudentNumber+" is already existing!!";
                            messageList.Add(message);
                            request.Message = messageList;
                            TempData["StudentIndexTD"] = request;
                            return RedirectToAction("StudentIndex", new { id = request.SectionId });
                        }
                    }
                    else if (getStudent == null)
                    {
                        request.Error = true;
                        string message = "The selected student does not exist!!";
                        messageList.Add(message);
                        request.Message = messageList;
                        TempData["StudentIndexTD"] = request;
                        return RedirectToAction("StudentIndex", new { id = request.SectionId });
                    }
                }
            }
            request.Error = true;
            request.Message = CustomClass.CustomValidationMessage.GetErrorList(ViewData.ModelState);
            TempData["StudentIndexTD"] = request;
            return RedirectToAction("StudentIndex", new { id = request.SectionId });
        }
        public ActionResult CreateCollege(MultipleModel.CollegeModelVM request)
        {
            var messageList = new List<string>();
            if (ModelState.IsValid)
            {
                using (var db = new ElectionDbContext())
                {
                    if(db.Colleges.Where(c=>c.College == request.CreateCollege.CollegeName).Any())
                    {
                        string message = request.CreateCollege.CollegeName + " is already existing!!";
                        messageList.Add(message);
                        request.Message = messageList;
                        request.Error = true;
                        TempData["GroupsIndexTD"] = request;
                        return RedirectToAction("GroupsIndex", "Admin");
                    }
                    else if(!(db.Colleges.Where(c => c.College == request.CreateCollege.CollegeName).Any()))
                    {
                        var newCollege = db.Colleges.Create();
                        newCollege.College = request.CreateCollege.CollegeName;
                        newCollege.CreatedAt = DateTime.Now;

                        db.Colleges.Add(newCollege);
                        db.SaveChanges();

                        request.Error = false;
                        string message = "You have successfully added " + newCollege.College + "";
                        messageList.Add(message);
                        request.Message = messageList;
                        TempData["GroupsIndexTD"] = request.Error;
                        TempData["GroupsIndexTD"] = request.Message;

                        return RedirectToAction("GroupsIndex", "Admin");
                    }
                }
            }
            request.Error = true;
            request.Message = CustomClass.CustomValidationMessage.GetErrorList(ViewData.ModelState);
            TempData["GroupsIndexTD"] = request;
            return RedirectToAction("GroupsIndex", "Admin");
        }
        public ActionResult UpdateCollege(MultipleModel.CollegeModelVM request)
        {
            var messageList = new List<string>();
            if (ModelState.IsValid)
            {
                using (var db = new ElectionDbContext())
                {
                    var colleges = db.Colleges.Find(request.UpdateCollege.CollegeId);

                    if (colleges != null)
                    {
                        if(!(db.Colleges.Where(c=>c.College == request.UpdateCollege.CollegeName).Any()))
                        {
                            colleges.College = request.UpdateCollege.CollegeName;
                            colleges.UpdatedAt = DateTime.Now;

                            db.Entry(colleges).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();

                            request.Error = false;
                            string message = "You have successfully updated " + colleges.College;
                            messageList.Add(message);
                            request.Message = messageList;
                            TempData["GroupsIndexTD"] = request.Error;
                            TempData["GroupsIndexTD"] = request.Message;

                            return RedirectToAction("GroupsIndex", "Admin");
                        }
                        else if(db.Colleges.Where(c => c.College == request.UpdateCollege.CollegeName).Any())
                        {
                            request.Error = true;
                            string message = request.UpdateCollege.CollegeName+" is already existing!!";
                            messageList.Add(message);
                            request.Message = messageList;
                            TempData["GroupsIndexTD"] = request;
                            return RedirectToAction("GroupsIndex", "Admin");
                        }

                    }
                    else if (colleges == null)
                    {
                        request.Error = true;
                        string message = "The selected college does not exist!!";
                        messageList.Add(message);
                        request.Message = messageList;
                        TempData["GroupsIndexTD"] = request;
                        return RedirectToAction("GroupsIndex", "Admin");
                    }
                }

            }
            request.Error = true;
            request.Message = CustomClass.CustomValidationMessage.GetErrorList(ViewData.ModelState);
            TempData["GroupsIndexTD"] = request;
            return RedirectToAction("GroupsIndex", "Admin");
        }
        public ActionResult UpdateSection(MultipleModel.SectionModelVM request)
        {
            var messageList = new List<string>();
            if (ModelState.IsValid)
            {
                using (var db = new ElectionDbContext())
                {
                    var section = db.Sections.Find(request.UpdateSection.SectionId);

                    if (section != null)
                    {
                        if (!(db.Sections.Where(m => m.Section == request.UpdateSection.SectionName).Any()))
                        {
                            section.Section = request.UpdateSection.SectionName;
                            section.UpdatedAt = DateTime.Now;

                            db.Entry(section).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();

                            request.Error = false;
                            string message = "You have successfully updated " + section.Section;
                            messageList.Add(message);
                            request.Message = messageList;
                            TempData["SectionIndexTD"] = request;
                            return RedirectToAction("SectionIndex", new { id = request.MajorId });
                        }
                        else if (db.Sections.Where(m => m.Section == request.UpdateSection.SectionName).Any())
                        {
                            request.Error = true;
                            string message = request.UpdateSection.SectionName + " is already existing!!";
                            messageList.Add(message);
                            request.Message = messageList;
                            TempData["SectionIndexTD"] = request;
                            return RedirectToAction("SectionIndex", new { id = request.MajorId });
                        }
                    }
                    else if (section == null)
                    {
                        request.Error = true;
                        string message = "The selected section does not exist!!";
                        messageList.Add(message);
                        request.Message = messageList;
                        TempData["SectionIndexTD"] = request;
                        return RedirectToAction("SectionIndex", new { id = request.MajorId });
                    }
                }

            }
            request.Error = true;
            request.Message = CustomClass.CustomValidationMessage.GetErrorList(ViewData.ModelState);
            TempData["SectionIndexTD"] = request;
            return RedirectToAction("SectionIndex", new { id = request.MajorId });
        }
        public ActionResult DeleteSection(MultipleModel.SectionModelVM request)
        {
            if (ModelState.IsValid)
            {
                using (var db = new ElectionDbContext())
                {
                    var section = db.Sections.Find(request.DeleteSection.SectionId);

                    if (section != null)
                    {
                        section.Deleted = true;
                        section.UpdatedAt = DateTime.Now;
                        db.SaveChanges();

                        request.Error = false;
                        var messageList = new List<string>();
                        string message = "You have successfully deleted " + section.Section + "";
                        messageList.Add(message);
                        request.Message = messageList;
                        TempData["SectionIndexTD"] = request;

                        return RedirectToAction("SectionIndex", new { id = request.MajorId });
                    }
                    else if (section == null)
                    {
                        request.Error = true;
                        var messageList = new List<string>();
                        string message = "The section selected does not exist!";
                        messageList.Add(message);
                        request.Message = messageList;
                        TempData["SectionIndexTD"] = request;

                        return RedirectToAction("SectionIndex", new { id = request.MajorId });
                    }
                }
            }
            request.Error = true;
            request.Message = CustomClass.CustomValidationMessage.GetErrorList(ViewData.ModelState);
            TempData["SectionIndexTD"] = request;
            return RedirectToAction("SectionIndex", new { id = request.MajorId });
        }
        public ActionResult DeleteStudent(MultipleModel.StudentIndexModelVM request)
        {
            var messageList = new List<string>();

            if (ModelState.IsValid)
            {
                using (var db = new ElectionDbContext())
                {
                    var getStudent = db.Students.Find(request.DeleteStudent.StudentId);
                    if (getStudent != null)
                    {
                        getStudent.Deleted = true;
                        getStudent.UpdatedAt = DateTime.Now;

                        db.Entry(getStudent).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();

                        request.Error = false;
                        string message = "You have successfully deleted student (" + getStudent.StudentNumber + ")";
                        messageList.Add(message);
                        request.Message = messageList;
                        TempData["StudentIndexTD"] = request;
                        return RedirectToAction("StudentIndex", new { id = request.SectionId });
                    }
                    else if (getStudent == null)
                    {
                        request.Error = true;
                        string message = "The selected student does not exist!!";
                        messageList.Add(message);
                        request.Message = messageList;
                        TempData["StudentIndexTD"] = request;
                        return RedirectToAction("StudentIndex", new { id = request.SectionId });
                    }
                }
            }
            request.Error = true;
            request.Message = CustomClass.CustomValidationMessage.GetErrorList(ViewData.ModelState);
            TempData["StudentIndexTD"] = request;
            return RedirectToAction("StudentIndex", new { id = request.SectionId });
        }
        public ActionResult DeleteCollege(MultipleModel.CollegeModelVM request)
        {
            if (ModelState.IsValid)
            {
                using (var db = new ElectionDbContext())
                {
                    var college = db.Colleges.Find(request.DeleteCollege.CollegeId);

                    if (college != null)
                    {
                        college.Deleted = true;
                        college.UpdatedAt = DateTime.Now;
                        db.SaveChanges();

                        request.Error = false;
                        var messageList = new List<string>();
                        string message = "You have successfully deleted " + college.College + "";
                        messageList.Add(message);
                        request.Message = messageList;
                        TempData["GroupsIndexTD"] = request;

                        return RedirectToAction("GroupsIndex", "Admin");
                    }
                    else if (college == null)
                    {
                        request.Error = true;
                        var messageList = new List<string>();
                        string message = "The college selected does not exist!";
                        messageList.Add(message);
                        request.Message = messageList;
                        TempData["GroupsIndexTD"] = request;

                        return RedirectToAction("GroupsIndex", "Admin");
                    }
                }
            }
            request.Error = true;
            request.Message = CustomClass.CustomValidationMessage.GetErrorList(ViewData.ModelState);
            TempData["GroupsIndexTD"] = request;
            return RedirectToAction("GroupsIndex", "Admin");
        }
        public ActionResult CreateStudent(MultipleModel.StudentIndexModelVM request)
        {
            var messageList = new List<string>();
            if(ModelState.IsValid)
            {
                using (var db = new ElectionDbContext())
                {
                    if(!(db.Students.Where(s => s.StudentNumber == request.CreateStudent.StudentNumber).Any()))
                    {
                        var newStudent = db.Students.Create();
                        newStudent.StudentNumber = request.CreateStudent.StudentNumber;
                        newStudent.LastName = request.CreateStudent.LastName;
                        newStudent.FirstName = request.CreateStudent.FirstName;
                        newStudent.MiddleInitial = request.CreateStudent.MiddleInitial;
                        newStudent.Gender = request.CreateStudent.Gender;
                        newStudent.SectionId = request.CreateStudent.SectionId;
                        newStudent.CreatedAt = DateTime.Now;

                        db.Students.Add(newStudent);
                        db.SaveChanges();

                        request.Error = false;
                        string message = "You have successfully added "+ request.CreateStudent.StudentNumber;
                        messageList.Add(message);
                        request.Message = messageList;
                        TempData["StudentIndexTD"] = request;
                        return RedirectToAction("StudentIndex", new { id = request.CreateStudent.SectionId });
                    }
                    else if (db.Students.Where(s => s.StudentNumber == request.CreateStudent.StudentNumber).Any())
                    {
                        request.Error = true;
                        string message = request.CreateStudent.StudentNumber + " is already existing!!";
                        messageList.Add(message);
                        request.Message = messageList;
                        TempData["StudentIndexTD"] = request;
                        return RedirectToAction("StudentIndex", new { id = request.CreateStudent.SectionId });
                    }
                }
            }
            request.Error = true;
            request.Message = CustomClass.CustomValidationMessage.GetErrorList(ViewData.ModelState);
            TempData["StudentIndexTD"] = request;
            return RedirectToAction("StudentIndex", new { id = request.CreateStudent.SectionId});
        }
        public ActionResult CreateSection(MultipleModel.SectionModelVM request)
        {
            var messageList = new List<string>();
            if (ModelState.IsValid)
            {
                using (var db = new ElectionDbContext())
                {
                    if (!(db.Sections.Where(m => m.Section == request.CreateSection.SectionName).Any()))
                    {
                        var newSection = db.Sections.Create();
                        newSection.MajorId = request.CreateSection.MajorId;
                        newSection.Section = request.CreateSection.SectionName;
                        newSection.CreatedAt = DateTime.Now;

                        db.Sections.Add(newSection);
                        db.SaveChanges();

                        request.Error = false;
                        string message = "You have successfully added " + newSection.Section + "";
                        messageList.Add(message);
                        request.Message = messageList;
                        TempData["SectionIndexTD"] = request.Error;
                        TempData["SectionIndexTD"] = request.Message;

                        return RedirectToAction("SectionIndex", new { id = request.CreateSection.MajorId });
                    }
                    else if (db.Sections.Where(m => m.Section == request.CreateSection.SectionName).Any())
                    {
                        request.Error = true;
                        string message = request.CreateSection.SectionName + " is already existing!!";
                        messageList.Add(message);
                        request.Message = messageList;
                        TempData["SectionIndexTD"] = request;

                        return RedirectToAction("SectionIndex", new { id = request.CreateSection.MajorId });
                    }
                }
            }
            request.Error = true;
            request.Message = CustomClass.CustomValidationMessage.GetErrorList(ViewData.ModelState);
            TempData["SectionIndexTD"] = request;

            return RedirectToAction("SectionIndex", new { id = request.CreateSection.MajorId });
        }
        public ActionResult CreateAccount(MultipleModel.StudentIndexModelVM request)
        {
            var messageList = new List<string>();
            if(ModelState.IsValid)
            {
                using (var db = new ElectionDbContext())
                {
                    var getStudent = db.Students.SingleOrDefault(s=>s.Id == request.CreateUser.StudentId);
                    if(getStudent != null)
                    {
                        if(!( (db.Users.Where(u => u.StudentId == getStudent.Id).Any()) && (db.Users.Where(u=>u.Email == request.CreateUser.Email).Any()) ))
                        {
                            string pin = SimpleCrypto.RandomPassword.Generate(6, SimpleCrypto.PasswordGroup.Lowercase, SimpleCrypto.PasswordGroup.Lowercase, SimpleCrypto.PasswordGroup.Numeric);
                            var crypto = new SimpleCrypto.PBKDF2();
                            var encrypPin = crypto.Compute(pin);

                            var newUser = db.Users.Create();
                            newUser.StudentId = request.CreateUser.StudentId;
                            newUser.Email = request.CreateUser.Email;
                            newUser.Role = request.CreateUser.Role;
                            newUser.Pincode = encrypPin;
                            newUser.PincodeSalt = crypto.Salt;
                            newUser.CreatedAt = DateTime.Now;

                            db.Users.Add(newUser);
                            db.SaveChanges();

                            SMTP smtp = new SMTP();
                            smtp.SendEmal(newUser.Email, pin);

                            request.Error = false;
                            string message = "You have successfully created a user " + newUser.Email;
                            messageList.Add(message);
                            request.Message = messageList;
                            TempData["StudentIndexTD"] = request;
                            return RedirectToAction("StudentIndex", new { id = request.SectionId });
                        }
                        else if ((db.Users.Where(u => u.StudentId == getStudent.Id).Any()) && (db.Users.Where(u => u.Email == request.CreateUser.Email).Any()))
                        {
                            request.Error = true;
                            string message = "Failed to create a user. Email address or Student Id is already existing";
                            messageList.Add(message);
                            request.Message = messageList;
                            TempData["StudentIndexTD"] = request;
                            return RedirectToAction("StudentIndex", new { id = request.SectionId });
                        }
                    }
                    else if (getStudent == null)
                    {
                        request.Error = true;
                        string message = "Failed to create a user. The selected student does not exist";
                        messageList.Add(message);
                        request.Message = messageList;
                        TempData["StudentIndexTD"] = request;
                        return RedirectToAction("StudentIndex", new { id = request.SectionId });
                    }
                }
            }
            request.Error = true;
            request.Message = CustomClass.CustomValidationMessage.GetErrorList(ViewData.ModelState);
            TempData["StudentIndexTD"] = request;
            return RedirectToAction("StudentIndex", new { id = request.SectionId });
        }
        public ActionResult CreateElection(MultipleModel.ElectionIndexVM request)
        {
            var messageList = new List<string>();
            if(ModelState.IsValid)
            {
                using( var db = new ElectionDbContext())
                {
                    if(!(db.Elections.Where(e => e.ElectionName == request.CreateElection.ElectionName).Any()))
                    {
                        var newElection = db.Elections.Create();
                        var startDate = request.CreateElection.StartDate;
                        var startTime = request.CreateElection.StartTime;
                        var endDate = request.CreateElection.EndDate;
                        var endTime = request.CreateElection.EndTime;

                        if (request.CreateElection.GroupId != null)
                        {
                            newElection.GroupId = request.CreateElection.GroupId;
                        }
                        newElection.ElectionName = request.CreateElection.ElectionName;
                        newElection.Id = request.CreateElection.GroupId;
                        newElection.StartDateTime = new DateTime(startDate.Year, startDate.Month, startDate.Day, startTime.Hour, startTime.Minute, startTime.Second);
                        newElection.EndDateTime = new DateTime(endDate.Year, endDate.Month, endDate.Day, endTime.Hour, endTime.Minute, endTime.Second);
                        newElection.CreatedAt = DateTime.Now;

                        db.Elections.Add(newElection);
                        db.SaveChanges();

                        request.Error = false;
                        string message = "You have successfully created " + newElection.ElectionName;
                        messageList.Add(message);
                        request.Message = messageList;
                        TempData["ElectionIndexTD"] = request;
                        return RedirectToAction("ElectionsIndex", "Admin");
                    }
                    else if (db.Elections.Where(e => e.ElectionName == request.CreateElection.ElectionName).Any())
                    {
                        request.Error = true;
                        string message = request.CreateElection.ElectionName + " is already existing!";
                        messageList.Add(message);
                        request.Message = messageList;
                        TempData["ElectionIndexTD"] = request;
                        return RedirectToAction("ElectionsIndex", "Admin");
                    }
                }
            }
            request.Error = true;
            request.Message = CustomClass.CustomValidationMessage.GetErrorList(ViewData.ModelState);
            TempData["ElectionIndexTD"] = request;
            return RedirectToAction("ElectionsIndex", "Admin");
        }