public void AssignCurrentUser(string username, string password) { using (var db = new myContext()) { currentUser = db.Users.Where(x => x.Username == username && x.Password == password).SingleOrDefault(); } }
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)); } }
// 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); } }
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}"); } }
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); } }
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(); } }
public void DeleteMessage(int messageId) { using (var db = new myContext()) { db.Messages.Remove(db.Messages.Find(messageId)); db.SaveChanges(); } }
public void DeleteUser(int userId) { using (var db = new myContext()) { db.Users.Remove(db.Users.Find(userId)); db.SaveChanges(); } }
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)); } }
public List <User> GetUsers() { using (var db = new myContext()) { var users = db.Users.ToList(); return(users); } }
public bool UserIndexExists(int index) { using (var db = new myContext()) { var user = db.Users.Find(index); return(UserExists(user)); } }
public void CreateUserSimple(string registerUsername, string registerPassword) { using (var db = new myContext()) { db.Users.Add(new User(registerUsername, registerPassword)); db.SaveChanges(); } }
// 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(); } }
public bool UsernameExists(string username) { using (var db = new myContext()) { var user = db.Users.Where(x => x.Username == username).SingleOrDefault(); return(UserExists(user)); } }
public void UpdateUserRole(int userId, Role role) { using (var db = new myContext()) { var user = db.Users.Find(userId); user.Role = role; db.SaveChanges(); } }
public void UpdateUserPassword(int userId, string password) { using (var db = new myContext()) { var user = db.Users.Find(userId); user.Password = password; db.SaveChanges(); } }
public void UpdateUserUsername(int userId, string username) { using (var db = new myContext()) { var user = db.Users.Find(userId); user.Username = username; db.SaveChanges(); } }
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); } }
public void EditMessageBody(int messageId, string body) { using (var db = new myContext()) { var message = db.Messages.Find(messageId); message.Body = body; db.SaveChanges(); } }
public void EditMessageTitle(int messageId, string title) { using (var db = new myContext()) { var message = db.Messages.Find(messageId); message.Title = title; db.SaveChanges(); } }
// 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; }
// 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(); } } } } }