public async Task <IActionResult> Create() { ReportingCaseCreateVM vm = new ReportingCaseCreateVM { DownTime = DateTime.Now, CaseItems = await _context.CaseItemTemplates.Include(cit => cit.Options).OrderBy(ci => ci.SerialNum).ToListAsync() }; vm.Responses = Enumerable.Range(1, vm.CaseItems.Count).Select(i => "").ToList(); vm.ChoiceTexts = Enumerable.Range(1, vm.CaseItems.Count).Select(i => "").ToList(); var userList = await _context.Users.ToListAsync(); ViewData["userId"] = new SelectList(userList, nameof(IdentityUser.Id), nameof(IdentityUser.UserName)); return(View(vm)); }
public async Task <IActionResult> Create(ReportingCaseCreateVM vm) { if (ModelState.IsValid) { // create a new case object ReportingCase newCase = new ReportingCase() { DownTime = vm.DownTime }; // add issue created by Information newCase.CreatedById = _userManager.GetUserId(User); // add case items from vm List <ReportingCaseItem> caseItems = new List <ReportingCaseItem>(); for (int caseTemplIter = 0; caseTemplIter < vm.CaseItems.Count; caseTemplIter++) { CaseItemTemplate caseItemTemplate = vm.CaseItems[caseTemplIter]; string resp = vm.Responses[caseTemplIter]; if (caseItemTemplate.ResponseType == ResponseType.ChoicesWithText) { resp = vm.ChoiceTexts[caseTemplIter] ?? resp; } ReportingCaseItem caseItem = new ReportingCaseItem() { SerialNum = caseItemTemplate.SerialNum, Question = caseItemTemplate.Question, ResponseType = caseItemTemplate.ResponseType, Response = resp }; caseItems.Add(caseItem); } newCase.CaseItems = caseItems; // Getting Attachment IFormFile attachment = vm.Attachment; // Saving attachment on Server if (attachment != null && attachment.Length > 0) { // create a new filename with timestamp string attachmentPath = $"{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}_{Guid.NewGuid()}{Path.GetExtension(attachment.FileName)}"; string filePath = Path.Combine(_folderPaths.AttachmentsFolder, attachmentPath); using (var fileStream = new FileStream(filePath, FileMode.Create)) { attachment.CopyTo(fileStream); newCase.AttachmentName = attachment.FileName; newCase.AttachmentPath = attachmentPath; } } _context.Add(newCase); int numInserted = await _context.SaveChangesAsync(); if (numInserted >= 1) { _logger.LogInformation("New Case created"); // insert each concerned agency if (!string.IsNullOrWhiteSpace(vm.ConcernedAgencyId)) { string caseDetail = String.Join("<br>", newCase.CaseItems.Select(ci => $"{ci.Question} - {ci.Response}").ToArray()); foreach (string concAgId in vm.ConcernedAgencyId.Split(",")) { IdentityUser user = await _context.Users.FindAsync(concAgId); ReportingCaseConcerned concerned = new ReportingCaseConcerned() { ReportingCaseId = newCase.Id, UserId = user.Id }; _context.Add(concerned); numInserted = await _context.SaveChangesAsync(); if (numInserted == 1) { _logger.LogInformation("New Case Concerned agency created"); await _emailSender.SendEmailAsync($"{user.Email}", "New Issue alert", $"Sir/Madam,<br>You are being associated with a new issue with id <b>{newCase.Id}</b> in WRLDC issues portal." + "<br><br><b>Issue Details</b>" + $"<br>{caseDetail}" + "<br><br>For kind information please<br><br>Regards<br>IT WRLDC"); } else { string msg = $"New Case Concerned agency not created as db returned num insertions as {numInserted}"; _logger.LogInformation(msg); //todo create custom exception throw new Exception(msg); } } return(RedirectToAction(nameof(Index)).WithSuccess("New Issue created")); } } else { string msg = $"New Case not created as db returned num insertions as {numInserted}"; _logger.LogInformation(msg); //todo create custom exception throw new Exception(msg); } } return(View(vm)); }