예제 #1
0
        public static WaterMeter Add(VesiPortalDbContext context, WaterMeter waterMeter)
        {
            try
            {
                if (waterMeter.Guid == Guid.Empty || waterMeter.Guid == null)
                {
                    waterMeter.Guid = Guid.NewGuid();
                }
                waterMeter.Timestamp = BitConverter.GetBytes(DateTime.Now.Ticks);

                context.WaterMeters.Create();
                context.WaterMeters.Add(waterMeter);
                context.SaveChanges();

                return(waterMeter);
            }
            catch
            {
                throw;
            }
        }
예제 #2
0
        public static WaterMeter Get(VesiPortalDbContext context, Guid guid)
        {
            WaterMeter waterMeter = context.WaterMeters.Where(x => x.Guid == guid).FirstOrDefault();

            return(waterMeter);
        }
예제 #3
0
        /// <summary>
        /// Get water meter readings from database
        /// </summary>
        /// <param name="context">VesiPortalDB context</param>
        /// <param name="waterMeter">Water meter for query</param>
        /// <param name="startDate">Optional: if value give, only includes readings where reading date is later or equal to this date</param>
        /// <param name="endDate">Optional: if value give, only includes readings where reading date is earlier or equal to this date</param>
        /// <returns></returns>
        public static IQueryable <RegisteredWaterMeterReading> GetRegisteredWaterMeterReadings(VesiPortalDbContext context, WaterMeter waterMeter, DateTime?startDate = null, DateTime?endDate = null)
        {
            try
            {
                IQueryable <RegisteredWaterMeterReading> waterMeterReadings = context.WaterMeterReadings.OfType <RegisteredWaterMeterReading>()
                                                                              .Where(x => x.WaterMeterGuid == waterMeter.Guid);

                if (startDate.HasValue)
                {
                    waterMeterReadings = waterMeterReadings.Where(x => x.ReadingDate >= startDate.Value);
                }

                if (endDate.HasValue)
                {
                    waterMeterReadings = waterMeterReadings.Where(x => x.ReadingDate <= endDate.Value);
                }

                return(waterMeterReadings);
            }
            catch
            {
                throw;
            }
        }