public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { using (var trans = _context.Database.BeginTransaction()) { var schoolCode = Shared.String.GenerateRandom(_settings.SchoolCodeLength, true, false, false, true); while (_schoolManager.GetAll().Where(o => o.Code == schoolCode).Count() > 0) { schoolCode = Shared.String.GenerateRandom(_settings.SchoolCodeLength, true, false, false, true); } var school = await _schoolManager.AddAsync(new SchoolProfile { Id = Guid.NewGuid(), ContactNumber = model.ContactNumber, FirstName = model.FirstName, LastName = model.LastName, Name = model.SchoolName, Code = schoolCode });; var user = new SchoolUser { UserName = model.Email, Email = model.Email, //extended properties FirstName = model.FirstName, LastName = model.LastName, School = school, Type = UserType.Admin, InsertedDateTime = DateTime.Now, UpdatedDateTime = DateTime.Now }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { await _userManager.AddToRoleAsync(user, "Admin"); // Send an email with this link var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); await _emailSender.SendEmailAsync(model.Email, "Confirm your account", $"Please confirm your account by clicking this: <a href='{callbackUrl}'>link</a>"); _logger.LogInformation(3, "User created a new account with password."); await trans.CommitAsync(); return(RedirectToAction(nameof(SuccessRegister))); } AddErrors(result); } } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ResponseResult <StudentViewModel> > AddStudent(StudentViewModel data, SchoolProfile school) { using (var trans = _context.Database.BeginTransaction()) { var r = Shared.String.GenerateRandom(_settings.StudentIdLength, false, true, false, false); var studentId = $"{school.Code}{r}"; //check if studentID already exist while (_manager.GetList(o => o.StudentId == studentId).Count() > 0) { r = Shared.String.GenerateRandom(_settings.StudentIdLength, false, true, false, false); studentId = $"{school.Code}{r}"; } var username = $"{studentId}@{school.Name.Replace(" ", "_")}.com"; var user = new SchoolUser { UserName = username, Email = username, EmailConfirmed = true, //extended properties FirstName = data.FirstName, LastName = data.LastName, School = school, Type = UserType.Student, InsertedDateTime = DateTime.Now, UpdatedDateTime = DateTime.Now }; var password = $"{data.LastName.ToProperCase()}{r}!"; var result = await _userManager.CreateAsync(user, password); if (result.Succeeded) { await _userManager.AddToRoleAsync(user, "Student"); var student = await _manager.AddAsync(new Model.StudentProfile() { FirstName = data.FirstName, LastName = data.LastName, MiddleName = data.MiddleName, Gender = data.Gender, DOB = data.DOB, Email = data.Email, Phone = data.Phone, Mobile = data.Mobile, StudentId = studentId, School = school, User = user }); data.Id = student.Id; await trans.CommitAsync(); } } return(ResponseResult <StudentViewModel> .SetSuccess(data)); }
public async Task <ResponseResult <SchoolTeacherViewModel> > AddTeacher(SchoolTeacherViewModel data, SchoolProfile school) { using (var trans = _context.Database.BeginTransaction()) { var r = Shared.String.GenerateRandom(4, false, true, false, false); var username = $"{data.FirstName[0]}{data.LastName.ToLower()}_{r}@{school.Name.Replace(" ", "_")}.com"; //check if studentID already exist while (_manager.GetList(o => o.User.UserName == username, o => o.User).Count() > 0) { r = Shared.String.GenerateRandom(4, false, true, false, false); username = $"{data.FirstName[0]}{data.LastName.ToLower()}_{r}@{school.Name.Replace(" ", "_")}.com"; } var user = new SchoolUser { UserName = username, Email = username, EmailConfirmed = true, //extended properties FirstName = data.FirstName, LastName = data.LastName, School = school, Type = UserType.Teacher, InsertedDateTime = DateTime.Now, UpdatedDateTime = DateTime.Now, }; var password = $"{data.LastName.ToProperCase()}{r}!"; var result = await _userManager.CreateAsync(user, password); if (result.Succeeded) { await _userManager.AddToRoleAsync(user, "Teacher"); var teacher = await _manager.AddAsync(new SchoolTeacher() { FirstName = data.FirstName, MiddleName = data.MiddleName, LastName = data.LastName, Position = data.Position, User = user }); data.Id = teacher.Id; await trans.CommitAsync(); } } return(ResponseResult <SchoolTeacherViewModel> .SetSuccess(data)); }
public async Task <ResponseResult <SchoolYearViewModel> > AddSchoolYear(SchoolYearViewModel data, SchoolProfile school) { await _manager.AddAsync(new Model.SchoolYear() { Name = data.Name, Start = data.Start.Value, End = data.End.Value, InsertedDateTime = DateTime.Now, UpdatedDateTime = DateTime.Now, School = school }); return(ResponseResult <SchoolYearViewModel> .SetSuccess(data)); }
public async Task <ResponseResult <SchoolLevelViewModel> > AddSchoolLevel(SchoolLevelViewModel data) { using (var trans = _context.Database.BeginTransaction()) { var school = _schoolManager.GetSingle(o => o.Id == data.SchoolId); var level = await _manager.AddAsync(new SchoolLevel() { Name = data.Name, School = school }); data.Id = level.Id; await trans.CommitAsync(); } return(ResponseResult <SchoolLevelViewModel> .SetSuccess(data)); }
public async Task <ActionResult <Armure> > Post([FromBody] Armure armure) { var result = await manager.AddAsync(armure); return(result != null ? (ActionResult)CreatedAtAction("Get", result) : BadRequest()); }
public async Task <ResponseResult <StudentViewModel> > UpdateStudent(StudentViewModel data, SchoolProfile school) { using (var trans = _context.Database.BeginTransaction()) { if (data.CurrentAddress.Id == Guid.Empty) { data.CurrentAddress = await _addressManager.AddAsync(new StudentAddress() { Address = data.CurrentAddress.Address, City = data.CurrentAddress.City, Zip = data.CurrentAddress.Zip, Country = data.CurrentAddress.Country }); } else { data.CurrentAddress = await _addressManager.UpdateAsync(new StudentAddress() { Id = data.CurrentAddress.Id, Address = data.CurrentAddress.Address, City = data.CurrentAddress.City, Zip = data.CurrentAddress.Zip, Country = data.CurrentAddress.Country }); } if (data.PermanentAddress.Id == Guid.Empty) { data.PermanentAddress = await _addressManager.AddAsync(new StudentAddress() { Address = data.PermanentAddress.Address, City = data.PermanentAddress.City, Zip = data.PermanentAddress.Zip, Country = data.PermanentAddress.Country }); } else { data.PermanentAddress = await _addressManager.UpdateAsync(new StudentAddress() { Id = data.PermanentAddress.Id, Address = data.PermanentAddress.Address, City = data.PermanentAddress.City, Zip = data.PermanentAddress.Zip, Country = data.PermanentAddress.Country }); } await _manager.UpdateAsync(new StudentProfile() { Id = data.Id, StudentId = data.StudentId, FirstName = data.FirstName, LastName = data.LastName, MiddleName = data.MiddleName, DOB = data.DOB, Email = data.Email, Gender = data.Gender, Mobile = data.Mobile, Phone = data.Phone, CurrentAddress = data.CurrentAddress, PermanentAddress = data.PermanentAddress }); await trans.CommitAsync(); } return(ResponseResult <StudentViewModel> .SetSuccess(data)); }