private async Task <DataSet> CreateDataSetAsync(MtdStore store, FormType type = FormType.Details) { if (store == null) { return(null); } WebAppUser webAppUser = await _userHandler.GetUserAsync(HttpContext.User); List <MtdFormPart> mtdFormParts = new List <MtdFormPart>(); IList <MtdFormPart> parts = await GetPartsAsync(store.MtdForm); bool isReviewer = await _userHandler.IsReviewer(webAppUser, store.MtdForm); ApprovalHandler approvalHandler = new ApprovalHandler(_context, store.Id); List <string> blockedParts = new List <string>(); if (!isReviewer) { blockedParts = await approvalHandler.GetBlockedPartsIds(); } foreach (MtdFormPart formPart in parts) { if (type == FormType.Edit) { bool isEditor = await _userHandler.IsEditorPartAsync(webAppUser, formPart.Id); if (isEditor && !blockedParts.Contains(formPart.Id)) { mtdFormParts.Add(formPart); } } else { bool isViewer = await _userHandler.IsViewerPartAsync(webAppUser, formPart.Id); if (isViewer) { mtdFormParts.Add(formPart); } } } IList <MtdFormPartField> mtdFormPartFields = await GetFieldsAsync(mtdFormParts); var mtdStore = await _context.MtdStore .Include(m => m.ParentNavigation) .Include(m => m.MtdFormNavigation) .ThenInclude(m => m.MtdFormHeader) .Include(m => m.MtdFormNavigation) .ThenInclude(m => m.ParentNavigation) .FirstOrDefaultAsync(m => m.Id == store.Id); IList <long> ids = await _context.MtdStoreStack.Where(x => x.MtdStore == mtdStore.Id).Select(x => x.Id).ToListAsync(); IList <MtdStoreStack> stack = await _context.MtdStoreStack .Include(m => m.MtdStoreStackText) .Include(m => m.MtdStoreStackDecimal) .Include(m => m.MtdStoreStackFile) .Include(m => m.MtdStoreStackDate) .Include(m => m.MtdStoreStackInt) .Include(m => m.MtdStoreLink) .Where(x => ids.Contains(x.Id)) .ToListAsync(); DataSet result = new DataSet() { Store = mtdStore, Parts = mtdFormParts, Fields = mtdFormPartFields, Stack = stack, }; return(result); }
private async Task <OutData> CreateDataAsync(string Id, WebAppUser user, TypeAction typeAction, IFormCollection requestForm) { var store = await _context.MtdStore .Include(m => m.MtdFormNavigation) .ThenInclude(p => p.MtdFormPart) .FirstOrDefaultAsync(m => m.Id == Id); List <string> partsIds = new List <string>(); bool isReviewer = await _userHandler.IsReviewer(user, store.MtdForm); ApprovalHandler approvalHandler = new ApprovalHandler(_context, store.Id); List <string> blockedParts = new List <string>(); if (!isReviewer) { blockedParts = await approvalHandler.GetBlockedPartsIds(); } foreach (var part in store.MtdFormNavigation.MtdFormPart) { switch (typeAction) { case TypeAction.Create: { if (await _userHandler.IsCreatorPartAsync(user, part.Id)) { partsIds.Add(part.Id); } break; } default: { if (await _userHandler.IsEditorPartAsync(user, part.Id) && !blockedParts.Contains(part.Id)) { partsIds.Add(part.Id); } break; } } } var fields = await _context.MtdFormPartField.Include(m => m.MtdFormPartNavigation) .Where(x => partsIds.Contains(x.MtdFormPartNavigation.Id)) .OrderBy(x => x.MtdFormPartNavigation.Sequence) .ThenBy(x => x.Sequence) .ToListAsync(); var titleField = fields.FirstOrDefault(x => x.MtdSysType == 1); List <MtdStoreStack> stackNew = new List <MtdStoreStack>(); foreach (MtdFormPartField field in fields) { string data = requestForm[field.Id]; MtdStoreStack mtdStoreStack = new MtdStoreStack() { MtdStore = Id, MtdFormPartField = field.Id }; switch (field.MtdSysType) { case 2: { if (data != string.Empty) { bool isOkInt = int.TryParse(data, out int result); if (isOkInt) { mtdStoreStack.MtdStoreStackInt = new MtdStoreStackInt { Register = result }; } } break; } case 3: { if (data != string.Empty) { string separ = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator; bool isOkDecimal = decimal.TryParse(data.Replace(".", separ), out decimal result); if (isOkDecimal) { mtdStoreStack.MtdStoreStackDecimal = new MtdStoreStackDecimal { Register = result }; } } break; } case 5: case 6: case 10: { if (data != string.Empty) { bool isOkDate = DateTime.TryParse(data, out DateTime dateTime); if (isOkDate) { mtdStoreStack.MtdStoreStackDate = new MtdStoreStackDate { Register = dateTime }; } } break; } case 7: case 8: { var actionDelete = requestForm[$"{field.Id}-delete"]; if (actionDelete.FirstOrDefault() == null || actionDelete.FirstOrDefault() == "false") { IFormFile file = requestForm.Files.FirstOrDefault(x => x.Name == field.Id); if (file != null) { byte[] streamArray = new byte[file.Length]; await file.OpenReadStream().ReadAsync(streamArray, 0, streamArray.Length); mtdStoreStack.MtdStoreStackFile = new MtdStoreStackFile() { Register = streamArray, FileName = file.FileName, FileSize = streamArray.Length, FileType = file.ContentType }; } if (file == null) { MtdStoreStack stackOld = await _context.MtdStoreStack .Include(m => m.MtdStoreStackFile) .OrderByDescending(x => x.Id) .FirstOrDefaultAsync(x => x.MtdStore == Id & x.MtdFormPartField == field.Id); if (stackOld != null && stackOld.MtdStoreStackFile != null) { mtdStoreStack.MtdStoreStackFile = new MtdStoreStackFile() { FileName = stackOld.MtdStoreStackFile.FileName, FileSize = stackOld.MtdStoreStackFile.FileSize, Register = stackOld.MtdStoreStackFile.Register, FileType = stackOld.MtdStoreStackFile.FileType, }; } } } break; } case 11: { if (data != string.Empty) { string datalink = Request.Form[$"{field.Id}-datalink"]; mtdStoreStack.MtdStoreLink = new MtdStoreLink { MtdStore = data, Register = datalink }; } break; } case 12: { bool isOkCheck = bool.TryParse(data, out bool check); if (isOkCheck) { mtdStoreStack.MtdStoreStackInt = new MtdStoreStackInt { Register = check ? 1 : 0 }; } break; } default: { if (data != string.Empty) { mtdStoreStack.MtdStoreStackText = new MtdStoreStackText() { Register = data }; } break; } } stackNew.Add(mtdStoreStack); } OutData outParam = new OutData() { MtdFormPartField = titleField, MtdStoreStacks = stackNew, }; return(outParam); }