public async Task <IActionResult> Edit(int id, AgentViewModel agent) { if (id != agent.Id) { return(NotFound()); } Random rd = new Random(); Int32 passCode = rd.Next(9999); if (ModelState.IsValid) { try { if (!agent.IsActive && agent.Status == 1) { if (agent.EmpCode == null) { agent.EmpCode = "A-" + passCode; } ////agent.AgentAppUserName = agent.FirstName + "." + agent.SurName; //agent.AgentAppUserName = agent.PersonalEmail; //var password = agent.SurName + passCode + "@Merkato"; ////var password = "******"; //agent.AgentAppPassword = password; //var userName = agent.AgentAppUserName; //var email = agent.PersonalEmail; //ApplicationRole applicationRole = await _roleManager.FindByNameAsync("Agent"); //var role = applicationRole.Id; //UserViewModel user = new UserViewModel(); //{ // //user.UserName = userName; // user.Email = email; // user.Password = password; // user.ConfirmPassword = password; // user.ApplicationRoleId = role; //} //Repository repository = new Repository(_userManager, _roleManager); //await repository.CreateUser(user); //agent.IsActive = true; _context.Update(agent.GetModel()); await _context.SaveChangesAsync(); _mailSender.SendEmail(agent.PersonalEmail, "Merkato Notification", $"Merkato has been approved your request, kindly start with the training.{Environment.NewLine} -Username :{agent.PersonalEmail} {Environment.NewLine} -Password:{agent.AgentAppPassword}" + $"{Environment.NewLine} -Application link http://173.248.135.167/Merkato" + $"{Environment.NewLine} {Environment.NewLine} {Environment.NewLine} Merkato Team", _context, false); } else { _context.Update(agent.GetModel()); await _context.SaveChangesAsync(); } } catch (DbUpdateConcurrencyException) { if (!AgentExists(agent.Id)) { return(NotFound()); } else { throw; } } //if (User.IsInRole("Agent")) // return RedirectToAction(nameof(AgentView)); return(RedirectToAction(nameof(Index))); } agent.loadLists(_context); return(View(agent)); }
public async Task <IActionResult> Create(AgentViewModel agent) { Random rd = new Random(); Int32 passCode = rd.Next(9999); // DO BE EDIT BACK if (!ModelState.IsValid) { if (agent.Status == 1) { if (agent.EmpCode == null) { agent.EmpCode = "A-" + passCode; } //agent.AgentAppUserName = agent.FirstName + "." + agent.SurName; agent.AgentAppUserName = agent.PersonalEmail; var password = agent.SurName + passCode + "@Merkato"; //var password = "******"; agent.AgentAppPassword = password; var userName = agent.AgentAppUserName; var email = agent.PersonalEmail; ApplicationRole applicationRole = await _roleManager.FindByNameAsync("Agent"); var role = applicationRole.Id; UserViewModel user = new UserViewModel(); { //user.UserName = userName; user.Email = email; user.Password = password; user.ConfirmPassword = password; user.ApplicationRoleId = role; } Repository repository = new Repository(_userManager, _roleManager); await repository.CreateUser(user); agent.IsActive = true; //var httpRequest = HttpContext.Request.Form.Files; //var files = new List<string>(); //foreach (var Image in files) //{ // if (Image != null && Image.Length > 0) // { // var file = Image; // var postedFile = files[0]; // //There is an error here // if (file.Length > 0) // { // var fileName = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName); // var filePath = Path.Combine(_appEnvironment.WebRootPath, "images\\Agents"); // using (var fileStream = new FileStream(Path.Combine(filePath, fileName), FileMode.Create)) // { // await file.CopyToAsync(fileStream); // //files.Add(filePath); // agent.FirstPictureUrl = fileName; // //result = fileName; // } // } // } //} _context.Add(agent.GetModel()); await _context.SaveChangesAsync(); _mailSender.SendEmail(agent.PersonalEmail, "Merkato Notification", $"Merkato has been accepted your request, kindly find your credentials below.{Environment.NewLine} -Username :{agent.AgentAppUserName} {Environment.NewLine} -Password:{agent.AgentAppPassword}" + $"{Environment.NewLine} -Agent Code:{agent.EmpCode}" + $"{Environment.NewLine} -Application link http://173.248.135.167/Merkato" + $"{Environment.NewLine} {Environment.NewLine} {Environment.NewLine} Merkato Team", _context, false); return(RedirectToAction(nameof(Index))); } else { agent.IsActive = false; _context.Add(agent.GetModel()); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } agent.loadLists(_context); return(View(agent)); }