public async Task <ActionResult> ExcelFile(IList <IFormFile> excelFileName)
        {
            string[] validationSet     = configuration.GetSection("ExcelFileValidation:mandatoryFields").GetChildren().Select(val => val.Value).ToArray();
            string[] cellValidationSet = configuration.GetSection("ExcelFileValidation:cellValidationFields").GetChildren().Select(val => val.Value).ToArray();
            string   addressBookEnable = configuration["AddressBook:Enable"];

            ShipmentDataResponse shipmentDataResponse = new ShipmentDataResponse();

            try
            {
                int userId = Convert.ToInt32(HttpContext.User.Claims.FirstOrDefault(x => x.Type == JwtConstant.UserId).Value);
                ShipmentDataResponse result = null;
                //string response = string.Empty;
                if (excelFileName != null)
                {
                    //var uploads = Path.Combine(_hostingEnvironment.WebRootPath, "uploads");
                    foreach (var file in excelFileName)
                    {
                        if (file.Length > 0)
                        {
                            //string paths = hostingEnvironment.WebRootPath;

                            var filePath = Path.Combine(_hostingEnvironment.WebRootPath, file.FileName);

                            //var filePath = Path.Combine(@"D:\UserExcels", file.FileName);
                            using (var fileStream = new FileStream(filePath, FileMode.Create))
                            {
                                //FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read);
                                //response = new ExcelExtension().Test(filePath);
                                await file.CopyToAsync(fileStream);
                            }


                            ExcelExtensionReponse excelExtensionReponse =
                                new ExcelExtension()
                                .Test(
                                    filePath,
                                    configuration.GetSection("ExcelFileValidation:mandatoryFields").GetChildren().Select(val => val.Value).ToArray(),
                                    configuration.GetSection("ColumnValidation:regexList").GetChildren().Select(val => val.Value).ToArray(),
                                    configuration.GetSection("ColumnValidation:columnLengths").GetChildren().Select(val => val.Value).ToArray());
                            if (excelExtensionReponse.success)
                            {
                                var excelDataObject2 = JsonConvert.DeserializeObject <List <ExcelDataObject> >(excelExtensionReponse.ExcelExtensionReponseData);
                                WorkflowController   workflowController = new WorkflowController(this._hostingEnvironment, this._context, this._addressBookService, this._entityValidationService);
                                WorkflowDataResponse response           = ((WorkflowDataResponse)((ObjectResult)(workflowController.CreateWorkflow(file, userId)).Result).Value);
                                _workflowID = response.Workflow.ID;
                                result      = _shipmentService.CreateShipments(excelDataObject2, _workflowID, addressBookEnable, out int?workflowStatus);
                                if (result.Success)
                                {
                                    shipmentDataResponse.Success   = true;
                                    shipmentDataResponse.Shipments = result.Shipments;
                                    WorkflowDataRequest workflowDataRequest = new WorkflowDataRequest();
                                    workflowDataRequest.ID         = _workflowID;
                                    workflowDataRequest.WFL_STA_TE = workflowStatus;
                                    _workflowService.UpdateWorkflowStatusById(workflowDataRequest);
                                }
                                else
                                {
                                    shipmentDataResponse.Success = false;
                                    shipmentDataResponse.OperationExceptionMsg = result.OperationExceptionMsg;
                                    WorkflowService workflowService = new WorkflowService(_context, _addressBookService, _entityValidationService);
                                    workflowService.DeleteWorkflowById(_workflowID);
                                }
                            }
                            else
                            {
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                                Task.Run(() => iCustomLog.AddLogEntry(new UPS.DataObjects.LogData.LogDataModel()
                                {
                                    apiType        = Enum.GetName(typeof(UPS.DataObjects.LogData.APITypes), 7),
                                    dateTime       = System.DateTime.Now,
                                    LogInformation = new UPS.DataObjects.LogData.LogInformation()
                                    {
                                        LogException = string.Empty,
                                        LogRequest   = "Excel Uploaded",
                                        LogResponse  = JsonConvert.SerializeObject(excelExtensionReponse)
                                    }
                                }));
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                                return(Ok(excelExtensionReponse));
                            }
                        }
                    }
                }

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                Task.Run(() => iCustomLog.AddLogEntry(new UPS.DataObjects.LogData.LogDataModel()
                {
                    apiTypes       = UPS.DataObjects.LogData.APITypes.ExcelUpload,
                    apiType        = Enum.GetName(typeof(UPS.DataObjects.LogData.APITypes), 7),
                    dateTime       = System.DateTime.Now,
                    LogInformation = new UPS.DataObjects.LogData.LogInformation()
                    {
                        LogException = null,
                        LogRequest   = "Excel Uploaded",
                        LogResponse  = JsonConvert.SerializeObject(shipmentDataResponse)
                    }
                }));
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

                return(Ok(shipmentDataResponse));
            }
            catch (Exception ex)
            {
                // new AuditEventEntry.WriteEntry(new Exception(ex.Message));
                AuditEventEntry.WriteEntry(ex);
                return(Ok(shipmentDataResponse.OperationExceptionMsg = ex.Message));
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> ExcelFile(IList <IFormFile> excelFileName, int Emp_Id)
        {
            ShipmentDataResponse shipmentDataResponse = new ShipmentDataResponse();

            try
            {
                ShipmentDataResponse result = null;
                //string response = string.Empty;
                if (excelFileName != null)
                {
                    //var uploads = Path.Combine(_hostingEnvironment.WebRootPath, "uploads");
                    foreach (var file in excelFileName)
                    {
                        if (file.Length > 0)
                        {
                            //string paths = hostingEnvironment.WebRootPath;

                            var filePath = Path.Combine(hostingEnvironment.WebRootPath, file.FileName);

                            //var filePath = Path.Combine(@"D:\UserExcels", file.FileName);
                            using (var fileStream = new FileStream(filePath, FileMode.Create))
                            {
                                //FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read);
                                //response = new ExcelExtension().Test(filePath);
                                await file.CopyToAsync(fileStream);
                            }


                            ExcelExtensionReponse excelExtensionReponse = new ExcelExtension().Test(filePath);
                            if (excelExtensionReponse.success)
                            {
                                var excelDataObject2 = JsonConvert.DeserializeObject <List <ExcelDataObject> >(excelExtensionReponse.ExcelExtensionReponseData);
                                WorkflowController   workflowController = new WorkflowController();
                                WorkflowDataResponse response           = ((WorkflowDataResponse)((ObjectResult)(workflowController.CreateWorkflow(file, Emp_Id)).Result).Value);
                                _workflowID = response.Workflow.ID;
                                result      = this.CreateShipments(excelDataObject2, _workflowID);
                                if (result.Success)
                                {
                                    shipmentDataResponse.Success   = true;
                                    shipmentDataResponse.Shipments = result.Shipments;
                                }
                                else
                                {
                                    shipmentDataResponse.Success = false;
                                    shipmentDataResponse.OperationExceptionMsg = result.OperationExceptionMsg;
                                    WorkflowService workflowService = new WorkflowService();
                                    workflowService.DeleteWorkflowById(_workflowID);
                                }
                            }
                            else
                            {
                                return(Ok(excelExtensionReponse));
                            }
                        }
                    }
                }

                return(Ok(shipmentDataResponse));
            }
            catch (Exception ex)
            {
                AuditEventEntry.WriteEntry(new Exception(ex.Message));
                return(Ok(shipmentDataResponse.OperationExceptionMsg = ex.Message));
            }
        }