public Post GetPostById(int postId)
 {
     using (var context = new HackerNewsDataContext(_connectionString))
     {
         return(context.Posts.First(p => p.Id == postId));
     }
 }
 public User GetByEmail(string email)
 {
     using (var context = new HackerNewsDataContext(_connectionString))
     {
         return(context.Users.First(u => u.Email == email));
     }
 }
 public int CommentCount(int postId)
 {
     using (var context = new HackerNewsDataContext(_connectionString))
     {
         return(context.Comments.Count(c => c.PostId == postId));
     }
 }
 public User GetUserById(int userId)
 {
     using (var context = new HackerNewsDataContext(_connectionString))
     {
         return(context.Users.First(u => u.Id == userId));
     }
 }
 public void AddPost(Post post)
 {
     using (var context = new HackerNewsDataContext(_connectionString))
     {
         context.Posts.InsertOnSubmit(post);
         context.SubmitChanges();
     }
 }
 public void AddComment(Comment comment)
 {
     using (var context = new HackerNewsDataContext(_connectionString))
     {
         context.Comments.InsertOnSubmit(comment);
         context.SubmitChanges();
     }
 }
 public Post UpVote(int postId)
 {
     using (var context = new HackerNewsDataContext(_connectionString))
     {
         Post p = GetPostById(postId);
         p.UpVotes++;
         context.ExecuteCommand("UPDATE Posts SET UpVotes = {0} WHERE Id = {1}", p.UpVotes, postId);
         return(p);
     }
 }
 public IEnumerable <Post> AllPosts()
 {
     using (var context = new HackerNewsDataContext(_connectionString))
     {
         var loadOptions = new DataLoadOptions();
         loadOptions.LoadWith <Post>(p => p.User);
         context.LoadOptions = loadOptions;
         return(context.Posts.ToList());
     }
 }
 public IEnumerable <Post> GetPostsForUser(int userId)
 {
     using (var context = new HackerNewsDataContext(_connectionString))
     {
         var loadOptions = new DataLoadOptions();
         loadOptions.LoadWith <Post>(p => p.User);
         context.LoadOptions = loadOptions;
         return(context.Posts.Where(p => p.UserId == userId).ToList());
     }
 }
 public IEnumerable <Comment> getComments(int postId)
 {
     using (var context = new HackerNewsDataContext(_connectionString))
     {
         var loadOptions = new DataLoadOptions();
         loadOptions.LoadWith <Post>(p => p.User);
         loadOptions.LoadWith <Comment>(c => c.Post);
         context.LoadOptions = loadOptions;
         return(context.Comments.Where(c => c.PostId == postId).ToList());
     }
 }
        public void AddUser(User user, string password)
        {
            var salt = PasswordHelper.GenerateSalt();
            var hash = PasswordHelper.HashPassword(password, salt);

            user.PasswordHash = hash;
            user.PasswordSalt = salt;

            using (var context = new HackerNewsDataContext(_connectionString))
            {
                context.Users.InsertOnSubmit(user);
                context.SubmitChanges();
            }
        }