Esempio n. 1
0
 public void AssignCurrentUser(string username, string password)
 {
     using (var db = new myContext())
     {
         currentUser = db.Users.Where(x => x.Username == username && x.Password == password).SingleOrDefault();
     }
 }
Esempio n. 2
0
 public User GetUser(int userId)
 {
     using (var db = new myContext())
     {
         return(db.Users.Find(userId));
     }
 }
        //public Message currentMessage;

        //// Singleton Pattern
        //private static MessageRepository instance = null;
        //public static MessageRepository Instance
        //{
        //    get
        //    {
        //        if (instance == null)
        //        {
        //            instance = new MessageRepository();
        //        }
        //        return instance;
        //    }
        //}

        //private MessageRepository()
        //{

        //}

        public bool SendMessageToUser(string title, string body, int receiverId)
        {
            using (var db = new myContext())
            {
                db.Messages.Add(new Message(title, body, userRepository.currentUser.Id, receiverId));
            }
        }
Esempio n. 4
0
 // Tier123User
 public List <Message> GetMessageBetweenTwoUsers(int senderId, int receiverId)
 {
     using (var db = new myContext())
     {
         var messages = db.Messages.Include("Sender").Include("Receiver").Where(x => (x.Sender.Id == senderId && x.Receiver.Id == receiverId) || (x.Sender.Id == receiverId && x.Receiver.Id == senderId)).ToList();
         return(messages);
     }
 }
Esempio n. 5
0
 public string ViewMessageWithoutChangingRead(int messageId)
 {
     using (var db = new myContext())
     {
         var message = db.Messages.Include("Sender").Include("Receiver").Where(x => x.Id == messageId).SingleOrDefault();
         return($" {message.Date}\n From : {message.Sender.Username}  ->  To : {message.Receiver.Username}\n Title : {message.Title}\n Message : {message.Body}");
     }
 }
Esempio n. 6
0
 public List <Message> GetMessages()
 {
     using (var db = new myContext())
     {
         var messages = db.Messages.Include("Sender").Include("Receiver").Where(x => x.Receiver.Id == userRepository.currentUser.Id).OrderByDescending(x => x.Date).ToList();
         return(messages);
     }
 }
Esempio n. 7
0
 public void SendMessage(string title, string body, int receiverId)
 {
     using (var db = new myContext())
     {
         db.Messages.Add(new Message(title, body, db.Users.Find(userRepository.currentUser.Id), db.Users.Find(receiverId)));
         db.SaveChanges();
     }
 }
Esempio n. 8
0
 public void DeleteMessage(int messageId)
 {
     using (var db = new myContext())
     {
         db.Messages.Remove(db.Messages.Find(messageId));
         db.SaveChanges();
     }
 }
Esempio n. 9
0
 public void DeleteUser(int userId)
 {
     using (var db = new myContext())
     {
         db.Users.Remove(db.Users.Find(userId));
         db.SaveChanges();
     }
 }
Esempio n. 10
0
 public bool UsernameAndPasswordMatch(string loginUsername, string loginPassword)
 {
     using (var db = new myContext())
     {
         var user = db.Users.Where(x => x.Username == loginUsername && x.Password == loginPassword).SingleOrDefault();
         return(UserExists(user));
     }
 }
Esempio n. 11
0
 public List <User> GetUsers()
 {
     using (var db = new myContext())
     {
         var users = db.Users.ToList();
         return(users);
     }
 }
Esempio n. 12
0
 public bool UserIndexExists(int index)
 {
     using (var db = new myContext())
     {
         var user = db.Users.Find(index);
         return(UserExists(user));
     }
 }
Esempio n. 13
0
 public void CreateUserSimple(string registerUsername, string registerPassword)
 {
     using (var db = new myContext())
     {
         db.Users.Add(new User(registerUsername, registerPassword));
         db.SaveChanges();
     }
 }
Esempio n. 14
0
        // Superadmin

        public void CreateUserExtended(string username, string password, Role role)
        {
            using (var db = new myContext())
            {
                db.Users.Add(new User(username, password, role));
                db.SaveChanges();
            }
        }
Esempio n. 15
0
 public bool UsernameExists(string username)
 {
     using (var db = new myContext())
     {
         var user = db.Users.Where(x => x.Username == username).SingleOrDefault();
         return(UserExists(user));
     }
 }
Esempio n. 16
0
 public void UpdateUserRole(int userId, Role role)
 {
     using (var db = new myContext())
     {
         var user = db.Users.Find(userId);
         user.Role = role;
         db.SaveChanges();
     }
 }
Esempio n. 17
0
 public void UpdateUserPassword(int userId, string password)
 {
     using (var db = new myContext())
     {
         var user = db.Users.Find(userId);
         user.Password = password;
         db.SaveChanges();
     }
 }
Esempio n. 18
0
 public void UpdateUserUsername(int userId, string username)
 {
     using (var db = new myContext())
     {
         var user = db.Users.Find(userId);
         user.Username = username;
         db.SaveChanges();
     }
 }
Esempio n. 19
0
 public int UnreadMessageCounter()
 {
     using (var db = new myContext())
     {
         var unreadMessages = db.Messages.Where(x => x.Receiver.Id == userRepository.currentUser.Id && x.Read == false).ToList();
         int counter        = unreadMessages.Count;
         return(counter);
     }
 }
Esempio n. 20
0
 public void EditMessageBody(int messageId, string body)
 {
     using (var db = new myContext())
     {
         var message = db.Messages.Find(messageId);
         message.Body = body;
         db.SaveChanges();
     }
 }
Esempio n. 21
0
 public void EditMessageTitle(int messageId, string title)
 {
     using (var db = new myContext())
     {
         var message = db.Messages.Find(messageId);
         message.Title = title;
         db.SaveChanges();
     }
 }
Esempio n. 22
0
        // Type the message
        public void TypeMessage()
        {
            int receiverId = SelectUser();

            if (receiverId < 0)
            {
                return;
            }
            User Receiver = new User();

            using (var db = new myContext())
            {
                Receiver = db.Users.Find(receiverId);
            }
            Console.Clear();
            Various.Header($"{ UserRepository.currentUser.Username} : Send Message to {Receiver.Username}");

            string title;
            string body;

            // Title
            Various.SystemMessageDirections("Title must be max 15 characters long");
            Console.Write("Type the title of the message: ");
            title = Various.TypeLetterByLetterWithoutMaskingSTR(15);

            // Checks if Esc is pressed
            if (title == "")
            {
                Console.WriteLine();
                Various.SystemMessage("Send Message Failed");
                System.Threading.Thread.Sleep(1500);
                return;
            }

            // Body
            Console.WriteLine();
            Console.WriteLine();
            Various.SystemMessageDirections("Body must be max 250 characters long");
            Console.Write("Type the body of the message: ");
            body = Various.TypeLetterByLetterWithoutMaskingSTR(249);

            // Checks if Esc is pressed
            if (body == "")
            {
                Console.WriteLine();
                Various.SystemMessage("Send Message Failed");
                System.Threading.Thread.Sleep(1500);
                return;
            }
            MessageRepository.SendMessage(title, body, receiverId);
            LogRepository.TypeInLog($"{UserRepository.currentUser.Username}", "sent message");
            Console.WriteLine();
            Various.SystemMessageSuccess("Message Sent Successful");
            System.Threading.Thread.Sleep(1500);
            return;
        }
Esempio n. 23
0
 // SuperAdmin
 public void ChangeIdOfUserToDelete(int userId)
 {
     using (var db = new myContext())
     {
         var deletedUserMessages = db.Messages.Include("Sender").Include("Receiver").Where(x => x.Sender.Id == userId || x.Receiver.Id == userId).ToList();
         if (deletedUserMessages.Count != 0)
         {
             foreach (Message message in deletedUserMessages)
             {
                 if (message.Sender.Id == userId)
                 {
                     message.Sender = db.Users.Where(x => x.Username == "DeletedUser").SingleOrDefault();
                     db.SaveChanges();
                 }
                 if (message.Receiver.Id == userId)
                 {
                     message.Receiver = db.Users.Where(x => x.Username == "DeletedUser").SingleOrDefault();
                     db.SaveChanges();
                 }
             }
         }
     }
 }