Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            logger.Info("Program started");
            try
            {
                string choice;
                do
                {
                    Console.WriteLine("Enter your selection:");
                    Console.WriteLine("1) Display all blogs");
                    Console.WriteLine("2) Add Blog");
                    Console.WriteLine("3) Create Post");
                    Console.WriteLine("4) Display Posts");
                    Console.WriteLine("5) Delete Blog");
                    Console.WriteLine("6) Edit Blog");
                    Console.WriteLine("Enter q to quit");
                    choice = Console.ReadLine();


                    if (choice == "1")
                    {
                        var db = new BloggingContext();
                        logger.Info("Option 1 selected");
                        // Display all Blogs from the database
                        var bId = db.Blogs.OrderBy(b => b.BlogId);

                        if (bId != null)
                        {
                            Console.WriteLine("All blogs in the database:");
                            foreach (var blogName in bId)
                            {
                                Console.Write(blogName.BlogId + " ");
                                Console.WriteLine(blogName.Name);
                            }
                        }
                        else
                        {
                            {
                                logger.Info("No blogs available");
                            }
                        }
                    }
                    else if (choice == "2")
                    {
                        var  db   = new BloggingContext();
                        Blog blog = InputBlog(db);
                        if (blog != null)
                        {
                            //blog.BlogId = BlogId;
                            db.AddBlog(blog);
                            logger.Info("Blog added - {name}", blog.Name);
                        }
                    }

                    else if (choice == "3")
                    {
                        //Add title
                        var db = new BloggingContext();
                        Console.WriteLine("Enter the post title:");
                        var title = Console.ReadLine();
                        if (title != "")
                        {
                            var postTitle = new Post {
                                Title = title
                            };

                            logger.Info("Post added - {title}", title);
                            //Add post content
                            Console.WriteLine("Enter post content:");
                            var content     = Console.ReadLine();
                            var postContent = new Post {
                                Content = content
                            };
                            db.AddPost(postTitle);
                            db.AddPost(postContent);
                            logger.Info("Post added - {content}", content);
                        }
                        else
                        {
                            logger.Info("Post title cannot be null");
                        }
                    }
                    else if (choice == "4")
                    {
                        var db = new BloggingContext();
                        logger.Info("Option 4 selected");
                        // Display all posts from the database
                        var pID = db.Posts.OrderBy(b => b.PostId);

                        if (pID != null)
                        {
                            Console.WriteLine("All {posts} in the database:");
                            foreach (var postContent in pID)
                            {
                                Console.Write(postContent.PostId + " ");
                                Console.WriteLine(postContent.Title);
                                Console.WriteLine(postContent.Content);
                            }
                        }
                        else
                        {
                            {
                                logger.Info("No posts available");
                            }
                        }
                    }

                    else if (choice == "5")
                    {
                        // delete blog
                        Console.WriteLine("Choose the blog to delete:");
                        var db   = new BloggingContext();
                        var blog = GetBlog(db);
                        if (blog != null)
                        {
                            // TODO: delete blog
                            db.DeleteBlog(blog);
                            logger.Info($"Blog (id: {blog.BlogId}) deleted");
                        }
                    }
                    else if (choice == "6")
                    {
                        // edit blog
                        Console.WriteLine("Choose the blog to edit:");
                        var db   = new BloggingContext();
                        var blog = GetBlog(db);
                        if (blog != null)
                        {
                            // input blog
                            Blog UpdatedBlog = InputBlog(db);
                            if (UpdatedBlog != null)
                            {
                                UpdatedBlog.BlogId = blog.BlogId;
                                db.EditBlog(UpdatedBlog);
                                logger.Info($"Blog (id: {blog.BlogId}) updated");
                            }
                        }
                    }
                } while (choice.ToLower() != "q");
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }

            logger.Info("Program ended");
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            logger.Info("Program started");

            try
            {
                var    db = new BloggingContext();
                string input;


                do
                {
                    Console.WriteLine("1) Display all blogs.");
                    Console.WriteLine("2) Add a blog.");
                    Console.WriteLine("3) Create post.");
                    Console.WriteLine("4) Display posts.");
                    Console.WriteLine("press any other key to quit.");
                    Console.WriteLine("5) Delete Blog");
                    Console.WriteLine("6) Edit Blog");

                    input = Console.ReadLine();

                    //display all blogs
                    if (input == "1")
                    {
                        var query = db.Blogs.OrderBy(b => b.Name);

                        int count = db.Blogs.Count();
                        Console.WriteLine($"{count} blogs in the database:");
                        foreach (var item in query)
                        {
                            Console.WriteLine(item.Name);
                        }
                    }

                    //create new blog
                    if (input == "2")
                    {
                        Blog blog = InputBlog(db);
                        if (blog != null)
                        {
                            db.AddBlog(blog);
                            logger.Info("Blog added - {name}", blog.Name);
                        }
                    }

                    //create new post
                    if (input == "3")
                    {
                        var query = db.Blogs.OrderBy(b => b.Name);
                        foreach (var item in query)
                        {
                            Console.WriteLine($"ID: {item.BlogId}) Name: {item.Name}");
                        }


                        Console.WriteLine("Enter name of the blog to post to");

                        string Pto = Console.ReadLine();


                        var exists = db.Blogs.Any(b => b.Name.Contains($"{Pto}"));

                        if (exists == true)
                        {
                            var         post       = new Post();
                            var         BlogSearch = db.Blogs.Where(b => b.Name.Contains($"{Pto}"));
                            List <Blog> blog       = new List <Blog>(BlogSearch);
                            var         IdSearch   = db.Blogs.Where(b => b.Name.Contains($"{Pto}")).Select(b => b.BlogId);
                            List <int>  Id         = new List <int>(IdSearch);

                            post.Blog   = blog[0];
                            post.BlogId = Id[0];

                            Console.WriteLine("Please enter a title.");
                            post.Title = Console.ReadLine();

                            Console.WriteLine("Please enter the post content.");
                            post.Content = Console.ReadLine();

                            db.AddPost(post);
                            logger.Info("Post Added");
                        }

                        else
                        {
                            Console.WriteLine("Blog not found");
                        }
                    }

                    //display posts
                    if (input == "4")
                    {
                        var query = db.Blogs.OrderBy(b => b.Name);
                        foreach (var item in query)
                        {
                            Console.WriteLine($"ID: {item.BlogId}) Name: {item.Name}");
                        }


                        Console.WriteLine("Enter name of the blog to display posts from");

                        string display = Console.ReadLine();


                        var exists = db.Blogs.Any(b => b.Name.Contains($"{display}"));

                        if (exists == true)
                        {
                            var        IdSearch = db.Blogs.Where(b => b.Name.Contains($"{display}")).Select(b => b.BlogId);
                            List <int> Id       = new List <int>(IdSearch);
                            int        BlogId   = Id[0];

                            int count = db.Posts.Where(b => b.BlogId.Equals(Id[0])).Count();
                            Console.WriteLine($"{count} Posts in this Blog.");

                            var Dposts = db.Posts.Where(b => b.BlogId.Equals(Id[0]));
                            foreach (var item in Dposts)
                            {
                                Console.WriteLine($"Blog: {display} \nTitle: {item.Title} \nContent: {item.Content}");
                            }
                        }

                        else
                        {
                            Console.WriteLine("Blog not found");
                        }
                    }
                    else if (input == "5")
                    {
                        // delete blog
                        Console.WriteLine("Choose the blog to delete:");
                        var blog = GetBlog(db);
                        if (blog != null)
                        {
                            // delete blog
                            db.DeleteBlog(blog);
                            logger.Info($"Blog (id: {blog.BlogId}) deleted");
                        }
                    }

                    else if (input == "6")
                    {
                        // edit blog
                        Console.WriteLine("Choose the blog to edit:");
                        var blog = GetBlog(db);
                        if (blog != null)
                        {
                            // input blog
                            Blog UpdatedBlog = InputBlog(db);
                            if (UpdatedBlog != null)
                            {
                                UpdatedBlog.BlogId = blog.BlogId;
                                db.EditBlog(UpdatedBlog);
                                logger.Info($"Blog (id: {blog.BlogId}) updated");
                            }
                        }
                    }
                }while(input == "1" || input == "2" || input == "3" || input == "4" || input == "5");
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }

            logger.Info("Program ended");
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            logger.Info("Program started");
            String menuChoice;
            int idChoice;
            Boolean loop = true;
            var db = new BloggingContext();

            try
            {
                do
                {
                    Console.WriteLine("Enter your selection:");
                    Console.WriteLine("1) Display all blogs");
                    Console.WriteLine("2) Add Blog");
                    Console.WriteLine("3) Create Post");
                    Console.WriteLine("4) Display Posts");
                    Console.WriteLine("5) Delete Blog");
                    Console.WriteLine("6) Edit Blog");
                    Console.WriteLine("Enter q to quit");
                    logger.Info("User input taken");
                    menuChoice = Console.ReadLine();

                    switch(menuChoice){
                        case "1":
                            logger.Info("1) selected");
                            // Display all Blogs from the database
                            var query = db.Blogs.OrderBy(b => b.Name);

                            Console.WriteLine(query.Count() + " Blogs returned");
                            if(query.Count() > 0){
                                foreach (var item in query)
                                {
                                    Console.WriteLine(item.Name);
                                }
                            }
                            break;
                        case "2":
                            logger.Info("2) selected");
                            // Create and save a new Blog
                            //Console.Write("Enter a name for a new Blog: ");
                            //var blog = new Blog { Name = Console.ReadLine() };
                            //var name = Console.ReadLine();

                            //var db = new BloggingContext();
                            Blog blog = InputBlog(db);
                            if (blog != null)
                            {
                                //blog.BlogId = BlogId;
                                db.AddBlog(blog);
                                logger.Info("Blog added - {name}", blog.Name);
                            }
                                //var db = new BloggingContext();
                                // check for unique name

                            // if(name.Equals("")){
                            //     logger.Error("Blog name cannot be null");
                            // } else{
                            //     var blog = new Blog { Name = name };

                            //     db.AddBlog(blog);
                            //     logger.Info("Blog added - {name}", name);
                            // }
                            break;
                        case "3":
                            logger.Info("3) selected");
                            // Display all Blogs from the database
                            Console.WriteLine("Select the blog you would like to post to:");
                            var queryBlogPost = db.Blogs.OrderBy(b => b.BlogId);

                            foreach (var item in queryBlogPost)
                            {
                                Console.WriteLine(item.BlogId +") "+ item.Name);
                            }
                            try{
                                idChoice = Int16.Parse(Console.ReadLine());
                                var existingId = queryBlogPost.Where(i => i.BlogId == idChoice).Count();
                                if(existingId > 0){
                                    //Enter Title - check for title existing
                                    Console.Write("Enter the Post title: ");
                                    var postTitle = Console.ReadLine();
                                    if(postTitle.Equals("")){
                                        logger.Error("Post title cannot be null");
                                    } else{
                                        //Enter Content
                                        Console.Write("Enter the Post content: ");
                                        var postContent = Console.ReadLine();
                                        var post = new Post{Title = postTitle, Content = postContent, BlogId = idChoice};
                                        db.AddPost(post);
                                        logger.Info("Post added - {postTitle}", postTitle);
                                    }
                                } else{
                                    logger.Error("There are no Blogs saved with that Id");
                                }
                            }catch{
                                logger.Error("Invalid Blog ID");
                            }
                            break;
                        case "4":
                            logger.Info("4) selected");
                            // Display all Blogs from the database
                            Console.WriteLine("Select the blog's posts to display");
                            var queryBlogDisplay = db.Blogs.OrderBy(b => b.BlogId);
                            Console.WriteLine("0) Posts from all blogs");
                            foreach (var item in queryBlogDisplay)
                            {
                                Console.WriteLine(item.BlogId +") "+ item.Name);
                            }
                            try{
                                idChoice = Int16.Parse(Console.ReadLine());
                                if(idChoice == 0){
                                    var postQuery = db.Posts.OrderBy(p => p.PostId);

                                    Console.WriteLine(postQuery.Count() + " Blogs returned");
                                    if(postQuery.Count() > 0){
                                        foreach (var item in postQuery)
                                        {
                                            Console.WriteLine("Blog: " + item.Blog.Name);
                                            Console.WriteLine("Title: " + item.Title);
                                            Console.WriteLine("Content: " + item.Content);
                                        }
                                    }
                                }else{
                                    var postQuery = db.Posts.OrderBy(p => p.PostId).Where(p => p.BlogId == idChoice);
                                    Console.WriteLine(postQuery.Count() + " Blogs returned");
                                    if(postQuery.Count() > 0){
                                        foreach (var item in postQuery)
                                        {
                                            Console.WriteLine("Blog: " + item.Blog.Name);
                                            Console.WriteLine("Title: " + item.Title);
                                            Console.WriteLine("Content: " + item.Content);
                                        }
                                    }
                                }
                            }catch{
                                logger.Error("Invalid Blog ID");
                            }
                            break;
                        case "5":
                            logger.Info("5) selected");
                            //var db = new BloggingContext();
                            var blog_here = GetBlog(db);
                            if (blog_here != null)
                            {
                                // delete blog
                                db.DeleteBlog(blog_here);                                logger.Info($"Blog (id: {blog_here.BlogId}) deleted");
                            }

                            Console.WriteLine("Choose the blog to delete:");
                            break;
                        case "6":
                            logger.Info("6) selected");
                            // edit blog
                            Console.WriteLine("Choose the blog to edit:");
                            //var db = new BloggingContext();
                            var blog_there = GetBlog(db);
                            if (blog_there != null)
                            {
                            // input blog
                            Blog UpdatedBlog = InputBlog(db);
                            if (UpdatedBlog != null)
                            {
                                UpdatedBlog.BlogId = blog_there.BlogId;
                                db.EditBlog(UpdatedBlog);
                                logger.Info($"Blog (id: {blog_there.BlogId}) updated");
                            }                            }
                            break;
                        default:
                            logger.Info("User has quit");
                            loop = false;
                            break;
                    }

                } while (loop);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }

            logger.Info("Program ended");
        }
Ejemplo n.º 4
0
        public static void Main(string[] args)
        {
            //logger.Info("Program started");
            //try
            //{

            bool inProgram = true;

            do
            {
                Console.WriteLine("Enter your selection: ");
                Console.WriteLine("1) Display all Blogs");
                Console.WriteLine("2) Add Blog");
                Console.WriteLine("3) Edit Blog");
                Console.WriteLine("4) Delete Blog");
                Console.WriteLine("5) Create Post");
                Console.WriteLine("6) Display Posts");
                Console.WriteLine("7) Edit Post");
                Console.WriteLine("8) Delete Post");
                Console.WriteLine("Enter q to quit");
                string response = Console.ReadLine();

                switch (response)
                {
                case "1":
                {
                    var db = new BloggingContext();
                    db.DisplayBlogs();
                    db.SaveChanges();
                    break;
                }

                case "2":
                {
                    var db = new BloggingContext();
                    db.AddBlogs();
                    db.SaveChanges();
                    break;
                }

                case "3":
                {
                    var db = new BloggingContext();
                    db.EditBlog();
                    db.SaveChanges();
                    break;
                }

                case "4":
                {
                    var db = new BloggingContext();
                    db.DeleteBlog();
                    db.SaveChanges();
                    break;
                }

                case "5":
                {
                    var db = new BloggingContext();
                    db.CreatePost();
                    db.SaveChanges();
                    break;
                }

                case "6":
                {
                    var db = new BloggingContext();
                    db.DisplayPosts();
                    db.SaveChanges();
                    break;
                }

                case "7":
                {
                    var db = new BloggingContext();
                    db.EditPost();
                    db.SaveChanges();
                    break;
                }

                case "8":
                {
                    var db = new BloggingContext();
                    db.DeletePost();
                    db.SaveChanges();
                    break;
                }

                case "q":
                {
                    inProgram = false;
                    break;
                }
                }
            } while (inProgram);



            //    // Display all Blogs from the database
            //    var query = db.Blogs.OrderBy(b => b.Name);

            //    Console.WriteLine("All blogs in the database:");
            //    foreach (var item in query)
            //    {
            //        Console.WriteLine(item.Name);
            //    }
        }
Ejemplo n.º 5
0
        public static void Main(string[] args)
        {
            logger.Info("Program started");
            string choice = "";

            do
            {
                try
                {
                    DisplayMenu();
                    var dbContext = new BloggingContext();

                    logger.Info("User choice: {Choice}", choice);

                    if (choice == "3") //add post
                    {
                        Console.Write("Enter a Blog name for a new Post: ");
                        var Blogname = Console.ReadLine();

                        Blog MyBlog = dbContext.Blogs.Where(id => id.Name == Blogname).FirstOrDefault();

                        Console.WriteLine("Enter Title: ");
                        var myTitle = Console.ReadLine();
                        Console.WriteLine("\nEnter Content: ");
                        var myContent = Console.ReadLine();

                        //add post
                        Post post = new Post {
                            Title = myTitle, Content = myContent, BlogId = MyBlog.BlogId
                        };
                        dbContext.AddPost(post);
                    }
                    else if (choice == "4") //view posts per blog

                    {
                        /* prompt user for the name of the blog they would like to view posts from
                         * save input
                         * query blogs by name
                         * loop through blog for each post
                         * display each post during the loop
                         */
                        Console.WriteLine("Enter the blog name you would want to view");
                        var Blogname = Console.ReadLine();

                        Blog MyBlog = dbContext.Blogs.Where(id => id.Name == Blogname).FirstOrDefault();
                        Console.WriteLine("Posts for blog name " + Blogname);

                        IQueryable <Post> PostResults = dbContext.Posts.Where(b => b.BlogId == MyBlog.BlogId);
                        foreach (Post p in PostResults)
                        {
                            Console.WriteLine("Title: {0}\nContent: {1}\n\n", p.Title, p.Content);
                        }
                    }
                    else if (choice == "2") //add blog
                    {
                        Console.Write("Enter a name for a new Blog: ");
                        var name = Console.ReadLine();

                        var blog = new Blog {
                            Name = name
                        };
                        dbContext.AddBlog(blog);
                    }
                    else if (choice == "1") //display all blogs
                    {
                        Console.WriteLine("All blogs in the database:");

                        var query = dbContext.Blogs.OrderBy(b => b.Name);
                        foreach (var item in query)
                        {
                            Console.WriteLine("BlogID: {0}\nBlogName: {1}", item.BlogId, item.Name);
                        }
                    }
                    else if (choice == "5") //delete blog
                    {
                        Console.Write("Enter blog name to delete: ");
                        var  Blogname = Console.ReadLine();
                        Blog MyBlog   = dbContext.Blogs.Where(id => id.Name == Blogname).FirstOrDefault();
                        dbContext.DeleteBlog(MyBlog);
                    }
                }



                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    Console.ReadLine();

                    logger.Error(ex.Message);
                    Console.ReadLine();
                }
            } while (choice == "1" || choice == "2" || choice == "3" || choice == "4" || choice == "5");
            Console.WriteLine("Press enter to quit");
            string x = Console.ReadLine();

            logger.Info("Program ended");
            void DisplayMenu()
            {
                Console.WriteLine("1) Display All Blogs");
                Console.WriteLine("2) Add Blogs");
                Console.WriteLine("3) Create Post");
                Console.WriteLine("4)  Display posts for blogs");
                Console.WriteLine("5) Delete Blog");
                Console.WriteLine("Enter to quit");
                // input selection
                choice = Console.ReadLine();
            }
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            logger.Info("Program started");

            try
            {
                string choice;
                //string secondChoice;
                do
                {
                    Console.WriteLine("Enter your selection:");
                    Console.WriteLine("1) Display all blogs");
                    Console.WriteLine("2) Add Blog");
                    Console.WriteLine("3) Create Post");
                    Console.WriteLine("4) Display Posts");
                    Console.WriteLine("5) Delete Blog");
                    Console.WriteLine("6) Edit Blog");
                    Console.WriteLine("Enter q to quit");
                    choice = Console.ReadLine();
                    Console.Clear();
                    logger.Info("Option {choice} selected", choice);

                    if (choice == "1")
                    {
                        // display blogs
                        var db    = new BloggingContext();
                        var query = db.Blogs.OrderBy(b => b.Name);

                        Console.WriteLine($"{query.Count()} Blogs returned");
                        foreach (var item in query)
                        {
                            Console.WriteLine(item.Name);
                        }
                    }
                    else if (choice == "2")
                    {
                        // Add blog
                        var  db   = new BloggingContext();
                        Blog blog = InputBlog(db);
                        if (blog != null)
                        {
                            //blog.BlogId = BlogId;
                            db.AddBlog(blog);
                            logger.Info("Blog added - {name}", blog.Name);
                        }
                    }
                    else if (choice == "3")
                    {
                        // Add Post
                        Console.WriteLine("Select the blog you would post to:  ");
                        var db   = new BloggingContext();
                        var blog = GetBlog(db);
                        if (blog != null)
                        {
                            var post = new Post();
                            post.Blog   = blog;
                            post.BlogId = blog.BlogId;
                            Console.WriteLine("Enter name of post");
                            post.Title = Console.ReadLine();
                            Console.WriteLine("Enter the Content");
                            post.Content = Console.ReadLine();

                            //blog.Posts.Add(post);
                            db.AddPost(post);

                            logger.Info("Post added - {name}", post.Title);
                        }
                    }
                    else if (choice == "4")
                    {
                        // Display Posts
                        Console.WriteLine("Choose the blog to view posts:");
                        var db   = new BloggingContext();
                        var blog = GetBlog(db);
                        if (blog != null)
                        {
                            var query = db.Posts.OrderBy(p => p.Title);

                            Console.WriteLine($"{query.Count()} Posts returned");
                            foreach (var item in query)
                            {
                                Console.WriteLine(item.Title);
                            }
                        }
                    }
                    else if (choice == "5")
                    {
                        // delete blog
                        Console.WriteLine("Choose the blog to delete:");
                        var db   = new BloggingContext();
                        var blog = GetBlog(db);
                        if (blog != null)
                        {
                            // delete blog
                            db.DeleteBlog(blog);
                            logger.Info($"Blog (id: {blog.BlogId}) deleted");
                        }
                    }
                    else if (choice == "6")
                    {
                        // edit blog
                        Console.WriteLine("Choose the blog to edit:");
                        var db   = new BloggingContext();
                        var blog = GetBlog(db);
                        if (blog != null)
                        {
                            // input blog
                            Blog UpdatedBlog = InputBlog(db);
                            if (UpdatedBlog != null)
                            {
                                UpdatedBlog.BlogId = blog.BlogId;
                                db.EditBlog(UpdatedBlog);
                                logger.Info($"Blog (id: {blog.BlogId}) updated");
                            }
                        }
                    }
                    Console.WriteLine();
                } while (choice.ToLower() != "q");
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }

            logger.Info("Program ended");
        }