//Delete User public void DeleteUser(string userName, string password) { if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password)) { User user = GetUser(userName, password); if (user != null) { using (ChatDbContext context = new ChatDbContext()) { try { context.Users.Remove(user); context.SaveChanges(); } catch (Exception ex) { throw new Exception("En Error during removing the user", ex.InnerException); } } } } }
//Add New User to DB public void AddUser(User user) { if (user != null) { using (ChatDbContext context = new ChatDbContext()) { try { user.UserId = context.Users.Count(); context.Users.Add(user); context.SaveChanges(); } catch (Exception ex) { throw new Exception("En Error during registration", ex); } } } else { throw new Exception("The user object is null"); } }
//Get All Message from selected user public ICollection <Message> GetMessagePerUser(User user) { if (user != null) { List <Message> messages = new List <Message>(); using (ChatDbContext context = new ChatDbContext()) { try { messages = context.Messages.Where(m => m.UserName == user.UserName).ToList(); return(messages); } catch (Exception ex) { throw new Exception("En Error during registration", ex); } } } else { throw new Exception("The user object is null"); } }