public async Task <ConsumptionDto> Handle(
            UpdateApplianceUsageCommand command,
            CancellationToken cancellationToken)
        {
            // var applianceQuery = from a in _context.Appliances
            //     where a.Id == command.ApplianceId
            //     select a;

            // var appliance = await applianceQuery.SingleOrDefaultAsync(cancellationToken);

            var consumptionQuery = _context.Consumptions
                                   .Where(c => c.Id == command.ConsumptionId)
                                   .Include(c => c.ApplianceUsages)
                                   .ThenInclude(au => au.ApplianceUsageSchedule)
                                   // .ThenInclude(aus => aus.UsageIntervals)
                                   // .ThenInclude(au => au.Appliance)
            ;

            var consumption = await
                              consumptionQuery.SingleOrDefaultAsync(cancellationToken);

            var au = consumption.ApplianceUsages
                     .Where(au => au.Id == command.ApplianceUsageId)
                     .First();

            // Some logging
            Console.WriteLine($"\nApplianceUsageId is {au.Id}\n");
            Console.WriteLine($"Num usage intervals: {command.UsageIntervals.Count}\n");

            au.ApplianceUsageSchedule.ClearUsageIntervals();
            foreach (var ui in command.UsageIntervals)
            {
                var ti = ui.TimeInterval;
                var startTI = ui.TimeInterval.Start;
                var startHr = startTI.Hours; var startMin = startTI.Minutes;
                var endTI = ui.TimeInterval.End;
                var endHr = endTI.Hours; var endMin = endTI.Minutes;
                au.ApplianceUsageSchedule.AddUsageInterval(startHr, startMin,
                                                           endHr, endMin, ui.UsageKind);
            }

            au.SetQuantity(command.Quantity);
            au.SetPowerConsumption(command.PowerConsumption);
            consumption.Recalculate();

            await _context.SaveChangesAsync(cancellationToken);

            var consumptionDto = _mapper.Map <ConsumptionDto>(consumption);

            return(consumptionDto);
        }
Пример #2
0
        public async Task <ApplianceUsageDto> Handle(
            AddApplianceUsageCommand command,
            CancellationToken cancellationToken)
        {
            var applianceQuery = from a in _context.Appliances
                                 where a.Id == command.ApplianceId
                                 select a;

            var appliance = await applianceQuery.SingleOrDefaultAsync(cancellationToken);

            var siteQuery = _context.Sites
                            .Where(s => s.Id == command.ConsumptionId)
                            .Include(s => s.Consumption)
                            .ThenInclude(c => c.ApplianceUsages)
                            .ThenInclude(au => au.Appliance)
            ;

            var site = await
                       siteQuery.SingleOrDefaultAsync(cancellationToken);

            // Some logging
            Console.WriteLine($"\nSiteId is {site.Id}\n");
            Console.WriteLine($"\nConsumptionId is {site.Consumption.Id}\n");

            site.Consumption.AddApplianceUsage(appliance);
            var au = site.Consumption.ApplianceUsages.Last();

            au.SetQuantity(command.Quantity);
            au.SetPowerConsumption(command.PowerConsumption);

            _context.ApplianceUsages.Add(au);

            await _context.SaveChangesAsync(cancellationToken);

            var auDto = _mapper.Map <ApplianceUsageDto>(au);

            return(auDto);
        }