public async Task <IActionResult> Create(CreateAdminViewModel model) { if (ModelState.IsValid) { string uniqueFileName = processFileUpload.UploadImage(model.Image, "uploads"); var role = await roleManager.FindByIdAsync(model.Role.Id); IdentityUser user = new IdentityUser { UserName = passwordGenerator.GenerateUsernameFromEmail(model.EmailAddress), Email = model.EmailAddress, PhoneNumber = model.PhoneNumber, }; IdentityResult result = await userManager.CreateAsync(user, model.Password); if (result.Succeeded) { IdentityResult roleAddedResult = await userManager.AddToRoleAsync(user, role.Name); if (roleAddedResult.Succeeded) { Admin admin = new Admin { Firstname = model.Firstname, Lastname = model.Lastname, ImagePath = uniqueFileName, IdentityUserId = user.Id }; _entityRepository.Insert(admin); _entityRepository.Save(); TempData["created"] = $"Admin { model.Firstname } { model.Lastname }, was created successfully."; return(RedirectToAction("index")); } foreach (var error in roleAddedResult.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } return(View(model)); }
public async Task <IActionResult> AddTeacher(AddTeacherViewModel model) { if (ModelState.IsValid) { IdentityUser user = new IdentityUser { UserName = passwordGenerator.GenerateUsernameFromEmail(model.EmailAddress), Email = model.EmailAddress, PhoneNumber = model.PhoneNumber }; IdentityResult result = await userManager.CreateAsync(user, passwordGenerator.GeneratePassword(15)); if (result.Succeeded) { Teacher teacher = new Teacher { Firstname = model.Firstname, Middlename = model.Middlename, Lastname = model.Lastname, Gender = model.Gender, DateOfBirth = model.DateOfBirth.ToString(), PhoneNumber = model.PhoneNumber, EmailAddress = model.EmailAddress, Religion = model.Religion, ProfilePhotoPath = processFileUpload.UploadImage(model.Photo, "uploads"), IdentityUserId = user.Id, DepartmentId = model.DepartmentId }; _teacherRepository.InsertAndSaveTeacherData(teacher); TeacherContactInformation contactInformation = new TeacherContactInformation { Address1 = model.Address1, Address2 = model.Address2, AlternateEmailAddress = model.AlternateEmailAddress, ZipCode = model.ZipCode, HomePhone = model.HomePhone, MobilePhone = model.MobilePhone, NextOfKinFirstname = model.NextOfKinFirstname, NextOfKinLastname = model.NextOfKinLastname, RelationToNextOfKin = model.RelationToNextOfKin, PhoneOfNextOfKin = model.PhoneOfNextOfKin, EmailOfNextOfKin = model.EmailOfNextOfKin, TeacherId = teacher.Id, CountryId = model.CountryId }; _teacherRepository.InsertContactData(contactInformation); TeacherHighestDegree highestDegree = new TeacherHighestDegree { NameOfInstitution = model.NameOfInstitution, YearEnrolled = model.YearEnrolled.ToString(), YearOfGraduation = model.YearOfGraduation.ToString(), DegreeAttained = model.DegreeAttained, CGPA = model.CGPA, TeacherContactInfoId = contactInformation.Id }; _teacherRepository.InsertAndSaveHighestDegree(highestDegree); TeacherOtherDegree otherDegree = new TeacherOtherDegree { NameOfInstitution = model.OtherNameOfInstitution, YearOfEnrollement = model.YearOfEnrollement.ToString(), YearOfGraduation = model.OtherYearOfGraduation.ToString(), DegreeAttained = model.OtherDegreeAttained, CGPA = model.OtherCGPA, TeacherHighestDegreeId = highestDegree.Id }; _teacherRepository.InsertAndSaveOtherDegree(otherDegree); return(RedirectToAction("allteachers")); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } ListOfCountries(model); ListAllDepartments(model); return(View(model)); }
public async Task <IActionResult> AddStudent(AddStudentViewModel model) { if (ModelState.IsValid) { IdentityUser user = new IdentityUser { UserName = passwordGenerator.GenerateUsernameFromEmail(model.EmailAddress), Email = model.EmailAddress, PhoneNumber = model.PhoneNumber }; IdentityResult result = await userManager.CreateAsync(user, passwordGenerator.GeneratePassword(15)); string uniqueName = processFileUpload.UploadImage(model.Image, "uploads"); Student student = new Student { Firstname = model.Firstname, Middlename = model.Middlename, Lastname = model.Lastname, Gender = model.Gender, DateOfBirth = model.DateOfBirth.ToLongDateString(), PhoneNumber = model.PhoneNumber, AlternateEmailAddress = model.AlternateEmailAddress, EmailAddress = model.EmailAddress, Religion = model.Religion, MaritalStatus = model.MaritalStatus, ResidentialAddress = model.ResidentialAddress, ContactAddress = model.ContactAddress, AlternatePhoneNumber = model.AlternatePhoneNumber, IdentityUserId = user.Id, DepartmentId = model.DepartmentId, CourseYearId = SetStudentCurrentYear(model.PreviousLevel.Value.ToString()), ProfilePhotoPath = uniqueName, MatriculationNumber = SetMatriculationNumber(model.FacultyId, model.DepartmentId) }; studentRepository.InsertStudent(student); studentRepository.Save(); StudentAccademicInformation accademicInformation = new StudentAccademicInformation { NameOfInstitution = model.NameOfInstitution, YearEnrolled = model.YearEnrolled.ToLongDateString(), YearOfGraduation = model.YearOfGraduation.ToLongDateString(), PreviousLevel = model.PreviousLevel, StudentId = student.Id }; studentRepository.InsertStudentAccademicInformation(accademicInformation); studentRepository.Save(); StudentNextOfKinInformation nextOfKinInformation = new StudentNextOfKinInformation { NextOfKinFirstname = model.NextOfKinFirstname, NextOfKinLastname = model.NextOfKinLastname, RelationToNextOfKin = model.RelationToNextOfKin, PhoneOfNextOfKin = model.PhoneOfNextOfKin, EmailOfNextOfKin = model.EmailOfNextOfKin, StudentId = student.Id }; studentRepository.InsertStudentNextOfKin(nextOfKinInformation); studentRepository.Save(); StudentSponsor studentSponsor = new StudentSponsor { SponsorFirstname = model.SponsorFirstname, SponsorMiddlename = model.SponsorMiddlename, SponsorLastname = model.SponsorLastname, SponsorEmailAddress = model.SponsorEmailAddress, SponsorPhoneNumber = model.SponsorPhoneNumber, SponsorContactAddress = model.SponsorContactAddress, SponsorProffession = model.SponsorProffession, SponsorWorkAddress = model.SponsorWorkAddress, StudentId = student.Id }; SetStudentCourses(student.Id, student.DepartmentId, student.CourseYearId); studentRepository.InsertStudentSponsor(studentSponsor); studentRepository.Save(); return(Redirect("allstudents")); } ListAllFaculties(model); ListAllDepartments(model); return(View(model)); }