public ActionResult Register(TeacherViewModel model) { _db = new KnowledgeChannelEntities(); bool emailExists = _db.Teachers.Where(x => x.Email == model.Teacher.Email).ToList().Count > 0; if (emailExists) { ModelState.AddModelError("EmailExists", "Email is already taken"); } if (!model.Teacher.Email.IsEmail()) { ModelState.AddModelError("EmailWrongFormat", "Email is not in correct format"); } if (!model.Teacher.ContactNumber.IsNumeric()) { ModelState.AddModelError("ContactNotNumeric", "Not a valid contact number"); } if (model.Teacher.Gender == null || model.Teacher.Email == null || model.Teacher.LastName == null || model.Teacher.FirstName == null || model.Teacher.Password == null){ ModelState.AddModelError("InputRequired", "Please fill up the required fields"); } if (model.Teacher.Password != model.Teacher.ConfirmPassword) ModelState.AddModelError("PasswordsDoNotMatch", "Passwords do not match"); if (model.Teacher.Password.Length < 6) ModelState.AddModelError("PasswordsDoNotMatch", "Password must at least have 6 characters"); if (model.Teacher.SchoolID == 0) ModelState.AddModelError("NoSchoolSelected", "School not selected correctly. Please retry inputting your school"); if (!ModelState.IsValid){ return View(new TeacherViewModel()); } RegisterTeacher(model); return RedirectToAction("SignIn"); }
private void RegisterTeacher(TeacherViewModel model) { var teacher = Application.Db.Teachers.CreateObject(); teacher.BirthDate = model.Teacher.BirthDate; teacher.Gender = model.Teacher.Gender; teacher.ContactNo = model.Teacher.ContactNumber; teacher.Email = model.Teacher.Email; teacher.FirstName = model.Teacher.FirstName; teacher.LastName = model.Teacher.LastName; teacher.Password = Encrypt.ComputeHash(model.Teacher.Password, "SHA512", null); teacher.SchoolID = model.Teacher.SchoolID; teacher.created_at = DateTime.Now; Application.Db.AddToTeachers(teacher); Application.Db.SaveChanges(); }