/// <summary> /// This method increment number of likes of post. /// </summary> /// <param name="post">Post to be liked.</param> public void LikePost(vwFriendPost post, vwUser userWhoLikePost) { try { using (BetweenUsEntities context = new BetweenUsEntities()) { int id = context.vwFriendPosts.Where(x => x.PostId == post.PostId).Select(x => x.PostId).FirstOrDefault(); tblPost postToLike = context.tblPosts.Where(x => x.PostId == id).FirstOrDefault(); if (postToLike != null) { postToLike.NumberOfLikes++; context.SaveChanges(); } tblLikedPost likedPost = new tblLikedPost { UserId = userWhoLikePost.UserId, PostId = postToLike.PostId }; context.tblLikedPosts.Add(likedPost); context.SaveChanges(); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); } }
/// <summary> /// This method adds post to DbSet and saves changes to database. /// </summary> /// <param name="postToAdd">Post to be added.</param> /// <returns>True if post is added, false if not.</returns> public bool CreatePost(vwPost postToAdd) { try { using (BetweenUsEntities context = new BetweenUsEntities()) { tblPost newPost = new tblPost { DateOfPost = DateTime.Now.Date, PostContent = postToAdd.PostContent, UserId = postToAdd.UserId, NumberOfLikes = 0 }; context.tblPosts.Add(newPost); context.SaveChanges(); return(true); } } catch (Exception ex) { Debug.WriteLine("Exception" + ex.Message.ToString()); return(false); } }