public void BanUserToChat(UserChat userChatBanned, double time)
        {
            using (ChatAppContext db = new ChatAppContext())
            {
                User     userBanned = db.Users.Include(s => s.UserChats).FirstOrDefault(s => s.Name == userChatBanned.User.Name);
                Chat     chat       = db.Chats.Include(s => s.UserChats).FirstOrDefault(s => s.Name == userChatBanned.Chat.Name);
                UserChat userChat   = userBanned.UserChats.Where(uc => uc.ChatId == chat.Id && uc.UserId == userBanned.Id).FirstOrDefault();

                userChat.BanEndDate = DateTime.Now.AddMinutes(time);

                db.SaveChanges();
            }
        }
        public void RemoveUserFromChat(string chatName, string userName)
        {
            using (ChatAppContext db = new ChatAppContext())
            {
                User     user     = db.Users.Include(s => s.UserChats).FirstOrDefault(s => s.Name == userName);
                Chat     chat     = db.Chats.Include(s => s.UserChats).FirstOrDefault(s => s.Name == chatName);
                UserChat userChat = user.UserChats.Where(uc => uc.ChatId == chat.Id && uc.UserId == user.Id).FirstOrDefault();

                chat.UserChats.Remove(userChat);
                user.UserChats.Remove(userChat);

                db.SaveChanges();
            }
        }
        public bool IsUserBanned(string chatName, string userName)
        {
            using (ChatAppContext db = new ChatAppContext())
            {
                Chat     chat     = db.Chats.Include(u => u.UserChats).FirstOrDefault(c => c.Name == chatName);
                User     user     = db.Users.Include(u => u.UserChats).FirstOrDefault(c => c.Name == userName);
                UserChat userChat = chat.UserChats.Where(uc => uc.ChatId == chat.Id && uc.UserId == user.Id).FirstOrDefault();

                if (userChat != null && userChat.BanEndDate > DateTime.Now)
                {
                    return(true);
                }

                return(false);
            }
        }
        public string GetStatusUser(string chatName, string userName)
        {
            using (ChatAppContext db = new ChatAppContext())
            {
                User     user      = db.Users.Include(u => u.UserChats).FirstOrDefault(u => u.Name == userName);
                Chat     chat      = db.Chats.Include(c => c.UserChats).FirstOrDefault(c => c.Name == chatName);
                UserChat userChats = chat.UserChats.Where(c => c.Chat.Name == chatName && c.User.Name == userName).FirstOrDefault();

                if (chat.Creator == userName)
                {
                    return("creator");
                }

                return(user.Role);
            }
        }
        public void AddUserToChat(UserChat userChat)
        {
            using (ChatAppContext db = new ChatAppContext())
            {
                Chat     chat        = db.Chats.Include(s => s.UserChats).FirstOrDefault(s => s.Name == userChat.Chat.Name);
                User     user        = db.Users.Include(s => s.UserChats).FirstOrDefault(s => s.Name == userChat.User.Name);
                UserChat newUserChat = new UserChat {
                    ChatId = chat.Id, UserId = user.Id, BanEndDate = new DateTime()
                };

                if (user.UserChats.Select(uc => uc.ChatId).FirstOrDefault() != newUserChat.ChatId)
                {
                    chat.UserChats.Add(newUserChat);
                    user.UserChats.Add(newUserChat);

                    db.SaveChanges();
                }
            }
        }
        public void Delete(string chatName, string userName)
        {
            using (ChatAppContext db = new ChatAppContext())
            {
                User           user        = db.Users.Include(s => s.UserChats).FirstOrDefault(s => s.Name == userName);
                Chat           chat        = db.Chats.Include(s => s.UserChats).FirstOrDefault(s => s.Name == chatName);
                List <Message> messages    = db.Messages.Where(m => m.ChatId == chat.Id).ToList();
                UserChat       newUserChat = new UserChat {
                    ChatId = chat.Id, UserId = user.Id
                };

                chat.UserChats.Remove(newUserChat);
                user.UserChats.Remove(newUserChat);
                Messages.RemoveRange(messages);

                db.Remove(chat);
                db.SaveChanges();
            }
        }
        public List <Chat> GetChats(string userName)
        {
            using (ChatAppContext db = new ChatAppContext())
            {
                List <Chat> chats     = db.Chats.ToList();
                User        user      = db.Users.Include(s => s.UserChats).FirstOrDefault(s => s.Name == userName);
                List <Chat> userChats = new List <Chat>();

                foreach (Chat chat in chats)
                {
                    UserChat userChat = user.UserChats.Where(uc => uc.ChatId == chat.Id && uc.UserId == user.Id).FirstOrDefault();
                    if (userChat != null)
                    {
                        userChats.Add(new Chat {
                            Name = chat.Name
                        });
                    }
                }

                return(userChats);
            }
        }