Esempio n. 1
0
        public int CreatePost(string title, string content, string[] tags)
        {
            var postPoco = new PostPoco {
                Title = title, Content = content, UserId = Account.Id
            };

            int postId = this.Database.Insert(postPoco);

            foreach (string tagName in tags)
            {
                var tag = this.Database.QueryOne <TagPoco>("SELECT * FROM tags WHERE name=@n;", new NpgsqlParameter("n", tagName));

                if (tag == null)
                {
                    tag = new TagPoco {
                        Name = tagName
                    };
                    tag.TagId = this.Database.Insert(tag);
                }

                int tagId = tag.TagId;

                var postsTagsPoco = new PostsTagsPoco {
                    PostId = postId, TagId = tagId
                };

                this.Database.Insert(postsTagsPoco);
            }

            return(postId);
        }
Esempio n. 2
0
 public static void EditPostContent(PostPoco post, string content)
 {
     using (var conn = new NpgsqlConnection(Blog.ConnectionString))
     {
         var database = new Database(conn);
         var service  = new Service(database);
         service.UpdatePostContent(post, content);
     }
 }
Esempio n. 3
0
 public static void EditPostTitle(PostPoco post, string title)
 {
     using (var conn = new NpgsqlConnection(Blog.ConnectionString))
     {
         var database = new Database(conn);
         var service  = new Service(database);
         service.UpdatePostTitle(post, title);
         Console.WriteLine("You succesfully edited this post title!");
     }
 }
Esempio n. 4
0
        private static void DeletePost(PostPoco post)
        {
            using (var conn = new NpgsqlConnection(ConnectionString))
            {
                var database = new Database(conn);
                var service  = new Service(database);
                service.DeletePost(post);
            }

            Console.WriteLine("You sucesfully deleted this post!");
        }
Esempio n. 5
0
        public List <TagPoco> GetPostTags(PostPoco poco)
        {
            string sql =
                "SELECT tgs.tag_id AS tag_id,tgs.name AS name FROM posts_tags AS pt INNER JOIN tags AS tgs ON pt.post_id=@i AND pt.tag_id = tgs.tag_id;";

            var parametar = new Dictionary <string, object>
            {
                { "i", poco.PostId }
            };

            return(this.Database.Query <TagPoco>(sql, parametar));
        }
Esempio n. 6
0
        public static void ViewPost(PostPoco post)
        {
            using (var conn = new NpgsqlConnection(Blog.ConnectionString))
            {
                var database = new Database(conn);
                var service  = new Service(database);

                var tags = service.GetPostTags(post);

                Console.WriteLine("|------------------------------------------------------------------------------------------|");
                Console.WriteLine($"Title: {post.Title.Trim()}");
                Console.WriteLine("|------------------------------------------------------------------------------------------|");
                Console.WriteLine($"Content: {post.Content.Trim()}");
                Console.WriteLine("|------------------------------------------------------------------------------------------|");
                Console.WriteLine($"Tags: {string.Join(", ", tags.Select(t => t.Name)).Trim()}");
                Console.WriteLine("|------------------------------------------------------------------------------------------|");

                Blog.CurrentPost = post;
            }
        }
Esempio n. 7
0
        private static void DeleteProcess(PostPoco post)
        {
            Console.WriteLine("Once you've deleted a post it can't never be restored!");
            Console.Write("Are you sure you want to delete this post? (Y/N): ");
            string option = Console.ReadLine()?.ToLowerInvariant().Trim();

            if (option == "y")
            {
                Console.WriteLine("This requires your password!");
                Console.Write("Your password: "******"Password incorrect returning to the post!");
                }
            }
        }
Esempio n. 8
0
        public void DeletePost(PostPoco poco)
        {
            var parametar = new Dictionary <string, object>()
            {
                { "i", poco.PostId }
            };

            var comments  = this.Database.Query <CommentPoco>("SELECT * FROM comments WHERE post_id=@i;", parametar);
            var postsTags = this.Database.Query <PostsTagsPoco>("SELECT * FROM posts_tags WHERE post_id=@i;", parametar);

            foreach (var commentPoco in comments)
            {
                this.Database.Delete(commentPoco);
            }

            foreach (var postsTagsPoco in postsTags)
            {
                this.Database.Delete(postsTagsPoco);
            }

            this.Database.Delete(poco);
        }
Esempio n. 9
0
 public List <CommentPoco> GetPostsAllCommentars(PostPoco poco)
 {
     return(this.Database.Query <CommentPoco>("SELECT * FROM comments WHERE post_id=@i;", new NpgsqlParameter("i", poco.PostId)));
 }
Esempio n. 10
0
 public void UpdatePostTitle(PostPoco post, string title)
 {
     post.Title = title;
     this.Database.Update(post);
 }
Esempio n. 11
0
 public void UpdatePostContent(PostPoco post, string content)
 {
     post.Content = content;
     this.Database.Update(post);
 }
Esempio n. 12
0
        private static void AllComments(PostPoco post)
        {
            List <CommentPoco> comments;

            using (var conn = new NpgsqlConnection(ConnectionString))
            {
                var database = new Database(conn);
                var service  = new Service(database);
                comments = service.GetPostsAllCommentars(post);
            }

            if (comments.Count >= 1)
            {
                bool choosingComment = true;

                while (choosingComment)
                {
                    for (int i = 0; i < comments.Count; i++)
                    {
                        string shortContent = comments[i].Content.Length > 7
                            ? comments[i].Content.Substring(0, 7) + "..."
                            : comments[i].Content;
                        Console.WriteLine($"{i + 1} | {shortContent} | {comments[i].AuthorName}");
                    }

                    Console.WriteLine("Choose a comment to view or edit!\n");

                    Console.Write("You choosed: ");

                    string line = Console.ReadLine() ?? " ";

                    if (line.ToLowerInvariant().Trim() == "return")
                    {
                        choosingComment = false;
                    }
                    else
                    {
                        bool isNumber = int.TryParse(line, out int selectedIndex);
                        selectedIndex--;
                        if (isNumber)
                        {
                            if (selectedIndex >= 0 && selectedIndex <= comments.Count - 1)
                            {
                                Console.WriteLine("Please choose between view and edit");
                                Console.WriteLine("Type 'view' to view the whole comment");
                                Console.WriteLine(
                                    "Type 'edit' to edit this comment (Only if you have the right to do it)\n");

                                Console.Write("You want to: ");
                                string option = Console.ReadLine() ?? " ";
                                if (option.ToLowerInvariant().Trim() == "view")
                                {
                                    Console.WriteLine(
                                        $"\n{comments[selectedIndex].Content}\n\nAuthor:{comments[selectedIndex].AuthorName}\n");
                                }
                                else if (option.ToLowerInvariant().Trim() == "edit")
                                {
                                    if (comments[selectedIndex].UserId == Account.Id)
                                    {
                                        EditComment(comments[selectedIndex]);
                                    }
                                    else
                                    {
                                        Console.WriteLine("You dont have permission to edit this comment!");
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine($"Please choose between 1 and {comments.Count}\n");
                            }
                        }
                        else
                        {
                            Console.WriteLine("You need to type a number!\n");
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("They aren't any comments on this post!");
            }
        }
Esempio n. 13
0
        private static void PostInterface(PostPoco post)
        {
            if (Account.Id == post.UserId)
            {
                Console.WriteLine("DISCLAIMER: You can edit this post!");
                Console.WriteLine("Type: edit help to see how to edit!");
            }

            Console.WriteLine("Type 'all comments' to see all comments on this post! (If any)");
            Console.WriteLine("Type 'my comments' to see your comments on this post! (If any)");
            Console.WriteLine("Type 'comment post' to comment on this post!");
            Console.WriteLine("Type 'return' to return to the blog!");
            Console.WriteLine("Type 'refresh' to view again this post!\n");

            while (true)
            {
                Console.Write("Blog -->#Post: ");
                string line = Console.ReadLine()?.Trim().ToLowerInvariant().Replace(" ", string.Empty);

                if (!string.IsNullOrEmpty(line))
                {
                    if (line == "return")
                    {
                        break;
                    }

                    switch (line)
                    {
                    case "clear":
                        Console.Clear();
                        break;

                    case "refresh":
                        Post.ViewPost(CurrentPost);
                        break;

                    case "commentpost":
                        CreateCommentInterface();
                        break;

                    case "mycomments":
                        ShowUserComments();
                        break;

                    case "allcomments":
                        Blog.AllComments(CurrentPost);
                        break;

                    case "delete":
                        Blog.DeleteProcess(post);
                        break;

                    case "edithelp":
                        CommandPrinter.ShowPostEdits();
                        break;

                    case "edittitle":
                        Console.Write("New title: ");
                        Post.EditPostTitle(CurrentPost, Console.ReadLine() ?? " ");
                        break;

                    case "editcontent":
                        EditPostContentInterface();
                        break;

                    default:
                        Console.WriteLine("Your command was invalid!");
                        break;
                    }
                }
            }
        }
Esempio n. 14
0
 public static void ChoosePost(PostPoco post)
 {
     Post.ViewPost(post);
     PostInterface(post);
 }