public ActionResult CreateEditCourse(CoursesCreateVM model) { if (ModelState.IsValid) { Course course; if (model.Id == 0) { course = new Course(); } else { course = unitOfWork.CourseRepository.GetById(model.Id); } if (unitOfWork.CourseRepository.CheckIfCourseCodeExists(model.CourseCode)) { if (model.CourseCode != course.Code) { model.Subjects = unitOfWork.SubjectRepository.GetAll(true); TempData.FlashMessage("The course code you are trying to create is already in use", null, FlashMessageTypeEnum.Red); return(View(model)); } } int oldCourseCode = course.Code; course.Name = model.Name; model.Subjects = unitOfWork.SubjectRepository.GetAll(); List <Subject> subjects = new List <Subject>(); foreach (var s in model.Subjects) { if ((Request.Form[s.Id.ToString()] != null) && (Request.Form[s.Id.ToString()] == "on")) { subjects.Add(s); } } if (model.Id == 0) { course.Code = model.CourseCode; unitOfWork.CourseRepository.Insert(course); TempData.FlashMessage("Course Created"); } else { if (course.Code != model.CourseCode) { course.Code = model.CourseCode; List <Student> students = new List <Student>(); foreach (var s in unitOfWork.StudentRepository.GetAll(null, false)) { if (s.FacultyNumber.Substring(4, 2) == oldCourseCode.ToString("00")) { students.Add(s); } } GenerateFacultyNumber.ResetFacultyNumbersbyCourseId(course.Id, students); unitOfWork.StudentRepository.Save(); foreach (var s in students) { s.FacultyNumber = GenerateFacultyNumber.Generate(course, unitOfWork); } } course.Code = model.CourseCode; unitOfWork.CourseRepository.Update(course); TempData.FlashMessage("Course Edited"); } unitOfWork.Save(); unitOfWork.CoursesSubjectsRepository.UpdateTable(course, subjects); return(RedirectToAction("ManageCourses", "Admin")); } model.Subjects = unitOfWork.SubjectRepository.GetAll(true); return(View(model)); }
public ActionResult CreateEditStudent(StudentsCreateAccountVM model) { if (ModelState.IsValid) { Student student; if (model.Id == 0) { student = new Student(); } else { student = unitOfWork.StudentRepository.GetById(model.Id); } student.FirstName = model.FirstName; student.LastName = model.LastName; student.Email = model.Email; student.IsActive = true; if (model.Id == 0) { string password = Path.GetRandomFileName().Replace(".", "").Substring(0, 8); var passPhrase = PasswordHasher.Hash(password); student.Hash = passPhrase.Hash; student.Salt = passPhrase.Salt; student.IsConfirmed = false; student.CourseId = model.CourseId; Course course = unitOfWork.CourseRepository.GetById(model.CourseId); student.FacultyNumber = GenerateFacultyNumber.Generate(course, unitOfWork); unitOfWork.StudentRepository.Insert(student); unitOfWork.Save(); TempData.FlashMessage("Student has been added. Faculty number: " + student.FacultyNumber); #region Send password to mail MailMessage message = new MailMessage(); message.IsBodyHtml = true; message.Sender = new MailAddress("*****@*****.**"); message.To.Add(model.Email); message.Subject = "Welcome to the University System"; message.From = new MailAddress("*****@*****.**"); StringBuilder msgBody = new StringBuilder(); msgBody.AppendLine(String.Format("<h3>Hello, {0} {1}</h3>", student.FirstName, student.LastName)); msgBody.AppendLine("<h4>Welcome to our University System!</h4>"); msgBody.AppendLine(String.Format("<p>You must confirm your account: <a href='{0}'>Confirm</a></p>", Url.Action("ConfirmAccount", "Student", new { id = student.Id }, Request.Url.Scheme))); msgBody.AppendLine(String.Format("<p>Use this password to confirm: <strong>{0}</string></p>", password)); message.Body = msgBody.ToString(); SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587); smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; #region Private smtp.Credentials = new System.Net.NetworkCredential("*****@*****.**", "programistaphonebook"); #endregion smtp.Send(message); #endregion } else { if (student.CourseId != model.CourseId) { Course course = unitOfWork.CourseRepository.GetById(model.CourseId); student.CourseId = course.Id; student.FacultyNumber = GenerateFacultyNumber.Generate(course, unitOfWork); } unitOfWork.StudentRepository.Update(student); unitOfWork.Save(); TempData.FlashMessage("Student has been edited. Faculty number: " + student.FacultyNumber); } return(RedirectToAction("ManageStudents", "Admin")); } return(View(model)); }