public async Task <IActionResult> Edit(int?id) { // get the reporting case ReportingCase repCase = await _context.ReportingCases.Include(rc => rc.ConcernedAgencies) .ThenInclude(ca => ca.User) .Include(rc => rc.CaseItems) .SingleOrDefaultAsync(rc => rc.Id == id); ReportingCaseEditVM vm = new ReportingCaseEditVM { DownTime = repCase.DownTime, CaseItems = repCase.CaseItems, ResolutionTime = repCase.ResolutionTime, ConcernedAgencies = repCase.ConcernedAgencies }; if (repCase.ConcernedAgencies.Count > 0) { vm.ConcernedAgencyId = repCase.ConcernedAgencies[0].UserId; } var userList = await _context.Users.ToListAsync(); ViewData["userId"] = new SelectList(userList, nameof(IdentityUser.Id), nameof(IdentityUser.UserName)); ViewBag.caseId = id; return(View(vm)); }
public async Task <IActionResult> Edit(int id, ReportingCaseEditVM vm) { if (ModelState.IsValid) { IdentityUser adminUser = await _userManager.FindByNameAsync("admin"); // get the reporting case ReportingCase repCase = await _context.ReportingCases.Include(rc => rc.ConcernedAgencies) .SingleOrDefaultAsync(rc => rc.Id == id); if (repCase == null) { return(NotFound()); } // update the reporting case repCase.ResolutionTime = vm.ResolutionTime; repCase.DownTime = vm.DownTime; repCase.CaseItems = vm.CaseItems; try { _context.Update(repCase); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { // check if the we are trying to edit was already deleted due to concurrency if (!_context.ReportingCases.Any(ci => ci.Id == id)) { return(NotFound()); } else { throw; } } // get the concerned agency Ids List <string> concernedUserIds = await _context.ReportingCaseConcerneds .Where(rcc => rcc.ReportingCaseId == id) .Select(rcc => rcc.UserId).ToListAsync(); if (!concernedUserIds.Any(usr => usr == vm.ConcernedAgencyId)) { // check if we need to add a new concerned agency // _context.ReportingCaseConcerneds.Remove(conc); // await _context.SaveChangesAsync(); ReportingCaseConcerned concerned = new ReportingCaseConcerned() { ReportingCaseId = repCase.Id, UserId = vm.ConcernedAgencyId }; _context.Add(concerned); int numInserted = await _context.SaveChangesAsync(); IdentityUser user = await _context.Users.FindAsync(vm.ConcernedAgencyId); string caseDetail = String.Join("<br>", repCase.CaseItems.Select(ci => $"{ci.Question} - {ci.Response}").ToArray()); await _emailSender.SendEmailAsync($"{user.Email};{adminUser.Email}", "WRLDC Issue alert", $"Sir/Madam,<br>You are being associated with an issue with id <b>{repCase.Id}</b> in WRLDC Issues portal." + "<br><b>Issue Details</b>" + $"<br>{caseDetail}" + "<br><br>For kind information please<br><br>Regards<br>IT WRLDC"); } return(RedirectToAction(nameof(Index)).WithSuccess("Issue Editing done")); } var userList = await _context.Users.ToListAsync(); ViewData["userId"] = new SelectList(userList, nameof(IdentityUser.Id), nameof(IdentityUser.UserName)); return(View(vm)); }