public async Task AddNewPoint()
        {
            var AnalyticalDataRepository = new AnalyticalDataRepository(_appDbContext);

            AnalyticalDataPoint analyticalDataPoint = new AnalyticalDataPoint
            {
                DateTime = DateTime.Now,
                point    = 333.444m
            };

            await AnalyticalDataRepository.AddNewEntity(analyticalDataPoint).ConfigureAwait(false);

            var point = await AnalyticalDataRepository.GetDataAllAsync();//.Result.FirstOrDefault<AnalyticalDataPoint>();

            Assert.AreEqual(analyticalDataPoint.point, point.Last().point);
        }
        private void InitContext()
        {
            var builder = new DbContextOptionsBuilder <AppDbContext>().UseInMemoryDatabase("AppDbContextDB");

            _appDbContext = new AppDbContext(builder.Options);

            AnalyticalDataPoint analyticalDataPoint = new AnalyticalDataPoint
            {
                DateTime = DateTime.Now,
                point    = 123.456m
            };

            if (!_appDbContext.AnalyticalData.Any())
            {
                _appDbContext.AnalyticalData.Add(analyticalDataPoint);
                _appDbContext.AnalyticalData.Add(analyticalDataPoint);
                _appDbContext.SaveChangesAsync();
            }
        }
Ejemplo n.º 3
0
        public async Task <Response <FileLoadResults> > Handle(UploadDataCommand request, CancellationToken cancellationToken)
        {
            try
            {
                FileLoadResults fileLoadResults = new FileLoadResults();

                if (!File.Exists(request._filePath))
                {
                    fileLoadResults.fileLoadSuccess = false;
                    fileLoadResults.loadingError    = "File does not exist.";
                    throw new Exception("File does not exist.");
                }
                else
                {
                    int    counter = 0;
                    string dataPoint;
                    System.IO.StreamReader file = new System.IO.StreamReader(request._filePath);

                    while ((dataPoint = file.ReadLine()) != null)
                    {
                        if (counter == 0)
                        {
                            //Skip line as expected to be Data label
                            counter++;
                            continue;
                        }

                        //TODo Move this logic into a seperate class
                        String[] data       = dataPoint.Split(',');
                        string   dateFormat = "dd/MM/yyyy HH:mm";
                        DateTime parsedDate;
                        decimal  point = 0;

                        if (data[0].Length < 16)
                        {
                            data[0] = data[0] + " 00:00";
                        }

                        if (!DateTime.TryParseExact(data[0], dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate))
                        {
                            fileLoadResults.fileLoadSuccess = false;
                            fileLoadResults.loadingError    = "Date in incorrect format. Should be dd/mm/YYYY HH:MM.";
                            throw new Exception("Date in incorrect format. Should be dd/mm/YYYY HH:MM.");
                        }

                        if (data[1].Contains("."))
                        {
                            data[1] = data[1].Replace(".", ",");
                        }

                        if (!decimal.TryParse(data[1], out point))
                        {
                            fileLoadResults.fileLoadSuccess = false;
                            fileLoadResults.loadingError    = "Point in incorrect decimal format.";
                            throw new Exception("Point in incorrect decimal format.");
                        }

                        AnalyticalDataPoint analyticalDataPoint = new AnalyticalDataPoint
                        {
                            DateTime = parsedDate,
                            point    = point
                        };

                        await _analyticalDataRepository.AddNewEntity(analyticalDataPoint);

                        _points.Add(analyticalDataPoint);
                        counter++;
                    }

                    fileLoadResults.fileLoadSuccess = true;
                    CalculateAnalytics();
                }

                return(new Response <FileLoadResults>(true, "Data succesfully loaded.", fileLoadResults));
            }
            catch (Exception ex)
            {
                FileLoadResults fileLoadResults = new FileLoadResults();
                fileLoadResults.fileLoadSuccess = false;
                fileLoadResults.loadingError    = ex.Message;
                return(new Response <FileLoadResults>(false, ex.Message, fileLoadResults));
            }
        }