public Task <shopping_list> AddAsync(Guid token, shopping_list sl)
        {
            using var db = new ShoppingdbContext();
            if (AuthenticationService.ValidateAsync(token).Result)
            {
                var newSl = new shopping_list {
                    gid    = sl.gid,
                    slname = sl.slname
                };
                var result = db.shopping_list.Add(newSl).Entity;
                db.SaveChanges();

                var soldOut = db.item.Where(i => i.sold_out).ToList();
                foreach (var i in soldOut)
                {
                    db.item.Remove(i);
                    db.SaveChanges();
                    i.sold_out = false;
                    i.slid     = result.slid;
                    db.item.Add(i);
                }

                db.SaveChanges();

                return(Task.FromResult(result));
            }

            return(null);
        }
        public Task <List <item> > GetItemsAsync(Guid token, int slid)
        {
            using var db = new ShoppingdbContext();
            if (AuthenticationService.ValidateAsync(token).Result)
            {
                return(Task.FromResult(db.item.Where(i => i.slid == slid).ToList()));
            }

            return(null);
        }
        public Task <List <shopping_list> > GetShoppingListsAsync(Guid token)
        {
            using var db = new ShoppingdbContext();
            if (AuthenticationService.ValidateAsync(token).Result)
            {
                return(Task.FromResult(db.shopping_list.ToList()));
            }

            return(null);
        }
        public Task <item> DeleteAsync(Guid token, item item)
        {
            using var db = new ShoppingdbContext();
            if (AuthenticationService.ValidateAsync(token).Result)
            {
                var result = db.item.Remove(item).Entity;
                db.SaveChanges();

                return(Task.FromResult(result));
            }

            return(null);
        }
        public Task <item> UpdateAsync(Guid token, item item)
        {
            using var db = new ShoppingdbContext();
            if (AuthenticationService.ValidateAsync(token).Result)
            {
                db.Entry(item).State = EntityState.Modified;
                db.SaveChanges();

                return(Task.FromResult(item));
            }

            return(null);
        }
        public Task <Guid> AuthenticateAsync(string username, string pw)
        {
            using var db = new ShoppingdbContext();
            bool valid = db.user_.Any <user_>(u => u.uname == username && u.passw == pw);

            if (valid)
            {
                int uid = db.user_.Where(u => u.uname == username).First().uid;

                return(Task.FromResult(db.auth_token.Where(t => t.user == uid).First().token));
            }

            return(null);
        }
        public Task <shopping_list> DeleteAsync(Guid token, shopping_list sl)
        {
            using var db = new ShoppingdbContext();
            if (AuthenticationService.ValidateAsync(token).Result)
            {
                if (db.item.Any(i => i.slid == sl.slid))
                {
                    foreach (var i in db.item.Where(i => i.slid == sl.slid).ToList())
                    {
                        db.item.Remove(i);
                    }
                }

                var result = db.shopping_list.Remove(sl).Entity;
                db.SaveChanges();

                return(Task.FromResult(result));
            }

            return(null);
        }
        public Task <item> AddAsync(Guid token, item item)
        {
            using var db = new ShoppingdbContext();
            if (AuthenticationService.ValidateAsync(token).Result)
            {
                var newItem = new item
                {
                    slid     = item.slid,
                    iname    = item.iname,
                    ammount  = item.ammount,
                    added_by = item.added_by,
                    sold_out = item.sold_out
                };
                var result = db.item.Add(newItem).Entity;
                db.SaveChanges();

                return(Task.FromResult(result));
            }

            return(null);
        }
 public Task <bool> ValidateAsync(Guid token)
 {
     using var db = new ShoppingdbContext();
     return(Task.FromResult(db.auth_token.Any(t => t.token == token)));
 }