public Wishlist GetFavoritesByUserId(int userId)
        {
            Wishlist favorite;

            try
            {
                using (WishlistDbContext context = new WishlistDbContext())
                {
                    context.Database.EnsureCreated();
                    favorite = context.Wishlists.FirstOrDefault(w => w.WishlistId == context.OwnedWishlists.FirstOrDefault(ow => ow.IsFavorite && ow.OwnerId == userId).WishlistId);

                    favorite.Items = new ObservableCollection <Item>();

                    foreach (Item i in GetItemsByWishlistId(favorite.WishlistId))
                    {
                        favorite.Items.Add(i);
                    }
                    return(favorite);
                }
            }
            catch (Exception eContext)
            {
                Debug.WriteLine("Exception: " + eContext.Message);
                return(null);
            }
        }
        public List <Wishlist> GetOwnedWishlistsByUserId(int userId)
        {
            try
            {
                using (WishlistDbContext context = new WishlistDbContext())
                {
                    context.Database.EnsureCreated();

                    List <int> wishlistIds = context.OwnedWishlists
                                             .Where(ow => !ow.IsFavorite && ow.OwnerId == userId)
                                             .Select(ow => ow.WishlistId).ToList();

                    List <Wishlist> wishlists = new List <Wishlist>();
                    foreach (int id in wishlistIds)
                    {
                        context.Wishlists.FirstOrDefault(w => w.WishlistId == id).SetDeadlineText();
                        wishlists.Add(context.Wishlists.FirstOrDefault(w => w.WishlistId == id));
                    }
                    return(wishlists);
                }
            }
            catch (Exception eContext)
            {
                Debug.WriteLine("Exception: " + eContext.Message);
                return(null);
            }
        }
        public Message CheckIfMessageExists(Message msg)
        {
            try
            {
                using (WishlistDbContext context = new WishlistDbContext())
                {
                    Message message;

                    //First get all messages of receiver
                    foreach (int messageId in context.Notifications.Where(n => n.ReceiverId == msg.Receiver.ReceiverId).Select(n => n.MessageId).ToList())
                    {
                        message = GetMessageById(messageId);
                        if (message.MessageContent.Equals(msg.MessageContent))
                        {
                            return(message);
                        }
                    }
                }
            }
            catch (Exception eContext)
            {
                Debug.WriteLine("Exception: " + eContext.Message);
                return(null);
            }
            return(null);
        }
        public List <Wishlist> GetOpenParticipatingWishlistsByUserId(int userId)
        {
            try
            {
                using (WishlistDbContext context = new WishlistDbContext())
                {
                    context.Database.EnsureCreated();

                    List <Wishlist> ws = new List <Wishlist>();

                    foreach (User c in User.Contacts)
                    {
                        foreach (int wi in context.OwnedWishlists.Where(ow => ow.OwnerId == c.UserId && !ow.IsFavorite).Select(wo => wo.WishlistId).ToList())
                        {
                            if (context.Wishlists.FirstOrDefault(w => w.WishlistId == wi).IsOpen)
                            {
                                Wishlist wl = context.Wishlists.FirstOrDefault(w => w.WishlistId == wi);
                                wl.SetDeadlineText();
                                wl.Owner = context.Users.FirstOrDefault(o => o.UserId == context.OwnedWishlists.FirstOrDefault(ow => ow.WishlistId == wi).OwnerId);
                                ws.Add(wl);
                            }
                        }
                    }
                    return(ws);
                }
            }
            catch (Exception eContext)
            {
                Debug.WriteLine("Exception: " + eContext.Message);
                return(null);
            }
        }
        //Get all messages that have not been answered yet
        public List <Message> GetWaitingMessagesLoggedInUser()
        {
            List <Message> messages = new List <Message>();

            try
            {
                using (WishlistDbContext context = new WishlistDbContext())
                {
                    //All messages where IsAccepted is null have not been responded to yet
                    //messages = context.Messages.Where(msg => msg.Receiver.UserId == User.UserId && msg.IsAccepted == null).ToList();
                    List <int> msgIds = context.Notifications.Where(n => n.ReceiverId == User.UserId).Select(n => n.MessageId).ToList();
                    foreach (int id in msgIds)
                    {
                        Message m = context.Messages.FirstOrDefault(msg => msg.MessageId == id);
                        if (m.IsAccepted == null)
                        {
                            messages.Add(m);
                        }
                    }
                }
            }
            catch (Exception eContext)
            {
                Debug.WriteLine("Exception: " + eContext.Message);
            }

            return(messages);
        }
        public void CreateItem(Item i)
        {
            try
            {
                using (WishlistDbContext context = new WishlistDbContext())
                {
                    context.Items.Add(i);
                    context.SaveChanges();


                    //context.WishlistItems.Add(wi);
                    //context.Update(SelectedWishlist);
                    //context.SaveChanges();

                    //i.Wishlist = wi;
                    //UpdateItem(i);

                    /*
                     * WishlistItem wi = new WishlistItem(i.ItemId, SelectedWishlist.WishlistId, i, SelectedWishlist);
                     * context.WishlistItems.Add(wi);
                     * i.Wishlist = wi;
                     * // SelectedWishlist.Gifts.Add(wi);
                     * //context.SaveChanges();
                     * context.Items.Add(i);
                     * context.Update(SelectedWishlist);
                     * context.Update(wi);
                     * context.SaveChanges();
                     */
                }
            }
            catch (Exception eContext)
            {
                Debug.WriteLine("Exception: " + eContext.Message);
            }
        }
        public void DeleteWishlist(Wishlist w)
        {
            try
            {
                using (WishlistDbContext context = new WishlistDbContext())
                {
                    UserWishlist uw = context.OwnedWishlists.FirstOrDefault(ow => ow.OwnerId == User.UserId && ow.WishlistId == w.WishlistId);
                    context.Remove(uw);

                    if (GetItemsByWishlistId(w.WishlistId) != null || GetItemsByWishlistId(w.WishlistId).Count > 0)
                    {
                        foreach (Item i in GetItemsByWishlistId(w.WishlistId))
                        {
                            DeleteItem(i);
                        }
                    }


                    context.Remove(w);
                    context.SaveChanges();
                }
            }
            catch (Exception eContext)
            {
                Debug.WriteLine("Exception: " + eContext.Message);
            }
        }
        public List <Wishlist> GetClosedParticipatingWishlistsByUserId(int userId)
        {
            try
            {
                using (WishlistDbContext context = new WishlistDbContext())
                {
                    context.Database.EnsureCreated();

                    List <Wishlist> ws = new List <Wishlist>();

                    foreach (int id in context.Participants.Where(p => p.ParticipantId == User.UserId).Select(p => p.WishlistId).ToList())
                    {
                        Wishlist wl = context.Wishlists.FirstOrDefault(w => w.WishlistId == id);
                        wl.SetDeadlineText();
                        wl.Owner = context.Users.FirstOrDefault(o => o.UserId == context.OwnedWishlists.FirstOrDefault(ow => ow.WishlistId == id).OwnerId);
                        ws.Add(wl);
                    }
                    return(ws);
                }
            }
            catch (Exception eContext)
            {
                Debug.WriteLine("Exception: " + eContext.Message);
                return(null);
            }
        }
        public List <Item> GetItemsByWishlistId(int wishlistId)
        {
            try
            {
                using (WishlistDbContext context = new WishlistDbContext())
                {
                    List <int>  giftIds = new List <int>();
                    List <Item> gifts   = new List <Item>();
                    giftIds = context.WishlistItems
                              .Where(wi => wi.WishlistId == wishlistId)
                              .Select(wi => wi.ItemId)
                              .ToList();

                    foreach (int id in giftIds)
                    {
                        gifts.Add(context.Items.FirstOrDefault(i => i.ItemId == id));
                    }
                    //ObservableCollection<Item> gifts = new ObservableCollection<Item>(context.Items.Where(i => w.WishlistId == wishlistId).Select(w => w.Gifts) as List<Item>);
                    return(gifts);
                }
            }
            catch (Exception eContext)
            {
                Debug.WriteLine("Exception: " + eContext.Message);
                return(null);
            }
        }
Пример #10
0
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

            WishlistDbContext _context = new WishlistDbContext();

            _context.Database.EnsureDeleted();
            _context.Database.EnsureCreated();
            new WishlistDataInitializer(_context).InitializeData();
        }
Пример #11
0
        public void SetupLoggedInUser(User u)
        {
            User = u;

            try
            {
                using (WishlistDbContext context = new WishlistDbContext())
                {
                    context.Database.EnsureCreated();

                    //Set Contact list

                    User.Contacts = new ObservableCollection <User>(GetContactsByUserId(User.UserId) as List <User>);

                    //Set Wishlists
                    //Set Wishlist of favorites
                    User.Favorites = GetFavoritesByUserId(User.UserId);
                    //Reset lists
                    User.MyWishlists     = new ObservableCollection <Wishlist>();
                    User.OthersWishlists = new ObservableCollection <Wishlist>();
                    User.Notifications   = new ObservableCollection <Message>();
                    //Set Own Wishlists
                    foreach (Wishlist w in GetOwnedWishlistsByUserId(User.UserId))
                    {
                        User.MyWishlists.Add(w);
                    }
                    //Set all open wishlist of users contacts which user can participate in
                    foreach (Wishlist w in GetOpenParticipatingWishlistsByUserId(User.UserId))
                    {
                        User.OthersWishlists.Add(w);
                    }
                    //Set closed wishlists user is participating with
                    foreach (Wishlist w in GetClosedParticipatingWishlistsByUserId(User.UserId))
                    {
                        User.OthersWishlists.Add(w);
                    }
                    //Set closed wishlist user is capable of requesting to join
                    foreach (Wishlist w in GetClosedNonParticipatingWishlistsByUserId(User.UserId))
                    {
                        User.OthersWishlists.Add(w);
                    }

                    //Set Notifications
                    foreach (Message m in GetWaitingMessagesLoggedInUser())
                    {
                        m.Sender = GetUserById(m.IdSender);
                        User.Notifications.Add(m);
                    }
                }
            }
            catch (Exception eContext)
            {
                Debug.WriteLine("Exception: " + eContext.Message);
            }
        }
Пример #12
0
 public Message GetMessageById(int messageId)
 {
     try
     {
         using (WishlistDbContext context = new WishlistDbContext())
         {
             return(context.Messages.FirstOrDefault(m => m.MessageId == messageId));
         }
     }
     catch (Exception eContext)
     {
         Debug.WriteLine("Exception: " + eContext.Message);
         return(null);
     }
 }
Пример #13
0
 public void UpdateMessage(Message m)
 {
     try
     {
         using (WishlistDbContext context = new WishlistDbContext())
         {
             context.Update(m);
             context.SaveChanges();
         }
     }
     catch (Exception eContext)
     {
         Debug.WriteLine("Exception: " + eContext.Message);
     }
 }
Пример #14
0
 public void AddUserContact(UserContact uc)
 {
     try
     {
         using (WishlistDbContext context = new WishlistDbContext())
         {
             context.Contacts.Add(uc);
             context.SaveChanges();
         }
     }
     catch (Exception eContext)
     {
         Debug.WriteLine("Exception: " + eContext.Message);
     }
 }
Пример #15
0
 public void AddParticipant(WishlistParticipant wp)
 {
     try
     {
         using (WishlistDbContext context = new WishlistDbContext())
         {
             context.Participants.Add(wp);
             context.SaveChanges();
         }
     }
     catch (Exception eContext)
     {
         Debug.WriteLine("Exception: " + eContext.Message);
     }
 }
Пример #16
0
 public void AddWishlistItem(WishlistItem wi)
 {
     try
     {
         using (WishlistDbContext context = new WishlistDbContext())
         {
             context.WishlistItems.Add(wi);
             context.SaveChanges();
         }
     }
     catch (Exception eContext)
     {
         Debug.WriteLine("Exception: " + eContext.Message);
     }
 }
Пример #17
0
 public void AddMessageUser(MessageUser mu)
 {
     try
     {
         using (WishlistDbContext context = new WishlistDbContext())
         {
             context.Notifications.Add(mu);
             context.SaveChanges();
         }
     }
     catch (Exception eContext)
     {
         Debug.WriteLine("Exception: " + eContext.Message);
     }
 }
Пример #18
0
 public void CreateUser(User u)
 {
     try
     {
         using (WishlistDbContext context = new WishlistDbContext())
         {
             context.Users.Add(u);
             context.SaveChanges();
         }
     }
     catch (Exception eContext)
     {
         Debug.WriteLine("Exception: " + eContext.Message);
     }
 }
Пример #19
0
 public Wishlist GetWishlistById(int?wishlistId)
 {
     try
     {
         using (WishlistDbContext context = new WishlistDbContext())
         {
             context.Database.EnsureCreated();
             return(context.Wishlists.FirstOrDefault(w => w.WishlistId == wishlistId));
         }
     }
     catch (Exception eContext)
     {
         Debug.WriteLine("Exception: " + eContext.Message);
         return(null);
     }
 }
Пример #20
0
 //DB METHODS
 #region DBmethods GET/READ
 public User GetUserById(int userId)
 {
     try
     {
         using (WishlistDbContext context = new WishlistDbContext())
         {
             context.Database.EnsureCreated();
             return(context.Users.FirstOrDefault(u => u.UserId == userId));
         }
     }
     catch (Exception eContext)
     {
         Debug.WriteLine("Exception: " + eContext.Message);
         return(null);
     }
 }
Пример #21
0
 public User GetUserByEmail(string email)
 {
     try
     {
         using (WishlistDbContext context = new WishlistDbContext())
         {
             context.Database.EnsureCreated();
             return(context.Users.FirstOrDefault(u => string.Equals(u.Email, email, StringComparison.CurrentCultureIgnoreCase)));
         }
     }
     catch (Exception eContext)
     {
         Debug.WriteLine("Exception: " + eContext.Message);
         return(null);
     }
 }
Пример #22
0
 public User GetOwnerByWishlistId(int wishlistId)
 {
     try
     {
         using (WishlistDbContext context = new WishlistDbContext())
         {
             int ownerId = context.OwnedWishlists.FirstOrDefault(ow => ow.WishlistId == wishlistId).OwnerId;
             return(GetUserById(ownerId));
         }
     }
     catch (Exception eContext)
     {
         Debug.WriteLine("Exception: " + eContext.Message);
         return(null);
     }
 }
Пример #23
0
 public void DeleteItem(Item i)
 {
     try
     {
         using (WishlistDbContext context = new WishlistDbContext())
         {
             WishlistItem wi = context.WishlistItems.FirstOrDefault(wis => wis.ItemId == i.ItemId && wis.WishlistId == SelectedWishlist.WishlistId);
             context.Remove(wi);
             context.SaveChanges();
             context.Remove(i);
             context.SaveChanges();
         }
     }
     catch (Exception eContext)
     {
         Debug.WriteLine("Exception: " + eContext.Message);
     }
 }
Пример #24
0
        public Wishlist SetupSelectedWishlist(Wishlist w)
        {
            SelectedWishlist = w;

            try
            {
                using (WishlistDbContext context = new WishlistDbContext())
                {
                    context.Database.EnsureCreated();

                    //Set Wishlist owner
                    SelectedWishlist.Owner = GetOwnerByWishlistId(SelectedWishlist.WishlistId);
                    //Set Items of wishlist
                    SelectedWishlist.Items  = new ObservableCollection <Item>();
                    SelectedWishlist.Gifts  = new ObservableCollection <WishlistItem>();
                    SelectedWishlist.Buyers = new ObservableCollection <User>();

                    foreach (Item i in GetItemsByWishlistId(SelectedWishlist.WishlistId))
                    {
                        i.SetCategory();
                        SelectedWishlist.Items.Add(i);
                        SelectedWishlist.Gifts.Add(context.WishlistItems.FirstOrDefault(wi => wi.ItemId == i.ItemId && wi.WishlistId == SelectedWishlist.WishlistId));
                        if (i.BuyerId.GetValueOrDefault() != 0)
                        {
                            i.Buyer = GetUserById(i.BuyerId.GetValueOrDefault());
                        }
                    }

                    //Set Wishlist participants
                    foreach (User u in GetParticipantsByWishlistId(SelectedWishlist.WishlistId))
                    {
                        SelectedWishlist.Buyers.Add(u);
                    }

                    return(SelectedWishlist);
                }
            }
            catch (Exception eContext)
            {
                Debug.WriteLine("Exception: " + eContext.Message);
                return(null);
            }
        }
Пример #25
0
 public bool CheckIfUserParticipates(int wishlistId)
 {
     try
     {
         using (WishlistDbContext context = new WishlistDbContext())
         {
             if (context.Participants.Where(p => p.WishlistId == wishlistId).Select(p => p.ParticipantId).Contains(User.UserId))
             {
                 return(true);
             }
             return(false);
         }
     }
     catch (Exception eContext)
     {
         Debug.WriteLine("Exception: " + eContext.Message);
         return(false);
     }
 }
Пример #26
0
 public List <User> GetContactsByUserId(int userId)
 {
     try
     {
         using (WishlistDbContext context = new WishlistDbContext())
         {
             context.Database.EnsureCreated();
             return(context.Contacts
                    .Where(c => c.UserId == User.UserId)
                    .Select(c => c.Contact)
                    .ToList());
         }
     }
     catch (Exception eContext)
     {
         Debug.WriteLine("Exception: " + eContext.Message);
         return(null);
     }
 }
Пример #27
0
 public bool CheckIfBought(int itemId)
 {
     try
     {
         using (WishlistDbContext context = new WishlistDbContext())
         {
             int item = context.Items.FirstOrDefault(it => it.ItemId == itemId).BuyerId.GetValueOrDefault();
             if (item != 0)
             {
                 return(true);
             }
             return(false);
         }
     }
     catch (Exception eContext)
     {
         Debug.WriteLine("Exception: " + eContext.Message);
         return(false);
     }
 }
Пример #28
0
        public void CreateWishlist(Wishlist w, bool favorite)
        {
            try
            {
                using (WishlistDbContext context = new WishlistDbContext())
                {
                    //Update joined table
                    UserWishlist uw = new UserWishlist(User.UserId, w.WishlistId, User, w, favorite);
                    context.OwnedWishlists.Add(uw);
                    w.WishlistOwner = uw;

                    context.Wishlists.Add(w);
                    context.SaveChanges();
                }
            }
            catch (Exception eContext)
            {
                Debug.WriteLine("Exception: " + eContext.Message);
            }
        }
Пример #29
0
        public List <User> GetParticipantsByWishlistId(int wishlistId)
        {
            try
            {
                using (WishlistDbContext context = new WishlistDbContext())
                {
                    List <User> participants = new List <User>();

                    List <int> particpantIds = context.Participants.Where(p => p.WishlistId == wishlistId).Select(p => p.ParticipantId).ToList();
                    foreach (int id in particpantIds)
                    {
                        participants.Add(context.Users.FirstOrDefault(u => u.UserId == id));
                    }

                    return(participants);
                }
            }
            catch (Exception eContext)
            {
                Debug.WriteLine("Exception: " + eContext.Message);
                return(null);
            }
        }
Пример #30
0
        public List <Wishlist> GetClosedNonParticipatingWishlistsByUserId(int userId)
        {
            try
            {
                using (WishlistDbContext context = new WishlistDbContext())
                {
                    context.Database.EnsureCreated();

                    List <Wishlist> ws = new List <Wishlist>();
                    List <Wishlist> wl = new List <Wishlist>();

                    foreach (int id in context.Contacts.Where(c => c.UserId == User.UserId).Select(c => c.ContactId).ToList())
                    {
                        wl = GetOwnedWishlistsByUserId(id);
                        foreach (Wishlist wishlist in wl)
                        {
                            //add all wishlists that arent open and that haven't been joined yet by logged in user
                            if ((!wishlist.IsOpen) && (GetParticipantsByWishlistId(wishlist.WishlistId).FirstOrDefault(p => p.UserId == userId) == null)) //&& GetOwnerByWishlistId(wishlist.WishlistId).UserId != User.UserId
                            {
                                wishlist.SetDeadlineText();
                                wishlist.Owner = context.Users.FirstOrDefault(o => o.UserId == context.OwnedWishlists.FirstOrDefault(ow => ow.WishlistId == wishlist.WishlistId).OwnerId);
                                ws.Add(wishlist);
                            }
                        }
                    }


                    return(ws);
                }
            }
            catch (Exception eContext)
            {
                Debug.WriteLine("Exception: " + eContext.Message);
                return(null);
            }
        }