public Exercise[] GetExercises()
 {
     using (var db = new GainTrackerContext(ACTIVE_CONNECTION))
     {
         return(db.Exercises.ToArray());
     }
 }
 public void AddStatistic(CreateStatisticModel model)
 {
     using (var db = new GainTrackerContext(ACTIVE_CONNECTION))
     {
         db.Statistics.Add(Mapper.Map <Statistic>(model));
         db.SaveChanges();
     }
 }
 public void AddExercise(CreateExerciseViewModel model)
 {
     using (var db = new GainTrackerContext(ACTIVE_CONNECTION))
     {
         db.Exercises.Add(Mapper.Map <Exercise>(model));
         db.SaveChanges();
     }
 }
 public void AddDataPoint(CreateDataPointViewModel model)
 {
     using (var db = new GainTrackerContext(ACTIVE_CONNECTION))
     {
         db.DataPoints.Add(Mapper.Map <DataPoint>(model));
         db.SaveChanges();
     }
 }
 public void AddTrackedData(CreateTrackedDataViewModel model)
 {
     using (var db = new GainTrackerContext(ACTIVE_CONNECTION))
     {
         db.TrackedData.Add(Mapper.Map <TrackedData>(model));
         db.SaveChanges();
     }
 }
        public bool CheckIP(string address)
        {
            using (var db = new GainTrackerContext(ACTIVE_CONNECTION))
            {
                if (db.Statistics.FirstOrDefault(s => s.IPAddress == address) != null)
                {
                    return(true);
                }

                return(false);
            }
        }
        public bool CheckLogin(string address)
        {
            using (var db = new GainTrackerContext(ACTIVE_CONNECTION))
            {
                if (db.Statistics.FirstOrDefault(
                        s => s.Type == (int)StatisticsHelper.StatisticTypes.Login && s.IPAddress != address) == null)
                {
                    return(true);
                }

                return(false);
            }
        }
        public StatisticListViewModel GetStatistics()
        {
            using (var db = new GainTrackerContext(ACTIVE_CONNECTION))
            {
                var em = db.Statistics.OrderByDescending(s => s.Time).ToArray();
                StatisticListViewModel vm = new StatisticListViewModel();
                vm.Rows = new List <StatisticListRowViewModel>();

                foreach (var item in em)
                {
                    if (String.Equals(item.IPAddress, "::1"))
                    {
                        continue;
                    }

                    var today = em.Where(v => v.Time.Date == item.Time.Date).ToArray();

                    if (vm.Rows.Count == 0 || vm.Rows.Last().Date != item.Time.Date)
                    {
                        var vmItem = new StatisticListRowViewModel
                        {
                            Date            = item.Time.Date,
                            Visitors        = today.Where(v => v.Type == (int)StatisticsHelper.StatisticTypes.Visitor || v.Type == (int)StatisticsHelper.StatisticTypes.UniqueVisitor).Count(),
                            UniqueVisitors  = today.Where(v => v.Type == (int)StatisticsHelper.StatisticTypes.UniqueVisitor).Count(),
                            RegisteredUsers = today.Where(v => v.Type == (int)StatisticsHelper.StatisticTypes.Register).Count(),
                            AddedData       = today.Where(v => v.Type == (int)StatisticsHelper.StatisticTypes.AddedData).Count(),
                            Items           = Mapper.Map <StatisticViewModel[]>(em.Where(v => v.Time.Date == item.Time.Date).OrderByDescending(v => v.Time).ToArray())
                        };

                        vmItem.Initialize();

                        vm.Rows.Add(vmItem);
                    }
                }

                return(vm);
            }
        }
        public ViewModels.ProfileIndexViewModel GetProfileViewModel(string userName)
        {
            using (var db = new GainTrackerContext(ACTIVE_CONNECTION))
            {
                var vm = new ProfileIndexViewModel
                {
                    TrackedData = db.TrackedData.Where(u => u.UserName == userName).ToList()
                };

                foreach (var item in vm.TrackedData)
                {
                    item.DataPoints      = db.DataPoints.Where(d => d.TrackedDataId == item.Id).ToArray();
                    item.DataPointValues = item.DataPoints.Select(d => d.Value).ToArray();
                }

                vm.CreateTrackedViewModel = new CreateTrackedDataViewModel
                {
                    UserName = Membership.GetUser().UserName
                };

                return(vm);
            }
        }