public KVRRQuestionCategories CheckCategoryExisted(string text) { KVRRQuestionCategories category = new KVRRQuestionCategories(); if (text == KVRRQuestionCategories.BigExpenses.GetDisplayName() || text == KVRRQuestionsCategory.BigExpenses) { category = (KVRRQuestionCategories)0; } else if (text == KVRRQuestionCategories.Inflationary.GetDisplayName() || text == KVRRQuestionsCategory.Inflationary) { category = (KVRRQuestionCategories)1; } else if (text == KVRRQuestionCategories.Investment.GetDisplayName() || text == KVRRQuestionsCategory.Investment) { category = (KVRRQuestionCategories)2; } else if (text == KVRRQuestionCategories.PriceFluctuations.GetDisplayName() || text == KVRRQuestionsCategory.PriceFluctuations) { category = (KVRRQuestionCategories)3; } else if (text == KVRRQuestionCategories.RiskVersusProfit.GetDisplayName() || text == KVRRQuestionsCategory.RiskVersusProfit) { category = (KVRRQuestionCategories)4; } else if (text == KVRRQuestionCategories.Discount.GetDisplayName() || text == KVRRQuestionsCategory.Discount) { category = (KVRRQuestionCategories)5; } else if (text == KVRRQuestionCategories.UnderstandingTheRisks.GetDisplayName() || text == KVRRQuestionsCategory.UnderstandingTheRisks) { category = (KVRRQuestionCategories)6; } else if (text == KVRRQuestionCategories.PersonalTime.GetDisplayName() || text == KVRRQuestionsCategory.PersonalTime) { category = (KVRRQuestionCategories)7; } else if (text == KVRRQuestionCategories.LongTermInvestment.GetDisplayName() || text == KVRRQuestionsCategory.LongTermInvestment) { category = (KVRRQuestionCategories)8; } else { category = (KVRRQuestionCategories)400; } return(category); }
public async Task <IActionResult> ImportList(IFormFile file) { bool IsInvalidFile = false; if (file != null) { if (file.Length > 0) { string fileName = file.FileName; string extension = Path.GetExtension(fileName).ToLower(); if (extension != ".xlsx") { TempData["Error"] = ValidationMessages.WrongExcelFile; return(RedirectToAction(nameof(KVRRQuestionAnswerController.List))); } using (var stream = new MemoryStream()) { await file.CopyToAsync(stream); using (var package = new ExcelPackage(stream)) { ExcelWorksheet worksheet = package.Workbook.Worksheets[1]; if (worksheet.Dimension == null) { TempData["Error"] = ValidationMessages.DataIsEmpty; return(RedirectToAction(nameof(KVRRQuestionAnswerController.List))); } //Check if user choose the wrong file (Ex: KVRR instead of FAQ) //question column var questionCol = worksheet.Cells["B1"].Value?.ToString().Trim(); if (!questionCol.Equals(Model.Resources.Common.PortfolioContent)) { TempData["Error"] = ValidationMessages.FileIsWrongType; return(RedirectToAction(nameof(FAQController.List))); } KVRRQuestionCategories checkValidateInsideFile = new KVRRQuestionCategories(); checkValidateInsideFile = _kvrrQuestionAnswerService.CheckNeededFields(file); if (checkValidateInsideFile == (KVRRQuestionCategories)404) { TempData["Error"] = string.Format(ValidationMessages.FieldEmpty, "Danh mục"); return(RedirectToAction(nameof(KVRRQuestionAnswerController.List))); } var rowCount = worksheet.Dimension.Rows; List <string> listQuestionContent = new List <string>(); for (int row = 2; row <= rowCount; row++) { string questionsTitle = ""; questionsTitle = worksheet.Cells[row, 3].Value?.ToString().Trim(); listQuestionContent.Add(questionsTitle); //if check > 1 then data duplicate var check = listQuestionContent.FindAll(x => x == questionsTitle).Count(); if (check > 1) { TempData["Error"] = ValidationMessages.DuplicatedQuestionImport + " '" + questionsTitle + "'"; return(RedirectToAction(nameof(KVRRQuestionAnswerController.List))); } } if (checkValidateInsideFile == (KVRRQuestionCategories)400) { TempData["Error"] = string.Format(ValidationMessages.CategoryIsNotExisted, ""); return(RedirectToAction(nameof(KVRRQuestionAnswerController.List))); } if (checkValidateInsideFile == (KVRRQuestionCategories)401) { TempData["Error"] = string.Format(ValidationMessages.FieldEmpty, "Câu hỏi"); return(RedirectToAction(nameof(KVRRQuestionAnswerController.List))); } if (checkValidateInsideFile == (KVRRQuestionCategories)402) { TempData["Error"] = string.Format(ValidationMessages.MarkMustBeNumber); return(RedirectToAction(nameof(KVRRQuestionAnswerController.List))); } if (checkValidateInsideFile == (KVRRQuestionCategories)403) { TempData["Error"] = string.Format(ValidationMessages.LessThan2Answers); return(RedirectToAction(nameof(KVRRQuestionAnswerController.List))); } if (checkValidateInsideFile == (KVRRQuestionCategories)405) { TempData["Error"] = string.Format(ValidationMessages.MarkOrAnswerIsNull); return(RedirectToAction(nameof(KVRRQuestionAnswerController.List))); } IsInvalidFile = true; } } } } else { TempData["Error"] = ValidationMessages.NoFile; return(RedirectToAction(nameof(KVRRQuestionAnswerController.List))); } if (IsInvalidFile == true) { TempData["import_ok"] = ValidationMessages.ImportSuccessful; await _kvrrQuestionAnswerService.ImportListQuestions(file); } return(RedirectToAction(nameof(KVRRQuestionAnswerController.List))); }
public KVRRQuestionCategories CheckNeededFields(IFormFile file) { KVRRQuestionCategories category = new KVRRQuestionCategories(); using (var stream = new MemoryStream()) { file.CopyToAsync(stream); using (var package = new ExcelPackage(stream)) { ExcelWorksheet worksheet = package.Workbook.Worksheets[1]; var rowCount = worksheet.Dimension.Rows; for (int row = 2; row <= rowCount; row++) { //check category var categoryCell = ""; if (worksheet.Cells[row, 2].Value == null) { return((KVRRQuestionCategories)404); } else { categoryCell = worksheet.Cells[row, 2].Value.ToString().Trim(); } //check if question content is null if (worksheet.Cells[row, 3].Value == null) { return((KVRRQuestionCategories)401); } else { string contentQuestion = worksheet.Cells[row, 3].Value.ToString().Trim(); } //check if question content is not existed if (CheckCategoryExisted(categoryCell) == (KVRRQuestionCategories)400) { return((KVRRQuestionCategories)400); } List <KVRRAnswerMarkValidate> listAnswer = new List <KVRRAnswerMarkValidate>(); for (int col = 4; col <= 22; col += 2) { string answerContent = ""; string mark = ""; if (worksheet.Cells[row, col].Value == null) { if (worksheet.Cells[row, col + 1].Value != null) { return((KVRRQuestionCategories)405); } } if (worksheet.Cells[row, col + 1].Value == null) { if (worksheet.Cells[row, col].Value != null) { return((KVRRQuestionCategories)405); } } if (worksheet.Cells[row, col + 1].Value != null && worksheet.Cells[row, col].Value != null) { mark = worksheet.Cells[row, col + 1].Value.ToString().Trim(); if (!IsPositiveNumber(mark)) { return((KVRRQuestionCategories)402); } else { answerContent = worksheet.Cells[row, col].Value.ToString().Trim(); KVRRAnswerMarkValidate answer = new KVRRAnswerMarkValidate() { AnswerContent = answerContent, Mark = mark }; listAnswer.Add(answer); } } } if (listAnswer.Count < 2) { return((KVRRQuestionCategories)403); } } } } return(category); }