Exemplo n.º 1
0
        public void DeletePost(int postId)
        {
            // Get handle on post by PostID.
            Post post = postRepo.ById(postId);

            // If current user owns post, delete it.
            if (post.ProfileId == currentProfile.id)
            {
                // Burrow into the nested dependencies and delete them on the way out.
                // Pattern: (1)prep list, (2)fill list, (3)loop list, (4)repeat pattern on dependencies, (5)delete record.

                List <Comment> comments = new List <Comment>(); // (1)Prep list.
                foreach (Comment c in commentRepo.ByPostId(postId))
                {
                    comments.Add(c);
                }                               // (2)Fill list.
                foreach (Comment c in comments) // (3)Loop list.
                {
                    // (4)Repeat pattern on dependencies.
                    List <Like> commentLikes = new List <Like>();
                    foreach (Like l in likeRepo.ByTypeAndId(2, c.CommentId))
                    {
                        commentLikes.Add(l);
                    }
                    foreach (Like l in commentLikes)
                    {
                        likeRepo.DeleteLike(l);
                    }

                    commentRepo.DeleteComment(c);
                }

                List <Like> postLikes = new List <Like>();
                foreach (Like l in likeRepo.ByTypeAndId(1, postId))
                {
                    postLikes.Add(l);
                }
                foreach (Like l in postLikes)
                {
                    likeRepo.DeleteLike(l);
                }

                // (5)Delete record.
                postRepo.DeletePost(postRepo.ById(postId));
            }
        }
Exemplo n.º 2
0
        public List <CommentModel> SearchComments(int postId, int skip, int take, [FromBody] StringModel searchText)
        {
            if (searchText.str == "NULL")
            {
                return(null);
            }

            Post post = postRepo.ById(postId);

            if (post.PrivacyLevel > friendRepo.RelationshipTier(currentProfile.id, post.ProfileId))
            {
                return(null);
            }

            string resultsKey = $"{postId}commentSearch";

            if (skip == 0)
            {
                // Prep list for matches. Each index contains a key value pair of <CommentId, searchPoints>.
                List <KeyValuePair <int, int> > matches = new List <KeyValuePair <int, int> >();

                // Split search terms into array of search terms.
                string[] searchTerms = searchText.str.Split(' ');

                // Define how many points an exact match is worth.
                int exactMatchWorth = 3;

                // Loop through all profiles in the database.
                foreach (Comment c in commentRepo.ByPostId(postId))
                {
                    // Define points variable and start it at 0.
                    int points = 0;

                    string[] contentTerms = c.Content.Split(' ');

                    foreach (string contentTerm in contentTerms)
                    {
                        string lcContentTerm = contentTerm.ToLower();

                        // Loop through search terms.
                        foreach (string searchTerm in searchTerms)
                        {
                            // Convert search term to lowercase.
                            string lcSearchTerm = searchTerm.ToLower();

                            // If the terms are an exact match, add an exact match worth of points.
                            if (lcSearchTerm == lcContentTerm)
                            {
                                points += exactMatchWorth;
                            }

                            // Else if the terms are a partial match, add 1 point.
                            else if (lcSearchTerm.Contains(lcContentTerm) || lcContentTerm.Contains(lcSearchTerm))
                            {
                                points++;
                            }
                        }
                    }

                    // If the comment earned any points, add its id to the list of matches.
                    if (points > 0)
                    {
                        matches.Add(new KeyValuePair <int, int>(c.CommentId, points));
                    }
                }

                // Sort match results by points.
                matches.Sort((pair1, pair2) => pair1.Value.CompareTo(pair2.Value));

                // Prep list for preped results.
                List <int?> commentIds = new List <int?>();

                // Loop through matches.
                foreach (KeyValuePair <int, int> match in matches)
                {
                    // Add preped result to results.
                    commentIds.Add(match.Key);
                }

                sessionResults.AddResults(resultsKey, commentIds);
            }

            List <CommentModel> commentModels = new List <CommentModel>();

            foreach (int commentId in sessionResults.GetResultsSegment(resultsKey, skip, take))
            {
                commentModels.Add(GetCommentModel(commentId));
            }

            // Return search results to user.
            return(commentModels);
        }
Exemplo n.º 3
0
        public void DeleteImage(int imageId)
        {
            // Get a handle on the image record by ImageID provided.
            Models.Image image = imageRepo.ById(imageId);

            // Validate image ownership.
            if (image.ProfileId == currentProfile.id)
            {
                // Burrow into the nested dependencies and delete them on the way out.
                // Pattern: (1)prep list, (2)fill list, (3)loop list, (4)repeat pattern on dependencies, (5)delete record.

                List <Post> posts = new List <Post>(); // (1)Prep list.
                foreach (Post p in postRepo.Posts.Where(p => p.ImageId == imageId))
                {
                    posts.Add(p);
                }                         // (2)Fill list.
                foreach (Post p in posts) // (3)Loop list.
                {
                    // (4)Repeat pattern on dependencies.
                    List <Comment> comments = new List <Comment>();
                    foreach (Comment c in commentRepo.ByPostId(imageId))
                    {
                        comments.Add(c);
                    }
                    foreach (Comment c in comments)
                    {
                        List <Like> commentLikes = new List <Like>();
                        foreach (Like l in likeRepo.ByTypeAndId(2, c.CommentId))
                        {
                            commentLikes.Add(l);
                        }
                        foreach (Like l in commentLikes)
                        {
                            likeRepo.DeleteLike(l);
                        }

                        commentRepo.DeleteComment(c);
                    }

                    List <Like> postLikes = new List <Like>();
                    foreach (Like l in likeRepo.ByTypeAndId(1, p.PostId))
                    {
                        postLikes.Add(l);
                    }
                    foreach (Like l in postLikes)
                    {
                        likeRepo.DeleteLike(l);
                    }

                    // (5)Delete record.
                    postRepo.DeletePost(p);
                }

                // If the user is using the image to be deleted as a profile picture, give them their default profile picture.
                if (currentProfile.profile.ProfilePicture == imageId)
                {
                    // Get profile by current user's ProfileID.
                    // (By doing this, instead of using currentUser.profile, it gaurentees that this is the latest version of the profile.)
                    Profile profile = profileRepo.ById(currentProfile.id);
                    profile.ProfilePicture = 0;         // Give default profile picture ImageID.
                    profileRepo.SaveProfile(profile);   // Commit profile changes.
                    currentProfile.SetProfile(profile); // Update CurrentProfile in session.
                }

                // Remove fullsize and thumbnail images from file system.
                DeleteFromFileSystem(image.Name, env.WebRootPath);

                // Delete image record from database.
                imageRepo.DeleteImage(image);
            }
        }