public async Task <IActionResult> Create([Bind("Id,Goal,Description,Links,TargetAudience,Type")] Project project) { if (ModelState.IsValid) { using (var dbContextTransaction = _context.Database.BeginTransaction()) { _context.Add(project); await _context.SaveChangesAsync(); int projectId = project.Id; User user = await _userManager.GetUserAsync(User); int schoolId = user.SchoolId; if (!(schoolId >= 1)) { dbContextTransaction.Rollback(); Console.WriteLine("SchoolId is NULL"); } SchoolProject schoolProject = new SchoolProject(projectId, schoolId); _context.Add(schoolProject); await _context.SaveChangesAsync(); dbContextTransaction.Commit(); } return(RedirectToAction(nameof(Index))); } return(View(project)); }
public async Task <IActionResult> Create([Bind("Id,Name,Address,Website,CountryId")] School school, string?coordinatorId) { if (ModelState.IsValid) { if (coordinatorId != null) { var user = await _userManager.FindByEmailAsync(coordinatorId); user.School = school; school.Coordinator = user; _context.Add(school); _context.Update(user); } else { _context.Add(school); } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(school)); }
public async Task <IActionResult> Create([Bind("Id,ProjectId,SchoolId")] SchoolProject schoolProject) { if (ModelState.IsValid) { _context.Add(schoolProject); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(schoolProject)); }
public async Task <ActionResult> CreateCoordinator(CoordinatiorSchoolViewModel coordinatiorSchool, IFormFile file) { var user = new User(); try { if (ModelState.IsValid && file != null) { byte[] bytes; using (var stream = new BinaryReader(file.OpenReadStream())) { bytes = stream.ReadBytes(Convert.ToInt32(file.Length)); } var userFile = new UserFile { Content = bytes, FileName = file.FileName, ContentType = file.ContentType, FileType = FileType.Certificate }; //Create an user object user = new User { Name = coordinatiorSchool.Name, UserName = coordinatiorSchool.Email, Email = coordinatiorSchool.Email, About = coordinatiorSchool.About, PhoneNumber = coordinatiorSchool.PhoneNumber, Active = false, NationalityId = coordinatiorSchool.Nationality, Certificate = userFile, SchoolId = 1//School default before Creating their school. }; //Creating the user in the bd var result = await _userManager.CreateAsync(user, coordinatiorSchool.Password); if (result.Succeeded) { //The coordinator has the same privilege of the professor await _userManager.AddToRoleAsync(user, "Coordinator"); await _context.SaveChangesAsync(); //TODO: Send email to admin var adminList = await _userManager.GetUsersInRoleAsync("Admin"); /* * foreach (var userAux in adminList) * { * await _emailSender.SendEmailAsync(userAux.Email, "IINTOS - Coordinatior Request", "<p>A new Coordinator have requested to join the platform.</p>" + * "<p> Go and check it out!</p>"); * } */ return(RedirectToAction("Create", "Schools", new { area = "", coordinator = user.Email })); } return(View()); } //saida default return(View()); } catch (Exception e) { await _userManager.DeleteAsync(user); return(RedirectToAction(nameof(CreateCoordinator))); } }