// Returns a single profile object based on a database query. public static ProfileBar GetProfile(int ViewerId, int ProfileId) { snaptergramEntities db = new snaptergramEntities(); List <users> AllUsers = new List <users>(); using (db) { var data = from u in db.users select u; AllUsers = data.ToList(); } // Selects a user based on ID, if it can't find one then a null is returned. users User = AllUsers.Where(user => user.userId == ProfileId).FirstOrDefault(); // Handles the ocasions when the user cannot be found, or no Id was given. if (User == null) { return(new ProfileBar()); } // Need to create a profile bar object. ProfileBar ProfileHeader = new ProfileBar(); ProfileHeader.ProfileId = User.userId; ProfileHeader.ProfilePicture = User.profilePic; ProfileHeader.ProfileUserName = User.username; ProfileHeader.ViewerId = ViewerId; //NOT YET IMPLEMENTED. ProfileHeader.Following = Followers.IsFollowing(ViewerId, ProfileId);; return(ProfileHeader); }
// Gets all users. public static List <ProfileSwitcher> GetAllProfiles() { snaptergramEntities db = new snaptergramEntities(); List <users> AllUsers = new List <users>(); using (db) { var data = from u in db.users select u; AllUsers = data.ToList(); } // Convert the users to ProfileBar List <ProfileSwitcher> AllResults = new List <ProfileSwitcher>(); foreach (users U in AllUsers) { // Need to create a profile bar object. ProfileSwitcher Converted = new ProfileSwitcher(); Converted.ProfileId = U.userId; Converted.ProfilePicture = U.profilePic; Converted.ProfileUserName = U.username; AllResults.Add(Converted); } return(AllResults); }
// 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); }
// Returns multiple profile objects based on a username search public static List <ProfileBar> GetProfile(int ViewerId, string SearchQuery) { snaptergramEntities db = new snaptergramEntities(); List <users> AllUsers = new List <users>(); using (db) { var data = from u in db.users select u; AllUsers = data.ToList(); } // Selects a user based on username search. user.username.Contains(SearchQuery) AllUsers = AllUsers.Where(user => user.username.IndexOf(SearchQuery, StringComparison.CurrentCultureIgnoreCase) != -1).ToList(); // Convert the users to ProfileBar List <ProfileBar> AllResults = new List <ProfileBar>(); foreach (users U in AllUsers) { // Need to create a profile bar object. ProfileBar Converted = new ProfileBar(); Converted.ProfileId = U.userId; Converted.ProfilePicture = U.profilePic; Converted.ProfileUserName = U.username; Converted.ViewerId = ViewerId; //NOT YET IMPLEMENTED. Converted.Following = Followers.IsFollowing(ViewerId, U.userId); AllResults.Add(Converted); } return(AllResults); }
// Returns true if user is following follow. public static bool IsFollowing(int UserId, int FollowerId) { // If the user ids are identical they must be following if (UserId == FollowerId) { return(true); } // Else the database must be queried snaptergramEntities db = new snaptergramEntities(); List <followers> AllFollowers = new List <followers>(); using (db) { var data = from u in db.followers select u; AllFollowers = data.ToList(); } // Loops through all the likes to see if any match the current user. foreach (followers F in AllFollowers) { if (F.userId == UserId && F.followId == FollowerId) { return(true); } } return(false); }
// 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); }
// Gets the username of the userId; public static string GetUsername(int UserId) { snaptergramEntities db = new snaptergramEntities(); List <users> AllUsers = new List <users>(); using (db) { var data = from u in db.users select u; AllUsers = data.ToList(); } // Selects a user based on ID, if it can't find one then a null is returned. users User = AllUsers.Where(user => user.userId == UserId).FirstOrDefault(); // Handles the ocasions when the user cannot be found, or no Id was given. if (User == null) { return("Unkown User"); } else { return(User.username); } }
// Returns a list of posts made a list of authors. public static List <Post> GetPosts(int ViewerId, List <int> Authors) { snaptergramEntities db = new snaptergramEntities(); List <posts> AllPosts = new List <posts>(); using (db) { var data = from u in db.posts select u; AllPosts = data.ToList(); } List <posts> FollowedPosts = new List <posts>(); // Selects all posts based on author id. foreach (posts P in AllPosts) { if (Authors.Contains(P.userId)) { FollowedPosts.Add(P); } } // List of converted objects. List <Post> AllPostsConverted = new List <Post>(); foreach (posts P in FollowedPosts) { // Convert each post to a complex model post. Post Converted = new Post(); // Post and date do not need to be converted. Converted.PostImage = P.image; Converted.PostDate = P.postDate; // Author Conversion. Converted.PostAuthor = Users.GetAuthor(P.userId, ViewerId, P.postId); // Likes Conversion. Converted.PostLikes = new Like(ViewerId, P.postId, P.likes, Likes.HasLiked(ViewerId, P.postId)); // Comments Conversion. Converted.Comments = Comments.GetPostComments(ViewerId, P.postId); // Add a reply comments object. NewComment NewCom = new NewComment(); NewCom.UserId = ViewerId; NewCom.PostId = P.postId; Converted.PostCommentReply = NewCom; // Add the converted object to the list. AllPostsConverted.Add(Converted); } return(AllPostsConverted); }
// 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); }
// Returns true if a user has liked a post. public static bool HasLiked(int UserId, int PostId) { snaptergramEntities db = new snaptergramEntities(); List <likes> AllLikes = new List <likes>(); using (db) { var data = from u in db.likes select u; AllLikes = data.ToList(); } // Loops through all the likes to see if any match the current user. foreach (likes L in AllLikes) { if (L.userId == UserId && L.postId == PostId) { return(true); } } return(false); }
// 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); }
// Gets all the comments for a given post. public static List <Comment> GetPostComments(int ViewerId, int PostId) { snaptergramEntities db = new snaptergramEntities(); List <comments> AllComments = new List <comments>(); using (db) { var data = from u in db.comments select u; AllComments = data.ToList(); } List <Comment> PostComments = new List <Comment>(); foreach (comments C in AllComments) { if (C.postId == PostId) { // Convert the comment. Comment Converted = new Comment(); // Move across terms that don't need more data work. Converted.CommentAuthorId = C.userId; Converted.CommentText = C.comment; Converted.CommentId = C.commentId; // Replies have not been implemented yet (if ever). Converted.ViewerId = ViewerId; // Gets the username of the comment author. Converted.CommentAuthorUserName = Users.GetUsername(C.userId); PostComments.Add(Converted); } } return(PostComments); }
// Returns a list of all the users a user is follwing. public static List <int> GetFollowers(int UserId) { snaptergramEntities db = new snaptergramEntities(); List <followers> Following = new List <followers>(); using (db) { var data = from u in db.followers select u; Following = data.ToList(); } // Selects all posts based on author id. Following = Following.Where(follow => follow.userId == UserId).ToList(); List <int> Followers = new List <int>(); foreach (followers F in Following) { Followers.Add(F.followId); } return(Followers); }