public string Retweet(int id) { using (ApplicationDbContext db = new ApplicationDbContext()) { //find the post CodePost currPost = db.posts.ToList().Find(x => x.id == id); if (currPost != null) { var manager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext())); var currentUser = manager.FindById(User.Identity.GetUserId()); //currPost.user_id = currentUser.Id; currPost.content = "<a href='/Feed/UserPosts?type=" + currPost.user_id + "'> @" + currPost.userName + " </a> has tweeted: " + currPost.content; currPost.user_id = currentUser.Id; currPost.userName = currentUser.user; //add the new post with the new id/name and handle db.posts.Add(currPost); db.SaveChanges(); return("success"); } return("fail"); } }
public string submitCodePost(string title, string content, string type) { using (ApplicationDbContext db = new ApplicationDbContext()) { CodePost tmp = new CodePost(); content = HttpUtility.JavaScriptStringEncode(content); var store = new UserStore <ApplicationUser>(db); var userManager = new UserManager <ApplicationUser>(store); ApplicationUser user = userManager.FindByNameAsync(User.Identity.Name).Result; var currentUser = userManager.FindById(User.Identity.GetUserId()); if (title != null && content != null && type != null) { tmp.title = title; tmp.content = content; tmp.votes = 0; tmp.user_id = user.Id; tmp.userName = user.user; tmp.userImgPath = currentUser.userImgPath; } bool result = updateHashTags(tmp); if (result == false) { db.posts.Add(tmp); } db.SaveChanges(); return("success"); } }
public JsonResult Reply(int postId, string commentContent) { using (ApplicationDbContext db = new ApplicationDbContext()) { //find the post CodePost currPost = db.posts.ToList().Find(x => x.id == postId); //get current user var manager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(db)); var currentUser = manager.FindById(User.Identity.GetUserId()); if (currPost != null) { commentContent = HttpUtility.JavaScriptStringEncode(commentContent); Comment newComment = new Comment() { content = commentContent, user_id = currPost.user_id, userName = currentUser.user, post_id = postId }; db.postComments.Add(new CommentPost() { Post = currPost, Comment = newComment }); db.SaveChanges(); return(Json(newComment)); } return(Json("fail")); } }
//finds all the hashtags in the post, stores the new ones, updates the existing ones private bool updateHashTags(CodePost post) { bool added = false; using (ApplicationDbContext db = new ApplicationDbContext()) { //extracts all the tags List <string> tags = new List <string>(); var regex = new Regex(@"(?<=#)\w+"); var matches = regex.Matches(post.content); foreach (Match m in matches) { tags.Add(m.ToString()); } //find and updates, not optimised should try O(1) access for every tag, for now this works foreach (string postTag in tags) { HashTag result = db.tags.ToList().Find(t => t.tag == postTag); //this is a new tag if (result == null) { HashTag newTag = new HashTag(); newTag.count = 0; newTag.tag = postTag; db.posts.Add(post); db.hashTags.Add(new HashTagPost() { Hash = newTag, Post = post }); added = true; continue; } result.count++; db.hashTags.Add(new HashTagPost() { Hash = result, Post = post }); db.posts.Add(post); added = true; } db.SaveChanges(); } return(added); }
public JsonResult commentsForPost(int postId) { using (ApplicationDbContext db = new ApplicationDbContext()) { CodePost currPost = db.posts.ToList().Find(x => x.id == postId); if (currPost != null) { var comments = db.postComments.Select(comment => comment.CodePostId == postId); return(Json(comments)); } return(Json("")); } }
public IActionResult VerifyCode([FromBody] CodePost post) { CodeResponse response = _sessionManager.VerifySession(post.Token, post.Code); return(Ok(response)); }
public JsonResult getUserPosts(string orderParam, string type, string count, string hashtag) { var manager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext())); var currentUser = manager.FindById(User.Identity.GetUserId()); if (currentUser.user != null) { ViewBag.username = currentUser.user; } IEnumerable <CodePost> jsonList = null; //number of posts for infinite scroll int cnt = 0; if (!String.IsNullOrEmpty(count)) { cnt = Int32.Parse(count); } using (ApplicationDbContext db = new ApplicationDbContext()) { if (!String.IsNullOrEmpty(hashtag)) //all posts with that hashtag { jsonList = db.hashTags. Where(t => t.Hash.tag == (hashtag)). Select(t => t.Post). ToList(); } else if (type != null) //all posts from that user { //this means ge the current user profile if (type == "") { type = currentUser.Id; } jsonList = from code in db.posts.ToList() where code.user_id == type select code; } else //all posts of current users { jsonList = getFollowedPosts(currentUser); } } var postArr = jsonList.Reverse().ToArray(); //if no post found return empty if (postArr.Length == 0) { return(Json("")); } List <ProfilePostsViewModel> profileList = new List <ProfilePostsViewModel>(); int increment = cnt; if (!String.IsNullOrEmpty(orderParam)) { cnt = 0; increment = postArr.Length; } for (int i = cnt; i < postArr.Length; ++i) { if (i < increment + 5) { ProfilePostsViewModel tmp = new ProfilePostsViewModel(); CodePost post = postArr[i]; tmp.id = post.id; tmp.title = post.title; tmp.content = post.content; tmp.like = post.like; tmp.hate = post.hate; tmp.userName = post.userName; tmp.userImgPath = post.userImgPath; tmp.date = post.date; //uzmemo iz baze votova da li je korisnik na ovom postu lupio like/hate i saljemo ka klijentu using (ApplicationDbContext db = new ApplicationDbContext()) { var liked = (db.votes.ToList().Find(v => v.CodePostId == post.id && v.UserId == currentUser.Id && v.Type == 0)); var hated = (db.votes.ToList().Find(v => v.CodePostId == post.id && v.UserId == currentUser.Id && v.Type == 1)); tmp.liked = (liked != null) ? "You liked this" : "Like"; tmp.hated = (hated != null) ? "You hate this" : "Hate"; //IEnumerable<CommentPost> commentPost tmp.commentList = from c in db.comments.ToList() where c.post_id == post.id select c; profileList.Add(tmp); } } } //Post sorting by date and user name if (!String.IsNullOrEmpty(orderParam)) { orderPosts(orderParam, ref profileList); } return(Json(profileList)); }