public int Add(Conversation conversation)
 {
     using (var context = new ChatContext())
     {
         try
         {
             /// User remapping is done here because EF didn't recognize conversation users as
             /// same object as ones in the context so it created duplicate entries.
             /// Atm I had no time to find a better solution.
             Conversation newConversation = new Conversation();
             foreach (var participant in conversation.Participants)
             {
                 var user = context.Users.Find(participant.Id);
                 if (user != null)
                     newConversation.Participants.Add(user);
             }
             context.Conversations.Add(newConversation);
             context.SaveChanges();
             return newConversation.Id;
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.StackTrace);
             return -1;
         }
     }
 }
        public User Get(string username)
        {
            using (var context = new ChatContext())
            {
                try
                {
                    var fetchedUser = context.Users.ToList().First(user => user.Username.Equals(username));

                    Console.WriteLine("\n User Get response: ");
                    Console.WriteLine("---> userId: " + fetchedUser.Id);
                    Console.WriteLine("---> username: "******"-----> conversation: Id " + conversation.Id + " ->" + conversation.Participants[0].Username + " - " + conversation.Participants[1].Username);
                    }

                    return fetchedUser;
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.StackTrace);
                    return null;
                }
            }
        }
 public IEnumerable<Message> GetAllFromConversation(int conversationId)
 {
     using (var context = new ChatContext())
     {
         List<Message> messages = new List<Message>();
         foreach (var message in context.Messages.ToList())
         {
             if (message.Conversation.Id.Equals(conversationId))
                 messages.Add(message);
         }
         return messages;
     }
 }
 public Message Get(int messageId)
 {
     using (var context = new ChatContext())
     {
         try
         {
             return context.Messages.Find(messageId);
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.StackTrace);
             return null;
         }
     }
 }
 public Conversation Get(int conversationId)
 {
     using (var context = new ChatContext())
     {
         try
         {
             return context.Conversations.Find(conversationId);
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.StackTrace);
             return null;
         }
     }
 }
 public bool Remove(int messageId)
 {
     using (var context = new ChatContext())
     {
         try
         {
             context.Messages.Remove(context.Messages.Find(messageId));
             context.SaveChanges();
             return true;
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.StackTrace);
             return false;
         }
     }
 }
 public bool Add(Message message)
 {
     using (var context = new ChatContext())
     {
         try
         {
             context.Messages.Add(message);
             context.SaveChanges();
             return true;
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.StackTrace);
             return false;
         }
     }
 }
 public bool Update(Conversation conversation)
 {
     using (var context = new ChatContext())
     {
         try
         {
             context.Conversations.Remove(context.Conversations.Find(conversation.Id));
             context.Conversations.Add(conversation);
             context.SaveChanges();
             return true;
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.StackTrace);
             return false;
         }
     }
 }
 public bool Add(User user, string password)
 {
     Credential credential = new Credential();
     credential.User = user;
     credential.Password = password;
     using (var context = new ChatContext())
     {
         try
         {
             context.Users.Add(user);
             context.Credentials.Add(credential);
             context.SaveChanges();
             return true;
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.StackTrace);
             return false;
         }
     }
 }
        public bool Remove(User user)
        {
            using (var context = new ChatContext())
            {
                try
                {
                    var credential = context.Credentials.ToList().First(c => c.User.Username.Equals(user.Username));
                    context.Credentials.Remove(credential);
                    context.SaveChanges();

                    var fetchedUser = context.Users.First(u => u.Username.Equals(user.Username));
                    context.Users.Remove(fetchedUser);
                    context.SaveChanges();

                    return true;
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.StackTrace);
                    return false;
                }
            }
        }
        public bool Update(User user)
        {
            using (var context = new ChatContext())
            {
                try
                {
                    var originalUser = context.Users.Single(u => u.Username.Equals(user.Username));
                    originalUser.Conversations = user.Conversations;

                    context.SaveChanges();
                    return true;
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.StackTrace);
                    return false;
                }
            }
        }
 public bool ValidateCredentials(User user, string password)
 {
     using (var context = new ChatContext())
     {
         try
         {
             foreach (var u in context.Users.ToList().Where(u => u.Username.Equals(user.Username)))
             {
                 foreach (var credential in context.Credentials.ToList().Where(credential => credential.User.Equals(u)))
                 {
                     return credential.Password.Equals(password);
                 }
             }
             return false;
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.StackTrace);
             return false;
         }
     }
 }
 public bool Exists(User user)
 {
     using (var context = new ChatContext())
     {
         try
         {
             return context.Users.ToList().Any(u => u.Username.Equals(user.Username));
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.StackTrace);
             return false;
         }
     }
 }
 public IEnumerable<Conversation> GetAll()
 {
     using (var context = new ChatContext())
     {
         return context.Conversations;
     }
 }
 public bool Exists(Conversation conversation)
 {
     using (var context = new ChatContext())
     {
         try
         {
             bool isEqual;
             foreach (var c in context.Conversations.ToList())
             {
                 isEqual = new HashSet<User>(c.Participants.ToList()).SetEquals(conversation.Participants.ToList());
                 if (isEqual)
                     return true;
             }
             return false;
         }
         catch (Exception e)
         {
             Debug.WriteLine(e.StackTrace);
             return false;
         }
     }
 }