public void ShouldDislayOneDigitForAllPeriods()
        {
            Constants.DAILY_INFO_WEIGHT_DIGITS_PERSIST = 2;
            Constants.DAILY_INFO_WEIGHT_DIGITS_DISPLAY = 1;

            TestHelper.DropDailyInfoCollections();

            var service = ObjectFactory.GetInstance <IDailyInfoService>();


            var info = new Model.DailyInfo.DailyInfo
            {
                UserId = UserId,
                Hotel  = true,
                Weight = 50.23,
            };

            service.InsertDailyInfo(UserId, info, _now);
            ServiceDailyInfo data = service.GetDayInfoViewService(UserId, Constants.NUMBER_OF_DAY_RECORDS, _now);

            Assert.IsTrue(Math.Abs(data.Weight[0] - 50.2) < 0.1);


            ServiceDailyInfoSummaryData sumdata = service.GetDailyInfoServiceViewData(UserId, 2, _now);

            Assert.IsTrue(Math.Abs(sumdata.AverageWeight.Last() - 50.2) < 0.1);

            sumdata = service.GetDailyInfoServiceViewData(UserId, 3, _now);
            Assert.IsTrue(Math.Abs(sumdata.AverageWeight.Last() - 50.2) < 0.1);
        }
        public void InsertDailyInfo(int userId, Model.DailyInfo.DailyInfo info, DateTime?now = null)
        {
            _log.LogInfo("InsertDailyInfo : " + info);
            try
            {
                info.Date   = now ?? Controller.GetServerDateTime();
                info.UserId = userId;

                //async
                GenericError error;
                var          alreadyExisted = UpdateOrInsert(out error, userId, info, info.Date);

                if (error == null)
                {
                    if (alreadyExisted)
                    {
                        // UpdateMonthlyInfoRecord(userId, info, now);
                        //  UpdateYearlyInfoRecord(userId, info, now);
                    }
                    else
                    {
                        //    InsertMonthlyInfoRecord(info);
                        //   InsertYearlyInfoRecord(info);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Controller.LogError(ex);
                throw;
            }
        }
        public void ShouldUpdateDailyInfo()
        {
            DateTime _now    = DateTime.Now;
            int      userId  = 75;
            var      service = ObjectFactory.GetInstance <IDailyInfoService>();

            TestHelper.DropDailyInfoCollections();
            var info = new Model.DailyInfo.DailyInfo
            {
                UserId = userId,
                Hotel  = true,
                Weight = 50,
            };

            service.InsertDailyInfo(userId, info);


            info.Travel = true;
            service.InsertDailyInfo(userId, info);

            GenericError error;

            ServiceDailyInfo dayDailyInfo = service.GetDayInfoViewService(userId, Constants.NUMBER_OF_DAY_RECORDS, _now);

            Assert.IsTrue(dayDailyInfo.Weight.First() == 50);
            Assert.IsTrue(dayDailyInfo.Hotel.First());
            Assert.IsTrue(!dayDailyInfo.Sick.First());
            Assert.IsTrue(dayDailyInfo.Travel.First());
            Assert.IsTrue(!dayDailyInfo.Rest.First());
        }
        public void ShouldContainTrainingDataAverages()
        {
            DateTime now     = DateTime.Now;
            int      userId  = 75;
            var      service = ObjectFactory.GetInstance <IDailyInfoService>();

            TestHelper.DropDailyInfoCollections();

            TestHelper.SeedTrainingData(new List <int> {
                75
            }, now, 1, 1);

            var info = new Model.DailyInfo.DailyInfo
            {
                UserId = userId,
                Hotel  = true,
                Weight = 50
            };


            service.InsertDailyInfo(userId, info, now.AddDays(-7));
            ServiceDailyInfo dailyInfo = service.GetDayInfoViewService(userId, Constants.NUMBER_OF_DAY_RECORDS,
                                                                       now.AddDays(-1));

            Assert.IsTrue(Math.Abs(dailyInfo.Weight[6] - 50) < 0.001);
            Assert.IsTrue(dailyInfo.Hotel[6]);
            Assert.IsTrue(dailyInfo.Sick[6] == false);
            Assert.IsTrue(dailyInfo.Travel[6] == false);
            Assert.IsTrue(dailyInfo.Rest[6] == false);


            Assert.IsTrue(Math.Abs(dailyInfo.TrainingDataPerActivity.ActivitiesDic[Types.TrainingType.Endurance][0].Value - 0.25) < 0.001);
        }
        //private void UpdateYearlyInfoRecord(int userId, Model.DailyInfo.DailyInfo info, DateTime? now)
        //{
        //    GenericError error;
        //    if (now == null) now = Controller.GetServerDateTime();
        //    List<DailyInfoSummaryData> todays =YearlyInfoSummaryDataRepository.GetEntities(out error,
        //                                                          t => t.UserId == userId &&
        //                                                          t.Date.Day == now.Value.Day &&
        //                                                          t.Date.Month == now.Value.Month &&
        //                                                          t.Date.Year == now.Value.Year);
        //    var last = todays.Count > 1 ? todays[2] : RecomputeRecord(out error, userId, t => t.Date.Year == now.Value.Year);
        //    var current = todays.Count == 0 ? new DailyInfoSummaryData() : todays[0];

        //    var monthlyInfoRepository = YearlyInfoSummaryDataRepository as YearlyInfoRepository;
        //    YearlyInfoHandlingTemplate.ComputeFromLastRecord(info, current, last);

        //    if (monthlyInfoRepository != null) monthlyInfoRepository.Update(current);
        //}

        //private void UpdateMonthlyInfoRecord(int userId, Model.DailyInfo.DailyInfo info, DateTime? now)
        //{
        //    GenericError error;
        //    if (now == null) now = Controller.GetServerDateTime();
        //    List<DailyInfoSummaryData> todays =
        //        MonthlyInfoSummaryDataRepository.GetEntities(out error,
        //                                                          t => t.UserId == userId &&
        //                                                          t.Date.Day == now.Value.Day &&
        //                                                          t.Date.Month == now.Value.Month &&
        //                                                          t.Date.Year == now.Value.Year);

        //    var last = todays.Count > 1 ? todays[2] : RecomputeRecord(out error, userId, t => t.Date.Month == now.Value.Month && t.Date.Day.CompareTo(now.Value.Day - 1) <= 0);
        //    var current = todays.Count == 0 ? new DailyInfoSummaryData() : todays[0];

        //    var monthlyInfoRepository = MonthlyInfoSummaryDataRepository as MonthlyInfoRepository;
        //    MontlyDailyInfoHandlingTemplate.ComputeFromLastRecord(info, current, last);

        //    if (monthlyInfoRepository != null) monthlyInfoRepository.Update(current);
        //}

        //private DailyInfoSummaryData RecomputeRecord(out GenericError error, long userId, Func<Model.DailyInfo.DailyInfo, bool> timeComparison)
        //{
        //    var sumInfo = new DailyInfoSummaryData();
        //    var allData = DailyInfoSummaryDataRepository.GetEntities(out error,
        //                                                    t => t.UserId == userId && timeComparison(t));

        //    if (allData.Count == 0) return new DailyInfoSummaryData();
        //    allData.ForEach(info =>
        //     {

        //         sumInfo.WeightSumTotal = sumInfo.WeightSumTotal + info.Weight;
        //         sumInfo.Count = sumInfo.Count + 1;
        //         sumInfo.WeightSumTotal = sumInfo.WeightSumTotal + info.Weight;

        //         sumInfo.AverageWeight = sumInfo.Count == 0 ? 0 : Math.Round(sumInfo.WeightSumTotal / sumInfo.Count, Constants.DAILY_INFO_WEIGHT_DIGITS_PERSIST);

        //         sumInfo.Hotel = sumInfo.Hotel + (info.Hotel ? 1 : 0);
        //         sumInfo.Rest = sumInfo.Rest + (info.Rest ? 1 : 0);
        //         sumInfo.Sick = sumInfo.Sick + (info.Sick ? 1 : 0);
        //         sumInfo.Travel = sumInfo.Travel + (info.Travel ? 1 : 0);

        //     });
        //    return sumInfo;
        //}

        public bool UpdateOrInsert(out GenericError error, int userId, Model.DailyInfo.DailyInfo todayDailyInfo,
                                   DateTime dateTime)
        {
            error = null;

            todayDailyInfo.Weight = Math.Round(todayDailyInfo.Weight, Constants.DAILY_INFO_WEIGHT_DIGITS_PERSIST);

            var dailyInfoRepository = DailyInfoSummaryDataRepository;

            if (dailyInfoRepository != null)
            {
                return(dailyInfoRepository.UpdateOrInsert(todayDailyInfo));
            }
            throw new Exception("DailyInfo Repository does not support Update data");
        }
        public void ShouldGetMonthlyInfoSummaryData()
        {
            var _now    = new DateTime(2013, 4, 23);
            int userId  = 75;
            var service = ObjectFactory.GetInstance <IDailyInfoService>();

            TestHelper.DropDailyInfoCollections();
            var info = new Model.DailyInfo.DailyInfo
            {
                UserId = userId,
                Hotel  = true,
                Weight = 50,
            };

            service.InsertDailyInfo(userId, info, _now);
            ServiceDailyInfo data = service.GetDayInfoViewService(userId, 2, _now);


            Assert.IsTrue(data.Hotel[0]);
            Assert.IsTrue(data.Rest[0] == false);
            Assert.IsTrue(data.Sick[0] == false);

            //Add Second Info
            var info2 = new Model.DailyInfo.DailyInfo
            {
                UserId = userId,
                Hotel  = true,
                Weight = 100,
            };

            service.InsertDailyInfo(userId, info2, _now.AddDays(-1));

            List <DailyInfoSummaryData> dataMonth = service.GetDailyInfoViewData(userId, 3, _now);

            Assert.IsTrue(dataMonth[0].Hotel == 2);
            Assert.IsTrue(Math.Abs(dataMonth[0].AverageWeight - 75) < 0.01);
        }
        public void ShouldGetCorrectDailyInfo()
        {
            var       _now    = new DateTime(2013, 3, 13);
            const int userId  = 50;
            var       service = ObjectFactory.GetInstance <IDailyInfoService>();

            TestHelper.DropDailyInfoCollections();

            var info = new Model.DailyInfo.DailyInfo
            {
                UserId = userId,
                Hotel  = true,
                Weight = 50,
            };

            service.InsertDailyInfo(userId, info, _now.AddDays(-6));
            ServiceDailyInfo dailyInfo = service.GetDayInfoViewService(userId, Constants.NUMBER_OF_DAY_RECORDS, _now);

            Assert.IsTrue(Math.Abs(dailyInfo.Weight[6] - 50) < 0.001);
            Assert.IsTrue(dailyInfo.Hotel[6]);
            Assert.IsTrue(dailyInfo.Sick[6] == false);
            Assert.IsTrue(dailyInfo.Travel[6] == false);
            Assert.IsTrue(dailyInfo.Rest[6] == false);
        }
 private void InsertMonthlyInfoRecord(Model.DailyInfo.DailyInfo info)
 {
     MontlyDailyInfoHandlingTemplate.UpdateEntityRecords(info);
 }
 private void InsertYearlyInfoRecord(Model.DailyInfo.DailyInfo info)
 {
     YearlyInfoHandlingTemplate.UpdateEntityRecords(info);
 }