public void WritePost(string username, string message) { var ctx = new MessagingBoardContext(); var post = new Post { Message = EncryptDecryptProvider.Encrypt(message), Username = username, DatePosted = DateTime.Now }; ctx.Posts.Add(post); ctx.SaveChanges(); Clients.All.receivedNewPost(post.Id, post.Username, EncryptDecryptProvider.Decrypt(post.Message), post.DatePosted); }
// GET api/<controller> public IEnumerable <Post> Get() { var allPosts = this._ctx.Posts.OrderByDescending(x => x.DatePosted).ToList(); foreach (var post in allPosts) { post.Message = EncryptDecryptProvider.Decrypt(post.Message); var allCommentsOnThisPost = _ctx.Comments.Where(p => p.ParentPost.Id == post.Id).ToList(); foreach (var comment in allCommentsOnThisPost) { comment.Message = EncryptDecryptProvider.Decrypt(comment.Message); post.Comments.Add(comment); } } return(allPosts); }
public void AddComment(int postId, string comment, string username) { var ctx = new MessagingBoardContext(); var post = ctx.Posts.FirstOrDefault(p => p.Id == postId); if (post != null) { var newComment = new Comment { ParentPost = post, Message = EncryptDecryptProvider.Encrypt(comment), Username = username, DatePosted = DateTime.Now }; ctx.Comments.Add(newComment); ctx.SaveChanges(); Clients.All.receivedNewComment(newComment.ParentPost.Id, newComment.Id, EncryptDecryptProvider.Decrypt(newComment.Message), newComment.Username, newComment.DatePosted); } }