public async Task ExcelUploadBusinessTest_ValidateFile_Sucess() { var model = new ExcelUploadModel() { Name = "test", Content = FileHelper.ReadBytes($"{baseLocation}//Sample Excel_valid.xlsx") }; var res = await _uploadBusinessService.Validate(model); res.Should().NotBeNull(); res.HttpStatusCode.Should().Be(StatusCodes.BadRequest); var age20FailedResponse = res.ErrorMessages.Find(p => p.Row.Equals(2)); age20FailedResponse.Content.Row.Should().Be(2); var age23FailedResponse = res.ErrorMessages.Find(p => p.Row.Equals(4)); age20FailedResponse.Should().NotBeNull(); age23FailedResponse.Should().NotBeNull(); age20FailedResponse.Content.Should().NotBeNull(); age23FailedResponse.Content.Should().NotBeNull(); age20FailedResponse.Content.Row.Should().Be(2); age23FailedResponse.Content.Row.Should().Be(4); // content will have the vallues }
public async Task <ExcelUploadResponseModel> Upload(ExcelUploadModel model) { string tempFileName = GetTemperotyFileName(); //saving to temprory file; FileHelper.CreateDirectory(app_data); FileHelper.WriteToFile(model.Content, tempFileName); //performing the validation var dataTable = await _excelReader.ReadFromExcel(tempFileName); var validationResults = ApplyValidation(dataTable); if (validationResults.HttpStatusCode != StatusCodes.Sucess) { FileHelper.Delete(tempFileName); return(validationResults); } if (!await _storageRepsitory.Move(tempFileName)) { throw new OperationCanceledException("Could not copy the files"); } return(new ExcelUploadResponseModel(null)); }
private async Task <DataTable> GetExcelTable(ExcelUploadModel model, string tempFileName) { //saving to temprory file; FileHelper.CreateDirectory(app_data); FileHelper.WriteToFile(model.Content, tempFileName); //performing the validation return(await _excelReader.ReadFromExcel(tempFileName)); }
private ExcelUploadModel ExcelCellToModel(ExcelWorksheet worksheet, int row) { ExcelUploadModel msdata = new ExcelUploadModel { SrNo = Convert.ToInt32(worksheet.Cells[row, 1].Value), Title = worksheet.Cells[row, 2].Value?.ToString().Trim(), Content = worksheet.Cells[row, 3].Value?.ToString().Trim(), FeaturedImage = worksheet.Cells[row, 4].Value?.ToString().Trim(), Category = worksheet.Cells[row, 5].Value?.ToString().Trim(), Tags = worksheet.Cells[row, 6].Value?.ToString().Trim() }; return(msdata); }
public async Task <ExcelUploadResponseModel> Upload(ExcelUploadModel model) { try { return(await _businessService.Upload(model)); } catch (Exception ex) { throw new FaultException <FaultModel>(new FaultModel { Exception = ex.InnerException, Message = ex.Message }); } }
public async Task ExcelUploadBusinessTest_UploadFiles_Sucess() { var model = new ExcelUploadModel() { Name = "test", Content = FileHelper.ReadBytes($"{baseLocation}//Sample Excel_valid.xlsx") }; var res = await _uploadBusinessService.Upload(model); res.Should().NotBeNull(); res.HttpStatusCode.Should().Be(StatusCodes.Sucess); }
public async Task ExcelUploadBusinessTest_UploadFiles_Failure() { var model = new ExcelUploadModel() { Name = "test", Content = FileHelper.ReadBytes($"{baseLocation}//Sample Excel_Invalid.xlsx") }; var res = await _uploadBusinessService.Upload(model); res.Should().NotBeNull(); res.HttpStatusCode.Should().Be(StatusCodes.BadRequest); res.ErrorMessages.Count().Should().Be(2); }
public ActionResult CreateExcel(ExcelUploadModel model) { try { if (ModelState.IsValid) { var excelReader = new PaidDataExcelReader(); //upload excel file var result = excelReader.UploadCpExcelFile(model.ExcelFile); if (result.Key) { //if upload is successfull insert the record into the the cpuploads //if insert in ok process the excel file details var processResult = excelReader.ProcessExcel(result.Value, model.OrgId, User.Identity.Name); var sucessResult = ""; if (processResult.Key) { sucessResult = processResult.Value; } else { TempData["ExcelSummary"] = processResult.Value; return(RedirectToAction("Index", new { orgid = model.OrgId })); } TempData["ExcelSummary"] = sucessResult; return(RedirectToAction("Index", new { orgid = model.OrgId })); } else { TempData["ExcelSummary"] = result.Value; return(RedirectToAction("Index", new { orgid = model.OrgId })); } } model.OrgLookUp = LookUps.OrganisationById(model.OrgId); return(View(model)); } catch (Exception ex) { Elmah.ErrorSignal.FromCurrentContext().Raise(ex); TempData["message"] = Settings.Default.GenericExceptionMessage; return(RedirectToAction("Index")); } }
public async Task <ExcelResponseModel> Upload(ExcelUploadModel model) { string tempFileName = GetTemperotyFileName(model.Name); //saving to temprory file; FileHelper.WriteToFile(model.Content, tempFileName); //performing the validation var validationResults = await GetValidationResults(tempFileName); if (validationResults.HttpStatusCode != HttpStatusCode.Accepted) { return(validationResults); } await _storageRepsitory.copy(tempFileName); return(new ExcelResponseModel(null)); }
public ActionResult CreateExcel(int orgId) { try { var model = new ExcelUploadModel { OrgId = orgId, OrgLookUp = LookUps.OrganisationById(orgId) }; return(View(model)); } catch (Exception ex) { // Log with Elmah Elmah.ErrorSignal.FromCurrentContext().Raise(ex); TempData["message"] = Settings.Default.GenericExceptionMessage; return(RedirectToAction("Index")); } }
public async Task <ExcelUploadResponseModel> Validate_Workflow(ExcelUploadModel model) { string tempFileName = GetTemperotyFileName(); var dataTable = await GetExcelTable(model, tempFileName); List <ErrorMessageModel> errorMessageModels = new List <ErrorMessageModel>(); if (dataTable == null) { throw new InvalidOperationException(); } // performing the validation here if (dataTable.Rows.Count == 0) { throw new IndexOutOfRangeException(); } try { WorkflowInvoker workflowInvoker = new WorkflowInvoker(new ValidationSample()); IDictionary <string, object> arguments = new Dictionary <string, object>(); arguments.Add("InputTable", dataTable); var output = workflowInvoker.Invoke(arguments); Thread.Sleep(1000); var excelUploadResponseModel = output["ResponseList"] as ExcelUploadResponseModel; return(excelUploadResponseModel); } catch (Exception ex) { throw ex; } }
public async Task <ExcelUploadResponseModel> Upload(ExcelUploadModel model) { string tempFileName = GetTemperotyFileName(); var dataTable = await GetExcelTable(model, tempFileName); var validationResults = ApplyValidation(dataTable); if (validationResults.HttpStatusCode != StatusCodes.Sucess) { FileHelper.Delete(tempFileName); return(validationResults); } //this is not a recoomended approch, instead use scheduler based approach to push the files into file store var uploadStatus = await _fileStoreProxy.Post(new FileModel { Name = model.Name, FileBytes = model.Content }); if (!uploadStatus.IsSuccessStatusCode) { return(new ExcelUploadResponseModel(new List <ErrorMessageModel>() { new ErrorMessageModel(0) { ErrorMessagees = new List <string>() { "Unable to upload the file to storage" } } } )); } return(new ExcelUploadResponseModel(null)); }
public ActionResult UploadExcelFile(ExcelUploadModel uploadExcelModel, HttpPostedFileBase file) { if (ModelState.IsValid) { // Verify that the user selected a file if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); // store the file inside ~/App_Data/uploads folder var path = Path.Combine(Server.MapPath("~/App_data"), fileName); file.SaveAs(path); try { StringBuilder sb = new StringBuilder(); sb.AppendLine("<?xml version=\"1.0\" ?>"); var excelFile = new ExcelQueryFactory(Convert.ToString(path)); var getData = from a in excelFile.Worksheet("Sheet1") select a; foreach (var a in getData) { uploadExcelModel.AccountID = a["AccountID"]; uploadExcelModel.PhoneNumber = a["PhoneNumber"]; sb.AppendLine("<ExcelUpload>"); sb.AppendLine(" <SendMessage>"); sb.AppendLine(" <AccountID>" + uploadExcelModel.AccountID + "</AccountID>"); sb.AppendLine(" <Description>" + uploadExcelModel.Description + "</Description>"); sb.AppendLine(" <filePath>" + Convert.ToString(path) + "</filePath>"); sb.AppendLine(" <PhoneNumber>" + uploadExcelModel.PhoneNumber + "</PhoneNumber>"); sb.AppendLine(" </SendMessage>"); sb.AppendLine("</ExcelUpload>"); } var excelUploadQuery = _db.Query <string>(";Exec P_AddNewExcelUpload @ExcelUploadFile", new { ExcelUploadFile = sb.ToString() }).SingleOrDefault(); TempData["Success"] = "Excel File qeued Successfuly!"; return(RedirectToAction("UploadExcelFile")); } catch (Exception e) { ViewData["Error"] = e.Message; return(View("UploadExcelFile", uploadExcelModel)); } } else { ViewData["Error"] = "Excel File is Invalid. Try Again!"; return(RedirectToAction("UploadExcelFile")); } } else { var message = string.Join(" | ", ModelState.Values .SelectMany(v => v.Errors) .Select(e => e.ErrorMessage)); ViewData["Error"] = message; return(View("UploadExcelFile", uploadExcelModel)); } }
public async Task <ExcelUploadResponseModelV1 <SallaryModel> > Validate(ExcelUploadModel model) { string tempFileName = GetTemperotyFileName(); var dataTable = await GetExcelTable(model, tempFileName); List <ErrorMessageModelV1 <SallaryModel> > errorMessageModels = new List <ErrorMessageModelV1 <SallaryModel> >(); if (dataTable == null) { throw new InvalidOperationException(); } // performing the validation here if (dataTable.Rows.Count == 0) { throw new IndexOutOfRangeException(); } List <SallaryModel> sallaryModel = dataTable.ToListof <SallaryModel>(); List <Predicate <SallaryModel> > rules = new List <Predicate <SallaryModel> >() { //age group 20 sallary should be greater than 50 (m) => { if (m.Age == 20 && m.Sallary < 50000) { errorMessageModels.Add(new ErrorMessageModelV1 <SallaryModel>(Convert.ToInt32(m.No), m) { ErrorMessages = new List <string>() { "For age group 20 , minilum sallry would be 50000 " } }); return(true); } ; return(false); }, // age group 22 and 23 sallry should not be greater than 50000; (m) => { if ((m.Age == 22 || m.Age == 23) && m.Sallary > 5000) { errorMessageModels.Add(new ErrorMessageModelV1 <SallaryModel>(Convert.ToInt32(m.No), m) { ErrorMessages = new List <string>() { "For age group 20 , minilum sallry should be less than 5000" } }); return(true); } ; return(false); } }; foreach (var rule in rules) { sallaryModel.RemoveAll(rule); } return(new ExcelUploadResponseModelV1 <SallaryModel>(errorMessageModels)); }
public async Task <ExcelResponseModel> Upload([FromBody] ExcelUploadModel model) { return(await _uploadBusinessService.Upload(model)); }