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"); }
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"); }
public static void Main(string[] args) { logger.Info("Program started"); try { // Create and save a new Blog var db = new BloggingContext(); string name; do { Console.Write("Blog Manager\n1. Display all blogs\n2. Add Blog\n3. Create Post\nPress any other key to exit\n"); name = Console.ReadLine(); switch (name) { case "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); } break; case "2": Console.WriteLine("Enter a name for your blog."); string blogName = Console.ReadLine(); var blog = new Blog(); db.AddBlog(blog); logger.Info("Blog added - {name}", name); db.doSave(); break; case "3": Console.WriteLine("Please select the blog to post to:"); var search = db.Blogs.OrderBy(b => b.Name); var idSearch = db.Blogs.OrderBy(b => b.BlogId); foreach (var item in search) { Console.WriteLine(item.Name); } var select = Console.ReadLine(); foreach (var item in search) { if (select == item.Name) { Console.WriteLine("Enter the Title of the Post"); var title = Console.ReadLine(); Console.WriteLine("Enter the content of the Post"); var content = Console.ReadLine(); var post = new Post(title, content, item.BlogId); db.AddPost(post); } } db.doSave(); break; default: break; } }while (name == "1" || name == "2" || name == "3"); } catch (Exception ex) { logger.Error(ex.Message); } logger.Info("Program ended"); }
public static void Main(string[] args) { logger.Info("Program started"); try { int choice = 0; do { Console.WriteLine("1) Display all Blogs"); Console.WriteLine("2) Add Blog"); Console.WriteLine("3) Create post"); 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 }; var db = new BloggingContext(); db.AddBlog(blog); logger.Info("Blog added - {name}", name); } else if (choice == 1) { var db = new BloggingContext(); // 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); } } else if (choice == 3) { var db = new BloggingContext(); Console.WriteLine("Create post\n"); var query = db.Blogs.OrderBy(b => b.Name); Console.WriteLine("All blogs in the database:"); foreach (var item in query) { Console.WriteLine($"{item.BlogId} + {item.Name}"); } if (int.TryParse(Console.ReadLine(), out int BlogId)) { if (db.Blogs.Any(b => b.BlogId == BlogId)) { Post post = new Post(); post.BlogId = BlogId; Console.WriteLine("Enter the Title"); post.Title = Console.ReadLine(); if (post.Title.Length == 0) { logger.Error("Titles cant be blank"); } else { Console.WriteLine("Enter post info"); post.Content = Console.ReadLine(); db.AddPost(post); logger.Info("{title} added.", post.Title); } } else { logger.Error("There are no Blogs with that id"); } } else { logger.Error("No Blog with that ID"); } } else if (choice == 4) { // Display Posts var db = new BloggingContext(); var query = db.Blogs.OrderBy(b => b.BlogId); Console.WriteLine("Select the blog's posts to display:"); Console.WriteLine("0) Posts from all blogs"); foreach (var item in query) { Console.WriteLine($"{item.BlogId}) Posts from {item.Name}"); } if (int.TryParse(Console.ReadLine(), out int BlogId)) { IEnumerable <Post> Posts; if (BlogId != 0 && db.Blogs.Count(b => b.BlogId == BlogId) == 0) { logger.Error("There are no Blogs saved with that Id"); } else { // display posts from all blogs Posts = db.Posts.OrderBy(p => p.Title); if (BlogId == 0) { // display all posts from all blogs Posts = db.Posts.OrderBy(p => p.Title); } else { // display post from selected blog Posts = db.Posts.Where(p => p.BlogId == BlogId).OrderBy(p => p.Title); } Console.WriteLine($"{Posts.Count()} post(s) returned"); foreach (var item in Posts) { Console.WriteLine($"Blog: {item.Blog.Name}\nTitle: {item.Title}\nContent: {item.Content}\n"); } } } else { logger.Error("Invalid Blog Id"); } } Console.WriteLine(); } while (choice == 5); } catch (Exception ex) { logger.Error(ex.Message); } logger.Info("Program ended"); }