Пример #1
0
        public SessionViewModel Get(int sessionId)
        {
            var result = _thermometerContext.Sessions
                         .Where(session => session.Id == sessionId)
                         .Include(i => i.ProbeReadings)
                         .FirstOrDefault();

            var sessionViewModel = new SessionViewModel();

            if (result != null)
            {
                sessionViewModel.Id            = result.Id;
                sessionViewModel.ThermometerId = result.ThermometerId;
                sessionViewModel.ProbeReadings = new List <ProbeReadingViewModel>();

                foreach (var probeReading in result.ProbeReadings)
                {
                    var probeReadingViewModel = new ProbeReadingViewModel()
                    {
                        Id          = probeReading.Id,
                        Temperature = probeReading.Temperature,
                        ReadingTime = probeReading.ReadingTime.ToString("T"),
                        ProbeId     = probeReading.ProbeId,
                        SessionId   = probeReading.SessionId
                    };

                    sessionViewModel.ProbeReadings.Add(probeReadingViewModel);
                }
            }

            return(sessionViewModel);
        }
        public IList <ProbeReadingViewModel> Get(int sessionId, int interval)
        {
            var results = _thermometerContext.ProbeReadings
                          .Where(pr => pr.SessionId == sessionId &&
                                 pr.ReadingTime.Second % interval == 0).ToList();

            var probeReadings = new List <ProbeReadingViewModel>();

            if (results != null)
            {
                foreach (var probeReading in results)
                {
                    var probeReadingViewModel = new ProbeReadingViewModel()
                    {
                        Id          = probeReading.Id,
                        Temperature = probeReading.Temperature,
                        ReadingTime = probeReading.ReadingTime.ToString("T"),
                        ProbeId     = probeReading.ProbeId,
                        SessionId   = probeReading.SessionId
                    };

                    probeReadings.Add(probeReadingViewModel);
                }
            }

            return(probeReadings);
        }
        public void Post([FromBody] ProbeReadingViewModel probeReadingViewModel)
        {
            var probeReading = new ProbeReading()
            {
                Temperature = probeReadingViewModel.Temperature,
                ReadingTime = DateTime.Parse(probeReadingViewModel.ReadingTime),
                ProbeId     = probeReadingViewModel.ProbeId,
                SessionId   = probeReadingViewModel.SessionId
            };

            _thermometerContext.ProbeReadings.Add(probeReading);
            _thermometerContext.SaveChanges();
        }