static void Main(string[] args) { logger.Info("Program started"); try { string choice = ""; do { Console.WriteLine(" -MENU- \n1. Display All Blogs\n2. Add New Blog\n3. Create Post\n4. Display Posts\nEnter 'done' to quit"); choice = Console.ReadLine(); logger.Info("User choice: " + choice); if (choice == "1") { var db = new BloggingContext(); // Display all Blogs from the database var query = db.Blogs.OrderBy(b => b.BlogId); Console.WriteLine("All blogs in the database:"); var count = db.Blogs.Count(); Console.WriteLine(count + " blogs found."); foreach (var item in query) { Console.WriteLine(item.BlogId + ") " + item.Name); } } else if (choice == "2") { // Create and save a new Blog Console.Write("Enter a name for a new Blog: "); string input = Console.ReadLine(); if (string.IsNullOrEmpty(input)) { logger.Error("Blog name cannot be null"); } else { var name = input; 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 enter the blog you wish to post to: "); var query = db.Blogs.OrderBy(b => b.BlogId); foreach (var item in query) { Console.WriteLine(item.BlogId + ") " + item.Name); } string selectBlog = Console.ReadLine(); var test = db.Blogs.Select(b => b.BlogId); string test2 = Convert.ToString(test); if (test2.Contains(selectBlog)) { try { int idNumber = Int32.Parse(selectBlog); var titles = db.Blogs.Where(b => b.BlogId.Equals(idNumber)).Select(b => b.BlogId); Console.Write("Enter a post title: "); string postInput = Console.ReadLine(); if (string.IsNullOrEmpty(postInput)) { logger.Error("Post title cannot be null"); } else { var title = postInput; Console.WriteLine("Write your post:"); string postContent = Console.ReadLine(); var post = new Post { Title = title, Content = postContent, BlogId = idNumber }; var dp = new BloggingContext(); db.AddPost(post); logger.Info("Post added - {title}", title); } } catch (Exception exc) { logger.Error(exc.Message); } } else { logger.Error("Blog does not exist."); } } else if (choice == "4") { var db = new BloggingContext(); Console.WriteLine("Please enter the blog you wish to view posts from: "); var query = db.Blogs.OrderBy(b => b.BlogId); Console.WriteLine("0) Posts from all blogs"); foreach (var item in query) { Console.WriteLine(item.BlogId + ") Posts from " + item.Name); } string selectBlog = Console.ReadLine(); var convertBlogId = db.Blogs.Select(b => b.BlogId); string convertedBlogId = Convert.ToString(convertBlogId); if (selectBlog == "0") { var postQuery = db.Posts.OrderBy(p => p.PostId); Console.WriteLine("All posts in the database:"); var postCount = db.Posts.Count(); Console.WriteLine(postCount + " post(s) found."); foreach (var postItem in postQuery) { Console.WriteLine("Blog: " + postItem.Blog.Name + "\nTitle: " + postItem.Title + "\nContent: " + postItem.Content + "\n"); } } else if (convertedBlogId.Contains(selectBlog)) { //doesnt work int idNumber = Int32.Parse(selectBlog); var postQuery = db.Posts.Where(b => b.BlogId.Equals(idNumber)).OrderBy(p => p.PostId); Console.WriteLine("All posts in the select blog:"); var postCount = db.Posts.Where(b => b.BlogId.Equals(idNumber)).Count(); Console.WriteLine(postCount + " post(s) found."); foreach (var postItem in postQuery) { Console.WriteLine("Blog: " + postItem.Blog.Name + "\nTitle: " + postItem.Title + "\nContent: " + postItem.Content + "\n"); } } else { logger.Error("Blog does not exist."); } } }while (choice != "done"); } catch (Exception ex) { logger.Error(ex.Message); } logger.Info("Program ended"); }
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"); }
public static void Main(string[] args) { logger.Info("Program started"); string userChoice = ""; try { do { Console.WriteLine("1) Display list of Blogs"); Console.WriteLine("2) Make a Blog"); Console.WriteLine("3) Make a Post"); Console.WriteLine("Press any key to quit"); userChoice = Console.ReadLine(); logger.Info("User choice: ", userChoice); if (userChoice == "1") {// Display all Blogs from the database var database = new BloggingContext(); var sqlQuery = database.Blogs.OrderBy(b => b.Name); Console.WriteLine("List of Blogs: "); foreach (var item in sqlQuery) { Console.WriteLine(item.Name); } } else if (userChoice == "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 }; var database = new BloggingContext(); database.AddBlog(blog); logger.Info("Blog added - {name}", name); } else if (userChoice == "3") { var database = new BloggingContext(); Console.WriteLine("Which blog do you want to post to? : "); var blogName = Console.ReadLine(); var blog = database.Blogs.FirstOrDefault(b => b.Name.Contains(blogName)); Console.WriteLine("Name the title of your Post: "); var postTitle = Console.ReadLine(); Console.WriteLine("Wanna add some content to your post?"); var postContent = Console.ReadLine(); var post = new Post { Title = postTitle, Content = postContent, BlogId = blog.BlogId }; database.AddPost(post); logger.Info("Post added - {postTitle}", postTitle); logger.Info("Content added - {postContent}", postContent); } } while (userChoice == "1" || userChoice == "2" || userChoice == "3"); logger.Info("Program has ended"); } catch (Exception ex) { logger.Error(ex.Message); } logger.Info("Program ended"); }