Exemplo n.º 1
0
 public static T RemoveEntity(T entity)
 {
     using (var context = new LocalDbContext())
     {
         context.Entry(entity).State = EntityState.Deleted;
         context.SaveChanges();
     }
     return(entity);
 }
Exemplo n.º 2
0
 public static void UpdateEntity(int id, T updatedEntity)
 {
     using (var context = new LocalDbContext())
     {
         var entitySet = context.Set <T>();
         var oldEntity = entitySet.Find(id);
         context.Entry(oldEntity).CurrentValues.SetValues(updatedEntity);
         context.SaveChanges();
     }
 }
Exemplo n.º 3
0
        public IEnumerable <User> GetAllTenants()
        {
            IEnumerable <User> tenants = null;

            using (var dbContext = new LocalDbContext())
            {
                tenants = dbContext
                          .Users
                          .Where(user => user.UserType == UserTypes.Tenant).ToList().AsEnumerable();
            }
            return(tenants);
        }
Exemplo n.º 4
0
        public static T AddEntity(T entity)
        {
            T newEntity;

            using (var context = new LocalDbContext())
            {
                var entitySet = context.Set <T>();
                newEntity = entitySet.Add(entity);
                context.SaveChanges();
            }
            return(newEntity);
        }
Exemplo n.º 5
0
        public User GetUserById(int id)
        {
            User theUser = null;

            using (var dbContext = new LocalDbContext())
            {
                theUser = dbContext
                          .Users
                          .FirstOrDefault(user => user.Id == id);
            }
            return(theUser);
        }
        public SmartHome GetSmartHomeById(int id)
        {
            SmartHome smartHome = null;

            using (var dbContext = new LocalDbContext())
            {
                smartHome = dbContext
                            .SmartHomes
                            .Include(smh => smh.User) // eagerly loading
                            .FirstOrDefault(smh => smh.Id == id);
            }
            return(smartHome);
        }
Exemplo n.º 7
0
        public static T RemoveEntity(int id)
        {
            T entity;

            using (var context = new LocalDbContext())
            {
                var entitySet = context.Set <T>();
                entity = entitySet.Find(id);
                context.Entry(entity).State = EntityState.Deleted;
                context.SaveChanges();
            }
            return(entity);
        }
        public Invoice GetInvoiceById(int id)
        {
            Invoice theInvoice = null;

            using (var dbContext = new LocalDbContext())
            {
                theInvoice = dbContext
                             .Invoices
                             .Include(invoice => invoice.User) // eagerly loading
                             .FirstOrDefault(invoice => invoice.Id == id);
            }
            return(theInvoice);
        }
        public IEnumerable <NotifiedUser> GetAllNotifiedTenants()
        {
            IEnumerable <NotifiedUser> notifiedTenants = null;

            using (var dbContext = new LocalDbContext())
            {
                notifiedTenants = dbContext
                                  .NotifiedUsers
                                  .Include(user => user.User).Include(user => user.SmartHome)
                                  .Where(user => user.User.UserType == UserTypes.Tenant).ToList().AsQueryable();
            }
            return(notifiedTenants);
        }
        public IEnumerable <Event> GetEventsByMonthAndYear(int month, int year)
        {
            IEnumerable <Event> events = null;

            using (var dbContext = new LocalDbContext())
            {
                events = dbContext
                         .Events
                         .Include(e => e.User) // eagerly loading
                         .Where(e => e.Date.Year == year && e.Date.Month == month)
                         .ToList().AsEnumerable();
            }
            return(events);
        }
        public IEnumerable <Invoice> GetUnpaidInvoices()
        {
            IEnumerable <Invoice> theInvoices = null;

            using (var dbContext = new LocalDbContext())
            {
                //var query = from inv in dbContext.Invoices
                //            where !inv.IsPaid
                //            select inv;
                //theInvoices = query.AsEnumerable();
                theInvoices = dbContext.Invoices
                              .Include(i => i.User)
                              .Where(i => !i.IsPaid)
                              .ToList()
                              .AsEnumerable();
            }
            return(theInvoices);
        }
Exemplo n.º 12
0
        public static IEnumerable <T> GetAllEntities()
        {
            IList <T> entities = new List <T>();

            using (var context = new LocalDbContext())
            {
                var entitySet = context.Set <T>();
                var query     = from entity in entitySet
                                select entity;

                foreach (var element in query)
                {
                    entities.Add(element);
                }
                // entities = query.AsEnumerable();

                var q = from t in Assembly.GetExecutingAssembly().GetTypes()
                        where t.IsClass && t.Namespace == "Backend.Models"
                        select t;

                var props = typeof(T).GetProperties().Where(p => q.Contains(p.PropertyType)).Select(p => p.Name);

                if (props.Count() == 0)
                {
                    return(entitySet.ToList());
                }

                string includeString = string.Empty;

                foreach (string name in props)
                {
                    includeString += name + ".";
                }

                includeString = includeString.Trim('.');
                // works for every entity that has a User property
                // Solution: use reflaction to find complex properties
                // and call Include(foundProp1+"."+foundProp2+...)
                entities = entitySet
                           .Include(includeString)
                           .ToList();
            }
            return(entities);
        }
        public IEnumerable <Invoice> GetUnpaidInvoicesOfUser(User user)
        {
            IEnumerable <Invoice> theInvoices = null;

            using (var dbContext = new LocalDbContext())
            {
                // lazy loading
                //var query = from inv in dbContext.Invoices
                //            where inv.User.Id == user.Id && !inv.IsPaid
                //           select inv;
                //theInvoices = query.AsEnumerable();
                var theInv = dbContext.Invoices
                             .Include(i => i.User)
                             .Where(i => (i.User.Id == user.Id && !i.IsPaid))
                             .ToList();

                theInvoices = theInv
                              .AsEnumerable();
            }
            return(theInvoices);
        }