// Adds a like. public static bool AddLike(int UserId, int PostId) { snaptergramEntities db = new snaptergramEntities(); // Try removing from the database. try { likes ToAdd = new likes(); ToAdd.userId = UserId; ToAdd.postId = PostId; ToAdd.date = DateTime.Now; db.likes.Add(ToAdd); db.SaveChanges(); // Increment total likes counter. UpdateLikeCounter(PostId, 1); } catch { return(false); } return(true); }
// Removes a following relationship. public static bool RemoveFollowing(int UserId, int FollowId) { snaptergramEntities db = new snaptergramEntities(); // User cannot unfollow themselves. if (UserId == FollowId) { return(false); } // Try removing from the database. try { followers ToDelete = db.followers.Find(UserId, FollowId); db.followers.Remove(ToDelete); db.SaveChanges(); } catch { return(false); } return(true); }
// Changes the total likes of a post based on actions. static void UpdateLikeCounter(int PostId, int Ammount) { snaptergramEntities db = new snaptergramEntities(); posts ToChange = db.posts.Find(PostId); ToChange.likes += Ammount; db.Entry(ToChange).State = EntityState.Modified; db.SaveChanges(); }
// Changes a user's profile picture. public static bool UpdateProfilePicture(int UserId, string NewProfilePicture) { snaptergramEntities db = new snaptergramEntities(); users ToChange = db.users.Find(UserId); ToChange.profilePic = NewProfilePicture; db.Entry(ToChange).State = EntityState.Modified; db.SaveChanges(); return(true); }
// Deletes a post from the database. public static bool DeletePost(int PostId) { snaptergramEntities db = new snaptergramEntities(); // Try removing from the database. posts ToDelete = db.posts.Find(PostId); db.posts.Remove(ToDelete); db.SaveChanges(); return(true); }
// Deletes a comment based on Id. public static bool DeleteComment(int CommentId) { snaptergramEntities db = new snaptergramEntities(); // Try removing from the database. try { comments ToDelete = db.comments.Find(CommentId); db.comments.Remove(ToDelete); db.SaveChanges(); } catch { return(false); } return(true); }
// Adds a new comment. public static bool PostComment(NewComment C) { snaptergramEntities db = new snaptergramEntities(); comments Converted = new comments(); Converted.comment = C.CommentText; Converted.postDate = DateTime.Now; Converted.userId = C.UserId; Converted.postId = C.PostId; Converted.permanent = "temporary"; try { db.comments.Add(Converted); db.SaveChanges(); } catch { return(false); } return(true); }
// Removes a like public static bool RemoveLike(int UserId, int PostId) { snaptergramEntities db = new snaptergramEntities(); // Try removing from the database. try { likes ToDelete = db.likes.Find(UserId, PostId); db.likes.Remove(ToDelete); db.SaveChanges(); // Decrement total likes counter. UpdateLikeCounter(PostId, -1); } catch { return(false); } return(true); }
// Adds a new following relationship. public static bool AddFollowing(int UserId, int FollowId) { snaptergramEntities db = new snaptergramEntities(); // Try removing from the database. try { followers ToAdd = new followers(); ToAdd.userId = UserId; ToAdd.followId = FollowId; ToAdd.date = DateTime.Now; db.followers.Add(ToAdd); db.SaveChanges(); } catch { return(false); } return(true); }
// Adds a new post to the database. public static bool AddNewPost(int UserId, string Image) { snaptergramEntities db = new snaptergramEntities(); posts NewPost = new posts(); NewPost.userId = UserId; NewPost.image = Image; NewPost.permanent = "no"; NewPost.postDate = DateTime.Now; NewPost.likes = 0; try { db.posts.Add(NewPost); db.SaveChanges(); } catch { return(false); } return(true); }