コード例 #1
0
        public async Task <VouchersUploadModel> GenerateVouchers(VouchersUploadModel vouchersUploadModel)
        {
            SunwingVouchersEntities context         = new SunwingVouchersEntities();
            IssuanceManager         issuanceManager = new IssuanceManager(context);

            #region test data
            //payLoad.Token = "";
            vouchersUploadModel.Token = "130E3452920015FEB7EE83AD7B1E80850495C4A0DDA137498E39783DC7355453";
            Byte[] bytes = System.IO.File.ReadAllBytes(System.Web.Hosting.HostingEnvironment.MapPath(@"~/Content/Template/export.csv"));
            String base64encodedstring = Convert.ToBase64String(bytes);
            #endregion

            bool isTokenValid = issuanceManager.IsAddNewVoucherPayloadValid(vouchersUploadModel) &&
                                issuanceManager.IsValidToken(vouchersUploadModel.Token);

            // add new voucher upload
            if (isTokenValid)
            {
                // save this file to database first step when api will hit the data with base64 string
                // vouchersUploadModel.UploadContent = fileLocation;
                vouchersUploadModel.CsvString      = base64encodedstring;
                vouchersUploadModel.CreatedOn      = DateTime.Now;
                vouchersUploadModel.Status         = (int)VouchersUploadStatus.NotProcessed;
                vouchersUploadModel.PayloadContent = JsonConvert.SerializeObject(vouchersUploadModel);// save all payload
                issuanceManager.AddVouchersUpload(vouchersUploadModel);
            }
            return(vouchersUploadModel);
        }
コード例 #2
0
 /// <summary>
 /// add new upload data in database for further iteration
 /// </summary>
 /// <param name="vouchersUploadModel"></param>
 /// <returns></returns>
 public bool AddVouchersUpload(VouchersUploadModel vouchersUploadModel)
 {
     try
     {
         VouchersUpload vouchersUpload = new VouchersUpload();
         // update
         if (vouchersUploadModel.Id > 0)
         {
             vouchersUpload = _dbContext.VouchersUploads.Where(x => x.Id == vouchersUploadModel.Id &&
                                                               !x.IsDeleted).FirstOrDefault();
             if (vouchersUpload != null)
             {
                 vouchersUpload.UploadContent = vouchersUploadModel.UploadContent;
                 vouchersUpload.Status        = vouchersUploadModel.Status;
                 vouchersUpload.ModefiedOn    = System.DateTime.Now;
                 //number of records that parsed successfully
                 vouchersUpload.SuccessfullyProcessed    = vouchersUploadModel.SuccessfullyProcessed;
                 vouchersUpload.NotSuccessfullyProcessed = vouchersUploadModel.NotSuccessfullyProcessed;
                 vouchersUpload.TotalProcessed           = vouchersUploadModel.TotalProcessed;
             }
         }
         else // add
         {
             // UploadContent = vouchersUploadModel.UploadContent,
             vouchersUpload.Status         = vouchersUploadModel.Status;
             vouchersUpload.CreatedOn      = vouchersUploadModel.CreatedOn;
             vouchersUpload.PayloadContent = vouchersUploadModel.PayloadContent;
             vouchersUpload.CsvString      = vouchersUploadModel.CsvString;
             //Base= vouchersUploadModel.Base64Content
             _dbContext.VouchersUploads.Add(vouchersUpload);
         }
         _dbContext.SaveChanges();
         // return this
         vouchersUploadModel.Id = vouchersUpload.Id;
         return(true);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
コード例 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileLocation"></param>
        /// <param name="relativeExcelPath"></param>
        /// <param name="IsFirstRowAsColumnNames"></param>
        /// <returns></returns>
        public async Task <bool> SaveProcessedToDb(VouchersUploadModel vouchersUploadModel)
        {
            int successCount    = 0; // coupons successfully inserted
            int notSuccessCount = 0; // coupons not successfully inserted
            int totalProcessed  = 0; // to track the current row of the sheet been iterated

            SunwingVouchersEntities context         = new SunwingVouchersEntities();
            IssuanceManager         issuanceManager = new IssuanceManager(context);

            // added for testing, remove this line for production
            vouchersUploadModel.IsFirstRowAsColumnNames = true;
            List <string> ErrorMassage = new List <string>();

            try
            {
                using (var reader = new StreamReader(vouchersUploadModel.UploadContent))
                {
                    List <string> csvRows = new List <string>();
                    while (!reader.EndOfStream)
                    {
                        var line   = reader.ReadLine();
                        var values = line.Split(';');
                        csvRows.Add(values[0]);
                    }
                    // if first row is header remove that
                    if (vouchersUploadModel.IsFirstRowAsColumnNames && csvRows.Count > 1)
                    {
                        csvRows.RemoveAt(0);
                    }
                    // check for validation for each row
                    //read row data one by one
                    foreach (var rowItem in csvRows)
                    {
                        // validate each row
                        // and save the status to database
                        // with status
                        var validationData = CsvRowValidate(rowItem);
                        ProcessVouchersUploadModel processVouchersUploadModel = new ProcessVouchersUploadModel();
                        processVouchersUploadModel.UploadId = vouchersUploadModel.Id;
                        // if no error
                        if (!validationData.Item1)
                        {
                            var voucher = await AddNewVoucher(validationData.Item3);

                            if (voucher.IsSuccess)
                            {
                                // add the data of iterated csv row into db
                                // add the column Content For holding all the processed Data  here change
                                processVouchersUploadModel.CsvContent = JsonConvert.SerializeObject(voucher);
                                processVouchersUploadModel.Status     = (int)ProcessVouchersUploadStatus.Success;
                                successCount++;
                            }
                            else
                            {
                                processVouchersUploadModel.Status   = (int)ProcessVouchersUploadStatus.NotSuccess;
                                processVouchersUploadModel.ErrorMsg = voucher.ErrorMsg;
                                notSuccessCount++;
                            }
                            //save the status in ProcessVouchersUpload  with status success
                        }
                        else
                        {
                            //save the status in ProcessVouchersUpload with status not success
                            processVouchersUploadModel.Status   = (int)ProcessVouchersUploadStatus.NotSuccess;
                            processVouchersUploadModel.ErrorMsg = "Row No " + totalProcessed + " process with errors: " + string.Join(", ", validationData.Item2
                                                                                                                                      .Select(p => p.ToString()));// validationData.Item2.ToString();
                            notSuccessCount++;
                        }
                        issuanceManager.AddProcessVouchersUpload(processVouchersUploadModel);
                        totalProcessed++;
                    }
                }
                //
                vouchersUploadModel.Status = (int)VouchersUploadStatus.IssuedWithSuccess;
                vouchersUploadModel.SuccessfullyProcessed    = successCount;
                vouchersUploadModel.NotSuccessfullyProcessed = notSuccessCount;
                vouchersUploadModel.TotalProcessed           = totalProcessed;
                issuanceManager.AddVouchersUpload(vouchersUploadModel);
                return(true);
            }
            catch (Exception ex)
            {
                // add total process and unprocessed
                vouchersUploadModel.Status = (int)VouchersUploadStatus.IssuedWithNotSucces;
                issuanceManager.AddVouchersUpload(vouchersUploadModel);
                return(false);
            }
        }