private double GetUserWaterConsumtionSent(User user, int month, int year) { var thisMonthLastSent = user.WaterConsumptions .Where(x => x.CreationDate.Value.Month == month && x.CreationDate.Value.Year == year) .OrderByDescending(x => x.CreationDate).FirstOrDefault(); var precedentMonthLastSent = user.WaterConsumptions .Where(x => x.CreationDate.Value.Month == (month == 1 ? 12 : month - 1) && x.CreationDate.Value.Year == (month == 1 ? year - 1: year)) .OrderByDescending(x => x.CreationDate) .FirstOrDefault(); var lastSent = user.WaterConsumptions .OrderByDescending(x => x.CreationDate) .FirstOrDefault(); if (thisMonthLastSent != null) { double total = precedentMonthLastSent != null ? ( (thisMonthLastSent.BathroomUnits - precedentMonthLastSent.BathroomUnits) + (thisMonthLastSent.KitchenUnits - precedentMonthLastSent.KitchenUnits) ) : thisMonthLastSent.KitchenUnits + thisMonthLastSent.BathroomUnits; return(total); } else { var date = new DateTime(year, month, 28); _waterConsumptionRepository.Insert(new WaterConsumption { UserId = user.UniqueId, KitchenUnits = lastSent != null ? lastSent.KitchenUnits : 0, BathroomUnits = lastSent != null ? lastSent.BathroomUnits : 0, CreationDate = date }); return(GetUserWaterConsumtionSent(user, month, year)); } }
public WaterConsumption Insert(WaterConsumption consumption) { return(_consumptionRepository.Insert(consumption)); }