コード例 #1
0
            public static void Main(string[] args)
            {
                var inProgram = true;

                logger.Info("Program started");
                do
                {
                    // Create and save a new Blog
                    Console.WriteLine("\nEnter Your Selection:");
                    Console.WriteLine("1) Display Blogs");
                    Console.WriteLine("2) Add Blog");
                    Console.WriteLine("3) Create Post");
                    Console.WriteLine("4) Display Posts");
                    Console.WriteLine("5) To Exit Program");

                    var choice = int.Parse(Console.ReadLine());
                    logger.Info("Selection " + choice + " was selected.");

                    switch (choice)
                    {
                    case 1:
                    {
                        var db = new BloggingContext();
                        // Display all Blogs from the database
                        var query = db.Blogs.OrderBy(b => b.Name);
                        Console.WriteLine(query.Count() + " blogs returned");

                        Console.WriteLine("All blogs in the database:");
                        foreach (var item in query)
                        {
                            Console.WriteLine(item.Name);
                        }
                        db.SaveChanges();

                        break;
                    }

                    case 2:
                    {
                        //add a Blog
                        var nameIsValid = false;
                        do
                        {
                            Console.Write("Enter a name for your new blog");
                            var name = Console.ReadLine();

                            //check to see if name is null
                            if (name is null)
                            {
                                logger.Info("Name cannot be null");
                            }
                            else
                            {
                                nameIsValid = true;
                                var blog = new Blog {
                                    Name = name
                                };

                                var db = new BloggingContext();
                                db.AddBlog(blog);
                                logger.Info("Blog added - {name}", name);
                                db.SaveChanges();
                            }
                        } while (!nameIsValid);

                        break;
                    }

                    case 3:
                    {
                        Console.WriteLine("Select the ID number of the blog you'd like to post to\n");

                        var db = new BloggingContext();
                        // Display all Blogs from the database
                        var query     = db.Blogs;
                        var post      = new Post();
                        var validBlog = false;

                        do
                        {
                            Console.WriteLine("All blogs in the database:\n");

                            foreach (var item in query)
                            {
                                Console.WriteLine(item.BlogId + ") " + item.Name);
                            }

                            var blogChoice = 0;

                            try
                            {
                                blogChoice = int.Parse(Console.ReadLine());
                            }
                            catch
                            {
                                logger.Info(blogChoice + "is not a valid integer.");
                            }

                            var blogs = db.Blogs.Where(p => p.BlogId == blogChoice);

                            if (blogs.Count() == 0)
                            {
                                logger.Info("\n{choice} is not a valid Blog Id", blogChoice);
                            }
                            else
                            {
                                var title        = "";
                                var titleIsValid = false;
                                do
                                {
                                    Console.Write("\nEnter a title for your post");
                                    title = Console.ReadLine();

                                    if (title is null)
                                    {
                                        logger.Info("Name cannot be null");
                                    }
                                    else
                                    {
                                        titleIsValid = true;
                                    }
                                } while (!titleIsValid);


                                Console.WriteLine("\nEnter the post content:");
                                var content = Console.ReadLine();

                                post = new Post {
                                    Title = title, Content = content, BlogId = blogChoice
                                };
                                db.SaveChanges();
                            }
                        } while (!validBlog);

                        db.AddPost(post);
                        logger.Info("\nPost added to - {name}", post.Blog.Name);
                        break;
                    }

                    case 4:
                    {
                        var db = new BloggingContext();
                        // Display all Blogs from the database
                        var query         = db.Blogs;
                        var choiceIsValid = false;

                        do
                        {
                            Console.WriteLine("Select the blog's posts to display:");
                            Console.WriteLine("0) Display all posts");
                            foreach (var item in query)
                            {
                                Console.WriteLine(item.BlogId + ") Display all posts from " + item.Name);
                            }

                            var postChoice = 0;

                            try
                            {
                                postChoice = int.Parse(Console.ReadLine());
                            }
                            catch
                            {
                                logger.Info(postChoice + "is not a valid integer.");
                            }

                            if (postChoice == 0)
                            {
                                var posts = db.Posts;
                                Console.WriteLine($"{posts.Count()} post(s) returned");
                                foreach (var p in posts)
                                {
                                    Console.WriteLine("Blog: " + p.Blog.Name);
                                    Console.WriteLine("Title: " + p.Title);
                                    Console.WriteLine("Content: " + p.Content);
                                }
                                choiceIsValid = true;
                            }
                            else
                            {
                                if (db.Blogs.Where(p => p.BlogId == postChoice).Count() == 0)
                                {
                                    logger.Info("\n{choice} is not a valid Blog Id", postChoice);
                                }
                                else
                                {
                                    var posts = db.Posts.Where(p => p.BlogId == postChoice);
                                    Console.WriteLine($"{posts.Count()} post(s) returned");
                                    foreach (var p in posts)
                                    {
                                        Console.WriteLine("Title: " + p.Title);
                                        Console.WriteLine("Content: " + p.Content);
                                    }
                                    choiceIsValid = true;
                                }
                            }
                        } while (!choiceIsValid);

                        db.SaveChanges();

                        break;
                    }

                    case 5:
                    {
                        Console.WriteLine("Are you sure you'd like to exit the program? (Y/N)");

                        var exitChoice = Console.ReadLine().ToUpper();
                        if (exitChoice == "Y")
                        {
                            inProgram = false;
                        }
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                } while (inProgram);
            }
コード例 #2
0
        public static void Main(string[] args)
        {
            logger.Info("Program started");
            try
            {
                string choice = "";
                var    db     = new BloggingContext();
                do
                {
                    //Present user with options
                    Console.WriteLine("1) Display all blogs?");
                    Console.WriteLine("2) Add new blog?");
                    Console.WriteLine("3) Create blog post?");
                    Console.WriteLine("Enter any other key to exit.");

                    choice = Console.ReadLine();


                    if (choice == "1")
                    {
                        // 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);
                        }
                    }

                    if (choice == "2")
                    {
                        // Create and save a new Blog
                        Console.Write("Enter a name for a new Blog: ");
                        var name = Console.ReadLine();

                        var blog = new Blog {
                            Name = name
                        };

                        db.AddBlog(blog);
                        logger.Info("Blog added - {name}", name);
                    }

                    if (choice == "3")
                    {
                        //select the blog in which to post
                        // Display all Blogs from the database
                        var query          = db.Blogs.OrderBy(b => b.Name);
                        int selectedBlogId = 0;

                        Console.WriteLine("All blogs in the database:");
                        Console.WriteLine("Blog ID\t\tBlog Name");
                        foreach (var item in query)
                        {
                            Console.WriteLine("{0}\t\t{1}", item.BlogId, item.Name);
                        }
                        try
                        {
                            Console.Write("Enter the Blog ID of the blog to which you wish to post: ");
                            selectedBlogId = int.Parse(Console.ReadLine());
                        }
                        catch (Exception ex)
                        { logger.Error(ex.Message); }

                        //Console.Write("Enter the Blog ID of the blog to which you wish to post: ");
                        //selectedBlogId = IntParse().Console.ReadLine();
                        //Add new blog post
                        Console.Write("Enter blog post title: ");
                        var title = Console.ReadLine();

                        Console.Write("Enter your blog content: ");
                        var content = Console.ReadLine();

                        var post = new Post {
                            Title = title, Content = content, BlogId = selectedBlogId
                        };

                        db.AddPost(post);
                        logger.Info("Post added - {title}", title);
                    }
                } while (choice == "1" || choice == "2" || choice == "3");
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }
            logger.Info("Program ended");
        }
コード例 #3
0
        public static void Main(string[] args)
        {
            logger.Info("Program started");
            string choice = "";

            try
            {
                do
                {
                    Console.WriteLine("1)Display all Blogs");
                    Console.WriteLine("2)Add a Blog");
                    Console.WriteLine("3)Create a Post");
                    Console.WriteLine("Enter to quit");
                    choice = Console.ReadLine();
                    logger.Info("User choice: {choice}", choice);
                    if (choice == "1")
                    {
                        var db    = new BloggingContext();
                        var query = db.Blogs.OrderBy(b => b.Name);

                        Console.WriteLine("All blogs in the database:");
                        foreach (var item in query)
                        {
                            Console.WriteLine(item.Name);
                        }
                    }
                    else if (choice == "2")
                    {
                        Console.Write("Enter a name for a new Blog: ");
                        var name = Console.ReadLine();

                        var blog = new Blog {
                            Name = name
                        };

                        var db = new BloggingContext();
                        db.AddBlog(blog);
                        logger.Info("Blog added - {name}", name);
                    }
                    else if (choice == "3")
                    {
                        var db = new BloggingContext();
                        Console.WriteLine("Please choose a Blog you would like to post to: ");
                        var name = Console.ReadLine();
                        var blog = db.Blogs.FirstOrDefault(b => b.Name.Contains(name));
                        Console.WriteLine("What is the title of the Post: ");
                        var title = Console.ReadLine();
                        Console.WriteLine("What is the content of the Post: ");
                        var content = Console.ReadLine();
                        var post    = new Post {
                            Title = title, Content = content, BlogId = blog.BlogId
                        };
                        db.AddPost(post);
                        logger.Info("Post added - {title}", title);
                        logger.Info("Post added - {content}", content);
                    }
                } while (choice == "1" || choice == "2" || choice == "3");
                logger.Info("Program ended");
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: YungSushiRoll/Blog-Console
        public static void Main(string[] args)
        {
            logger.Info("Program started");
            try
            {
                String option;

                do
                {
                    Console.WriteLine("1) Display all Blogs");
                    Console.WriteLine("2) Add Blog");
                    Console.WriteLine("3) New Post");
                    Console.WriteLine("4) Show Posts");
                    Console.WriteLine("Press Enter to exit");
                    option = Console.ReadLine();
                    if (option == "1")
                    {
                        var db = new BloggingContext();
                        // Display all Blogs from the database
                        List <Blog> blogs = db.Blogs.OrderBy(b => b.Name).ToList();

                        Console.WriteLine("All blogs in the database:");
                        foreach (var item in blogs)
                        {
                            Console.WriteLine(item.Name);
                        }
                    }
                    else if (option == "2")
                    {
                        var db = new BloggingContext();
                        // Create and save a new Blog
                        Console.Write("Enter a name for a new Blog: ");
                        var name = Console.ReadLine();

                        var blog = new Blog {
                            Name = name
                        };

                        db.AddBlog(blog);
                        logger.Info("Blog added - {name}", name);
                    }
                    else if (option == "3")
                    {
                        var    db = new BloggingContext();
                        String resp;
                        // Display all Blogs from the database
                        List <Blog> blogs = db.Blogs.OrderBy(b => b.Name).ToList();

                        Console.WriteLine("What blog would you like to post to?");
                        foreach (var item in blogs)
                        {
                            Console.WriteLine(item.BlogId + ": " + item.Name);
                        }
                        resp = Console.ReadLine();
                        bool isValid = false;
                        foreach (var id in blogs)
                        {
                            if (int.Parse(resp) == id.BlogId)
                            {
                                isValid = true;
                            }
                        }
                        if (isValid)
                        {
                            Post post = new Post();
                            post.BlogId = int.Parse(resp);
                            MakePost(post);
                            db.AddPost(post);
                            logger.Info("Post added - {title}", post.Title);
                        }
                        else
                        {
                            Console.WriteLine("Invalid Blog Id");
                        }
                        db.SaveChanges();
                    }
                } while (option.ToLower() != "");
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }
            logger.Info("Program ended");
        }