public async Task <IActionResult> Edit(int id, [Bind("Id,StudentID,FirstName,LastName,StudentEmail")] EligibleStudent eligibleStudent) { if (id != eligibleStudent.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(eligibleStudent); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EligibleStudentExists(eligibleStudent.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(eligibleStudent)); }
/// <summary> /// Parse incoming students from CSV file. /// </summary> /// <returns>Parsed data with successful results in form of 'eligible student'</returns> public ParsedCsvData <EligibleStudent> ParseStudents() { using (var reader = new StreamReader(FileData)) { //SETUP ParsedCsvData <EligibleStudent> parsedStudents = new ParsedCsvData <EligibleStudent>(); int currentLineNumber = 0; while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(Delimiter); try { //Validate student number by attempting parse var studentNumber = Int32.Parse(values[0]); //Validate student has proper Mohawk email var studentEmail = values[3].ToLower(); var emailValidated = Regex.Match(studentEmail, patterns["email"]); //First&Last name are not check vs email; some students may have different names the one in their email //However, names should not include numbers var firstNameValidated = Regex.Match(values[1], patterns["name"]); var lastNameValidated = Regex.Match(values[2], patterns["name"]); //Student number should be < 9 digits and > 0, other fields must match regex. if (studentNumber < 1000000000 && studentNumber > 0 && emailValidated.Success && firstNameValidated.Success && lastNameValidated.Success) { var eligible = new EligibleStudent() { StudentID = studentNumber, FirstName = values[1], LastName = values[2], StudentEmail = values[3] }; parsedStudents.ValidList.Add(currentLineNumber.ToString(), eligible); } else { throw new Exception("Invalid format."); } } catch (Exception e) { parsedStudents.InvalidList.Add(currentLineNumber.ToString(), line); } currentLineNumber++; } return(parsedStudents); } }
public async Task <IActionResult> Create([Bind("Id,StudentID,FirstName,LastName,StudentEmail")] EligibleStudent eligibleStudent) { if (ModelState.IsValid) { _context.Add(eligibleStudent); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(eligibleStudent)); }
/// <summary> /// Parse incoming students from CSV file. /// </summary> public ParsedCsvData <EligibleStudent> ParseStudents() { using (var reader = new StreamReader(FileData)) { //SETUP ParsedCsvData <EligibleStudent> parsedStudents = new ParsedCsvData <EligibleStudent>(); int currentLineNumber = 0; while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(Delimiter); try { //Validate student number by attempting parse var studentNumber = Int32.Parse(values[0]); //Validate student has proper Mohawk email var studentEmail = values[3].ToLower(); var emailPattern = "[a-z]*.[a-z0-9]*\\@mohawkcollege.ca"; var emailValidated = Regex.Match(studentEmail, emailPattern); //First&Last name are not validated; some students may have different names the one in their email //Student number should be < 9 digits, email must pass regex if (studentNumber < 1000000000 && emailValidated.Success) { var eligible = new EligibleStudent() { StudentID = studentNumber, FirstName = values[1], LastName = values[2], StudentEmail = values[3] }; parsedStudents.ValidList.Add(eligible); } } catch (Exception e) { parsedStudents.InvalidList.Add(new InvalidCsvEntry(currentLineNumber, line)); } currentLineNumber++; } return(parsedStudents); } }
public async Task <IActionResult> Edit(int id, [Bind("Id,StudentID,FirstName,LastName,StudentEmail")] EligibleStudent eligibleStudent) { if (id != eligibleStudent.Id) { return(NotFound()); } //Check if given Id is a duplicate if (_context.EligibleStudent.Any(e => e.StudentID == eligibleStudent.StudentID && e.Id != id)) { ModelState.AddModelError("StudentID", "Student ID already exists"); } //Check if given email is a duplicate if (_context.EligibleStudent.Any(e => e.StudentEmail == eligibleStudent.StudentEmail && e.Id != id)) { ModelState.AddModelError("StudentEmail", "Student email already exists"); } if (ModelState.IsValid) { try { _context.Update(eligibleStudent); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EligibleStudentExists(eligibleStudent.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(eligibleStudent)); }
public async Task <IActionResult> Create([Bind("Id,StudentID,FirstName,LastName,StudentEmail")] EligibleStudent eligibleStudent) { //Check if given Id is a duplicate if (_context.EligibleStudent.Any(e => e.StudentID == eligibleStudent.StudentID)) { ModelState.AddModelError("StudentID", "Student ID already exists"); } //Check if given email is a duplicate if (_context.EligibleStudent.Any(e => e.StudentEmail == eligibleStudent.StudentEmail)) { ModelState.AddModelError("StudentEmail", "Student email already exists"); } if (ModelState.IsValid) { _context.Add(eligibleStudent); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(eligibleStudent)); }
//Seed the database with users, roles and assign users to roles. To call this method, use https://localhost:44350/Account/SeedUserData public async Task <IActionResult> SeedUserData() { //Variable to hold the status of our identity operations IdentityResult result; //Create 2 new roles (Student, Admin) if (await _roleManager.RoleExistsAsync("Student") == false) { result = await _roleManager.CreateAsync(new IdentityRole("Student")); if (!result.Succeeded) { return(View("Error", new ErrorViewModel { RequestId = "Failed to add Student role" })); } } if (await _roleManager.RoleExistsAsync("Admin") == false) { result = await _roleManager.CreateAsync(new IdentityRole("Admin")); if (!result.Succeeded) { return(View("Error", new ErrorViewModel { RequestId = "Failed to add Admin role" })); } } //Create a list of students List <ApplicationUser> StudentList = new List <ApplicationUser>(); //Sample student user StudentList.Add(new ApplicationUser { StudentId = 000777777, Email = "*****@*****.**", UserName = "******", FirstName = "Student", LastName = "One", ActiveStatus = "Active", Eligible = "Yes", EmailConfirmed = true, Role = "Student" }); EligibleStudent studentAccount = new EligibleStudent() { StudentID = 000777777, StudentEmail = "*****@*****.**", FirstName = "Student", LastName = "One", }; _context.EligibleStudent.Add(studentAccount); foreach (ApplicationUser student in StudentList) { //Create the new user with password "Mohawk1!" result = await _userManager.CreateAsync(student, "Mohawk1!"); if (!result.Succeeded) { return(View("Error", new ErrorViewModel { RequestId = "Failed to add new student user" })); } //Assign the new user to the student role result = await _userManager.AddToRoleAsync(student, "Student"); if (!result.Succeeded) { return(View("Error", new ErrorViewModel { RequestId = "Failed to assign student role" })); } } //Create a list of admins List <ApplicationUser> AdminsList = new List <ApplicationUser>(); //Sample admin user AdminsList.Add(new ApplicationUser { StudentId = 000101010, Email = "*****@*****.**", UserName = "******", FirstName = "Admin", LastName = "One", ActiveStatus = "Active", Eligible = "Yes", EmailConfirmed = true, Role = "Admin" }); EligibleStudent adminAccount = new EligibleStudent() { StudentID = 000101010, StudentEmail = "*****@*****.**", FirstName = "Admin", LastName = "One", }; _context.EligibleStudent.Add(adminAccount); foreach (ApplicationUser admin in AdminsList) { //Create the new user with password "Mohawk1!" result = await _userManager.CreateAsync(admin, "Mohawk1!"); if (!result.Succeeded) { return(View("Error", new ErrorViewModel { RequestId = "Failed to add new admin user" })); } //Assign the new user to the admin role result = await _userManager.AddToRoleAsync(admin, "Admin"); if (!result.Succeeded) { return(View("Error", new ErrorViewModel { RequestId = "Failed to assign admin role" })); } } //If we are here, everything executed according to plan, so we will show a success message return(Content("Users setup completed.\n\n" + "Admin Account:\n" + "Username = [email protected]\n" + "Password = Mohawk1!\n\n" + "Student Account:\n" + "Username = [email protected]\n" + "Password = Mohawk1!\n")); }