Exemple #1
0
        public void selectOption()
        {
beginning:
            string select = "";

            select = Console.ReadLine();
            switch (select)
            {
            case "1":     // DISPLAY BLOGS
                var orderQueryName = db.Blogs.OrderBy(b => b.Name);
                Console.WriteLine("All blogs in the database:");
                foreach (var item in orderQueryName)
                {
                    Console.WriteLine(item.Name);
                }
                startUp();
                goto beginning;

            case "2":     // ADD BLOG
tryAgain:
                Console.Write("Enter a name for a new Blog: ");
                var name = Console.ReadLine();
                if (String.IsNullOrEmpty(name))
                {
                    goto tryAgain;
                }
                var blog = new Blog {
                    Name = name
                };
                db.AddBlog(blog);
                logger.Info("Blog added - {name}", name);
                startUp();
                goto beginning;

            case "3":     // DISPLAY POSTS

                int blogIdL = selectBlog();

                using (var context = new BloggingContext())
                {
                    var blogs = context.Blogs
                                .FromSqlInterpolated($"SELECT * FROM dbo.Blogs Where BlogId = ({blogIdL})").ToList();

                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    foreach (Blog blogName in blogs)
                    {
                        System.Console.WriteLine($"Blog: " + blogName.Name);
                    }
                    Console.ForegroundColor = ConsoleColor.White;
                }

                using (var context = new BloggingContext())
                {
                    var posts = context.Posts
                                .FromSqlInterpolated($"SELECT * FROM dbo.Posts Where BlogId = ({blogIdL})").ToList();
                    foreach (Post post in posts)
                    {
                        System.Console.WriteLine($"Title: " + post.Title + "\tContent: " + post.Content);
                    }
                }



                startUp();
                goto beginning;

            case "4":      // ADD POST
                int workingBlogId = selectBlog();
oppsNoTitle:
                Console.Write("Enter a title for the new Post: ");
                var title = Console.ReadLine();
                if (String.IsNullOrEmpty(title))
                {
                    goto oppsNoTitle;
                }

                Console.Write("Enter content for the new Post: ");
                var content = Console.ReadLine();
                using (var db = new BloggingContext())
                {
                    var post = new Post()
                    {
                        BlogId  = workingBlogId,
                        Content = content,
                        Title   = title
                    };
                    db.Posts.Add(post);
                    db.SaveChanges();
                }
                logger.Info("Post added!");
                startUp();
                goto beginning;

            case "q":
                Console.WriteLine("Quit");
                db.QuitProgram();
                break;

            default:
                Console.WriteLine("Wrong input try again");
                startUp();
                goto beginning;
            }
        }