Ejemplo n.º 1
0
        public async Task <bool> Delete(int id)
        {
            using (CookieShopDbContext context = _contextFactory.CreateDbContext())
            {
                T entity = await context.Set <T>().FirstOrDefaultAsync((e) => e.Id == id);

                context.Set <T>().Remove(entity);
                await context.SaveChangesAsync();

                return(true);
            }
        }
Ejemplo n.º 2
0
        public async Task <IEnumerable <T> > GetAll()
        {
            using (CookieShopDbContext context = _contextFactory.CreateDbContext())
            {
                IEnumerable <T> entities = await context.Set <T>().ToListAsync();

                return(entities);
            }
        }
Ejemplo n.º 3
0
        public async Task <T> Get(int id)
        {
            using (CookieShopDbContext context = _contextFactory.CreateDbContext())
            {
                T entity = await context.Set <T>().FirstOrDefaultAsync((e) => e.Id == id);

                return(entity);
            }
        }
Ejemplo n.º 4
0
        public async Task <T> Update(int id, T entity)
        {
            using (CookieShopDbContext context = _contextFactory.CreateDbContext())
            {
                entity.Id = id;
                context.Set <T>().Update(entity);
                await context.SaveChangesAsync();

                return(entity);
            }
        }
Ejemplo n.º 5
0
        public async Task <T> Create(T entity) //async used for not interfering with the UI
        {
            using (CookieShopDbContext context = _contextFactory.CreateDbContext())
            {
                EntityEntry <T> createdEntity = await context.Set <T>().AddAsync(entity);

                await context.SaveChangesAsync();

                return(createdEntity.Entity);
            }
        }
Ejemplo n.º 6
0
        public async Task <IEnumerable <Cookie> > GetAll(string name, CookieType?type, int?price, int?sweeteners, int?rating)
        {
            using (CookieShopDbContext context = _contextFactory.CreateDbContext())
            {
                IEnumerable <Cookie> entities = (await context.Set <Cookie>()
                                                 .Include((a) => a.Ratings)
                                                 .Include(c => c.Stock)
                                                 .Where(cookie =>
                                                        (name == null || name == string.Empty || cookie.Name.Contains(name)) &&
                                                        (type == null || cookie.Type == type) &&
                                                        (price == null || cookie.Price == price) &&
                                                        (sweeteners == null || cookie.Sweeteners == sweeteners)

                                                        )
                                                 .ToListAsync())
                                                .Where(cookie => (rating == null || Math.Round(cookie.RatingAvg) == rating))
                                                .ToList()
                ;

                return(entities);
            }
        }