public static int GetCount()
 {
     using (var db = new dbContainer())
     {
         var models = from i in db.tblDevices
                      select i;
         return models.Count();
     }
 }
 public static void Edit(Customer model)
 {
     using (var db = new dbContainer())
     {
         var customer = Mapper.Map<Customer, tblCustomers>(model);
         db.Entry(customer).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
     }
 }
 public static void Create(Application model)
 {
     using (var db = new dbContainer())
     {
         var application = Mapper.Map<Application, tblApplications>(model);
         db.tblApplications.Add(application);
         db.SaveChanges();
     }
 }
 public static int GetCount()
 {
     using (var db = new dbContainer())
     {
         var count = from i in db.tblApplications
                      select i;
         return count.Count();
     }
 }
 public static List<Device> Get()
 {
     using (var db = new dbContainer())
     {
         var models = from i in db.tblDevices
                      select i;
        return Mapper.Map<List<tblDevices>, List<Device>>(models.ToList());;
     }
 }
        public static List<Customer> GetPagination(int itemPerPage = 0, int pageNumber = 1)
        {

            using (var db = new dbContainer())
            {
                var models = from i in db.tblCustomers
                             select i;
                return Mapper.Map<List<tblCustomers>, List<Customer>>(models.OrderBy(m => m.customerId).Skip(itemPerPage * (pageNumber - 1)).Take(itemPerPage).ToList());
            }
        }
        public static List<Customer> Get()
        {

            using (var db = new dbContainer())
            {
                var models = from i in db.tblCustomers
                             select i;
                return Mapper.Map<List<tblCustomers>, List<Customer>>(models.ToList());
            }
        }
        public static int GetCount()
        {

            using (var db = new dbContainer())
            {
                var count = from i in db.tblCrashReports
                            select i;
                return count.Count();
            }
        }
        public static List<Lib.Models.OperatingSystem> Get()
        {

            using (var db = new dbContainer())
            {
                var models = from i in db.tblOperatingSystems
                             select i;
                return Mapper.Map<List<tblOperatingSystems>, List<Lib.Models.OperatingSystem>>(models.ToList());
            }
        }
        public static List<CrashReport> Get()
        {

            using (var db = new dbContainer())
            {
                var models = from i in db.tblCrashReports
                             select i;
                return Mapper.Map<List<tblCrashReports>, List<CrashReport>>(models.ToList());
            }
        }
  public static List<Application> Get(int itemPerPage, int pageNumber = 1)
  {
 
      using (var db = new dbContainer())
      {
          var models = from i in db.tblApplications 
                       select i; 
         return Mapper.Map<List<tblApplications>, List<Application>>(models.OrderBy(m=>m.name).Skip(itemPerPage * (pageNumber-1)).Take(itemPerPage).ToList());
      }
  }
 public static Application AddMessage(MessageViewModel VMMessage)
 {
     using (var db = new dbContainer())
     {
         var application = db.tblApplications.Find(VMMessage.tblApplications_applicationId);
         tblMessages msg = Mapper.Map<MessageViewModel, tblMessages>(VMMessage);
         application.tblMessages.Add(msg);
         db.SaveChanges();
         return Mapper.Map<tblApplications, Application>(application);
     }
 }
        public static void Create(Customer model)
        {
            using (var db = new dbContainer())
            {
                var customer = Mapper.Map<Customer, tblCustomers>(model);
                customer.dateCreated = DateTime.Now;
                db.tblCustomers.Add(customer);
                db.SaveChanges();

            }
        }
 public static List<Device> GetDevicesInRecentDate(int daysAgo)
 {
     daysAgo = daysAgo * -1;
     using (var db = new dbContainer())
     {
         var dateweekago = DateTime.Now.AddDays(-7);
         var models = from d in db.tblDevices
                      where d.insertDate > dateweekago
                      select d;
         return Mapper.Map<List<tblDevices>, List<Device>>(models.ToList()); ;
     }
 }
        public static Application GetDetails(int id = 0)
        {
            using (var db = new dbContainer())
            {
                var models = from a in db.tblApplications
                             where a.applicationId == id
                             select a;
                //Result
                tblApplications result = models.FirstOrDefault();
                //mappen
                return Mapper.Map<tblApplications, Application>(result);

            }
        }
 public static Customer Delete(int customerId)
 {
     using (var db = new dbContainer())
     {
         var dbCustomer = (from c in db.tblCustomers where c.customerId == customerId select c).FirstOrDefault();
         var customer = Mapper.Map<tblCustomers, Customer>(dbCustomer);
         if (dbCustomer != null)
         {
             db.tblCustomers.Remove(dbCustomer);
             db.SaveChanges();
         }
         return customer;
     }
 }
 public static Customer GetDetails(int? id)
 {
     if (id.HasValue)
     {
         using (var db = new dbContainer())
         {
             var models = from c in db.tblCustomers
                          where c.customerId == id
                          select c;
             var result = models.FirstOrDefault();
             return Mapper.Map<tblCustomers, Customer>(result);
         }
     }
     return null;
 }
        public static List<PopularViewModel> GetPopularApps(int limit)
        {

            using (var db = new dbContainer())
            {
                var models = (from a in db.tblApplications
                              join d in db.tblDevices on a.applicationId equals d.tblApplications_applicationId
                              group a by new {a.applicationId, a.name} into popular
                              select new PopularViewModel
                              {
                                  applicationId = popular.Key.applicationId,
                                  applicationName = popular.Key.name,
                                  devices = popular.Count()
                              });

                return models.Take(limit).ToList();
            }
        }
        public static List<OSViewModel> GetStatistics()
        {
          //  select tblOperatingSystems.name, COUNT(*) as counter from tblApplications JOIN tblOperatingSystems on tblApplications.tblOperatingSystems_operatingSystemId = tblOperatingSystems.operatingSystemId group by tblOperatingSystems.name

            using (var db = new dbContainer())
            {
                var models = (from a in db.tblApplications
                              join d in db.tblOperatingSystems on a.tblOperatingSystems_operatingSystemId equals d.operatingSystemId
                              group a by d.name into OS
                              select new OSViewModel
                              {
                                 name = OS.Key,
                                 counter = OS.Count()
                              });

                return models.ToList(); 
        
            }
        }
        public static Application Delete(int appId)
        {
            using (var db = new dbContainer())
            {
                var dbApplication = (from i in db.tblApplications where i.applicationId == appId select i).FirstOrDefault();

                var application = Mapper.Map<tblApplications, Application>(dbApplication);

                if (dbApplication != null)
                {
                    db.tblApplications.Remove(dbApplication);
                    db.SaveChanges();
                }

                return application;
            }
        }
 public static List<Application> GetSearch(string searchtag)
 {
     using(var db = new dbContainer()) {
         var dbApplications = (from a in db.tblApplications where a.name.Contains(searchtag) select a).ToList();
         return Mapper.Map<List<tblApplications>, List<Application>>(dbApplications);
     }
 }
 public static Counter GetProgramStatistics()
 {
     using (var db = new dbContainer())
     {
         var models = from c in db.tblCounts
                      select c;
         //result
         tblCounts result = models.FirstOrDefault();
         return Mapper.Map<tblCounts, Counter>(result);
     }
 }