public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { InitModel(false); return(Page()); } var files = HttpContext.Request.Form.Files; var qrList = QuestionReplyList.Values.SelectMany(qr => qr.SubQuestionList).ToList(); for (int i = 0; i < files.Count; i++) { if (files[i] == null || files[i].Length == 0) { continue; } using (var memoryStream = new MemoryStream()) { await files[i].CopyToAsync(memoryStream); qrList[i].ReportImage = memoryStream.ToArray(); } } Report.ReportDate = DateTime.Now; Report.QuestionReply = qrList; await _context.SaveReport(Report); return(RedirectToPage("./Index")); }
public async Task <IActionResult> Sync([FromBody] List <Report> reports) { if (!ModelState.IsValid) { var errorsModel = new StringBuilder(); foreach (var modelState in ModelState.Values) { foreach (var error in modelState.Errors) { if (error.Exception != null) { errorsModel.AppendLine(error.Exception.Message); } else if (error.ErrorMessage != null) { errorsModel.AppendLine(error.ErrorMessage); } } } var res = new SyncModel() { Error = errorsModel.ToString() }; return(CreatedAtAction("Sync", res)); } var CryptInfo = this.HttpContext.Request.Headers["CryptInfo"]; if (CryptInfo.Count == 0) { var res = new SyncModel() { Error = "Login failed." }; return(CreatedAtAction("Sync", res)); } _logger.LogInformation("sync..."); if (reports == null) { _logger.LogInformation("sync...request is null"); } else { _logger.LogInformation($"sync...request '{CryptInfo[0]}'"); } var model = new SyncModel(); StringBuilder sb = new StringBuilder(); try { var userId = CheckUser(CryptInfo[0]); try { if (reports != null) { foreach (var report in reports) { if (report.AgedCareCenterId == -1 || report.AssessorId == -1) { continue; } if (report.IsDeleted) { if (report.QuestionReply != null) { foreach (var reply in report.QuestionReply) { if (reply.QuestionReplyId != 0) { _context.Remove(reply); } } } _logger.LogInformation($"INFO Delete...report #'{report.ReportId}'"); _context.Remove(report); await _context.SaveChangesAsync(); } else if (report.IsNew) { _logger.LogInformation($"INFO Inser new...report'{report.ReportDate}'"); await _context.SaveReport(report); } else if (report.IsChanged) { _logger.LogInformation($"INFO Update...report #'{report.ReportId}'"); await _context.UpdateReport(report); } } } } catch (Exception ex) { _logger.LogError(ex, "ReportSyncError"); sb.AppendLine(ex.Message); } model.AccreditationStandartList = await _context.AccreditationStandarts.Include(acc => acc.Questions).ToListAsync(); model.AgedCareCenterList = await _context.AgedCareCenters.ToListAsync(); model.AssessorList = await _context.Assessors.ToListAsync(); var Questions = await _context.Questions.Include(q => q.Questions).GroupBy(q => q.AccreditationStandartId).ToListAsync(); model.ReportList = await _context.GetReportsWithReplies(userId); var newReport = new Report { AgedCareCenterId = -1, AssessorId = -1, ReportDate = DateTime.Now, IsNew = true }; var questionReplyList = from asst in model.AccreditationStandartList from q in asst.Questions select new QuestionReply(q); newReport.QuestionReply = questionReplyList.SelectMany(q => q.SubQuestionList).ToList(); model.NewReport = newReport; model.ReportList .ForEach(r => { foreach (var item in r.QuestionReply) { var q = item.Question; if (item.Question.ParentId.HasValue) { item.QuestionParentId = item.Question.ParentId.Value; q = item.Question.Parent; } item.AccreditationStandartId = q.AccreditationStandartId.Value; } var questionNumberOrderBy = 0; //r.QuestionReply.GroupBy(qr => qr.Question.AccreditationStandartId) // .SelectMany(g => g.OrderBy(q => q.QuestionId) // .GroupBy(qr2 => qr2.QuestionParentId) // .SelectMany(g2 => g2.OrderBy(q => q.QuestionId) // .Select((qr, i) => // { // questionNumberOrderBy++; // qr.QuestionNumber = $"{g.Key}.{i + 1}"; // return qr.QuestionNumber; // })) // .ToList(); //r.QuestionReply = r.QuestionReply.OrderBy(qr => qr.AccreditationStandartId).ToList(); }); } catch (Exception ex) { _logger.LogError(ex, "MobileSync"); sb.AppendLine(ex.Message); } finally { var message = sb.ToString(); model.Error = message; } return(CreatedAtAction("Sync", model)); }