Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="meterReadingsImportRequestModel"></param>
        public async Task <ServiceResponse <MeterReadingImportResponseModel> > LoadReadings(MeterReadingsImportRequestModel meterReadingsImportRequestModel)
        {
            try
            {
                using var reader = new StreamReader(meterReadingsImportRequestModel.CsvFile.OpenReadStream());
                using var csv    = new CsvReader(reader, CultureInfo.GetCultureInfo("en-GB"));

                var validList     = new List <MeterReadingImportModel>();
                var inValidList   = new List <InvalidMeterReadingImportModel>();
                var duplicateList = new List <MeterReadingImportModel>();
                var isRecordBad   = false;

                while (csv.Read())
                {
                    var record = new MeterReadingImportModel();
                    csv.Configuration.RegisterClassMap <MeterReadingImportMap>();

                    try
                    {
                        record = csv.GetRecord <MeterReadingImportModel>();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        isRecordBad = true;
                        csv.Configuration.RegisterClassMap <InvalidMeterReadingMap>();
                        inValidList.Add(csv.GetRecord <InvalidMeterReadingImportModel>());
                    }

                    if (!isRecordBad)
                    {
                        validList.Add(record);
                    }

                    isRecordBad = false;
                }

                //TODO: Fix logic for calculating Added Record & duplicate record counts.

                var readings    = _mapper.Map <List <MeterReading> >(validList);
                var newReadings = new List <MeterReading>();
                var accounts    = await _context.Accounts.Select(s => s.AccountId).ToListAsync();

                foreach (var reading in readings.Where(w =>
                                                       !_context.MeterReadings.Any(m =>
                                                                                   m.AccountId == w.AccountId && m.ReadDate == w.ReadDate)).Where(w => accounts.Contains(w.AccountId)))
                {
                    newReadings.Add(reading);
                }

                if (newReadings.Any())
                {
                    _context.MeterReadings.AddRange(newReadings);

                    if (await _context.SaveChangesAsync() > 0)
                    {
                        var diff = validList.Count - newReadings.Count;

                        return(new ServiceResponse <MeterReadingImportResponseModel>
                        {
                            Data = new MeterReadingImportResponseModel
                            {
                                Imports = newReadings,
                                SuccessfulImports = newReadings.Count(),
                                FailedImports = inValidList.Count(),
                                DuplicateImports = diff
                            }
                        });
                    }
                }

                if (validList.Any())
                {
                    return(new ServiceResponse <MeterReadingImportResponseModel>
                    {
                        Data = new MeterReadingImportResponseModel
                        {
                            Imports = new List <MeterReading>(),
                            SuccessfulImports = 0,
                            FailedImports = validList.Count(),
                            DuplicateImports = validList.Count()
                        }
                    });
                }

                return(new ServiceResponse <MeterReadingImportResponseModel>
                {
                    ErrorCode = HttpStatusCode.InternalServerError,
                    ErrorDescription = "Internal Server Error"
                });
            }
            catch (Exception e)
            {
                return(new ServiceResponse <MeterReadingImportResponseModel>
                {
                    ErrorCode = HttpStatusCode.InternalServerError,
                    ErrorDescription = "Internal Server Error"
                });
            }
        }
        public async Task <ActionResult <MeterReadingImportResponseModel> > ImportCsvFile([FromForm] MeterReadingsImportRequestModel model)
        {
            var response = await _meterReadingService.LoadReadings(model);

            if (response.IsError)
            {
                return(StatusCode((int)response.ErrorCode, response.ErrorDescription));
            }

            return(Ok(response.Data));
        }