static void Main(string[] args) { logger.Info("Program started"); try { // 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); // 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); } } catch (Exception ex) { logger.Error(ex.Message); } logger.Info("Program ended"); }
public static void Main(string[] args) { logger.Info("Program started"); try { var db = new BloggingContext(); string choice = ""; do { Console.WriteLine("1) Create Blog"); Console.WriteLine("2) Create Post"); Console.WriteLine("Anything Else To Exit"); choice = Console.ReadLine(); logger.Info("User Choice:" + choice); if (choice.Equals("1")) { // 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.Equals("2")) { // Display all Blogs from the database var query = db.Blogs.OrderBy(b => b.BlogId); Console.WriteLine("Choose blog to write to:"); foreach (var item in query) { Console.WriteLine(item.BlogId + " - " + item.Name); } int blogid = int.Parse(Console.ReadLine()); Blog myBlog = query.ToList()[blogid]; Console.WriteLine("Title For Post:"); string title = Console.ReadLine(); Console.WriteLine("Content of Post:"); string content = Console.ReadLine(); Post post = new Post { Title = title, Content = content, BlogId = blogid, Blog = myBlog }; db.AddPost(post); } } while (choice == "1" || choice == "2"); } catch (Exception ex) { logger.Error(ex.Message); } logger.Info("Program ended"); }
private void CreateNewBlog() { var name = _userInterface.GetBlogName(); var blog = new Blog { Name = name }; using (var db = new BloggingContext()) { db.AddBlog(blog); } }
private static void createBlog() { // 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); }
public static void Main(string[] args) { logger.Info("Program started"); try { // 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); // 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); } Console.Write("Enter a blog you want to post to: "); var blogName = Console.ReadLine(); Blog mainBlog = db.Blogs.Where(ID => ID.Name == blogName).FirstOrDefault(); Console.Write("Enter title for your post: "); var postTitle = Console.ReadLine(); Console.Write("\nEnter content for the post: "); var postEntry /*content*/ = Console.ReadLine(); Post mainPost = new Post { Title = postTitle, Content = postEntry, BlogId = mainBlog.BlogId }; db.AddPost(mainPost); } catch (Exception ex) { logger.Error(ex.Message); } Console.WriteLine("Press enter to quit"); string x = Console.ReadLine(); logger.Info("Program ended"); }
//Adds a New Blog public static void addBlog() { try { bool validBlogName = false; String name = ""; do { Console.Write("\nEnter a name for a new Blog: "); name = Console.ReadLine(); var currentBlogNames = db.Blogs.Select(n => n.Name); if (!currentBlogNames.Contains(name) && !name.Equals("")) { validBlogName = true; } else if (name.Equals("")) { Console.WriteLine("Blog Name Cannot Be Blank\n"); } else { Console.WriteLine("Blog Name Already Taken\n"); } } while (!validBlogName); var blog = new Blog { Name = name }; db.AddBlog(blog); logger.Info("Blog added - {name}", name); } catch (Exception e) { logger.Error("Error Adding Blog: {e.Message} {e.StackTrace}", e.Message, e.StackTrace); } }
public static void Main(string[] args) { logger.Info("Program started"); try { string choice; do { Console.WriteLine("1) Display all blogs"); Console.WriteLine("2) Add blog"); Console.WriteLine("3) Create posts"); Console.WriteLine("\"q\" to quit"); choice = Console.ReadLine(); Console.Clear(); logger.Info($"Option {choice} selected"); 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 == "2") { // ADD 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 == "3") { // Create Post var db = new BloggingContext(); var query = db.Blogs.OrderBy(b => b.BlogId); Console.WriteLine("Select the blog you would to post to:"); 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 = InputPost(db); if (post != null) { post.BlogId = BlogId; db.AddPost(post); logger.Info("Post added - {title}", post.Title); } } else { logger.Error("There are no Blogs saved with that Id"); } } else { logger.Error("Invalid Blog Id"); } } }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"); 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("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 Console.Write("Enter a name for a new Blog: "); var blog = new Blog { Name = Console.ReadLine() }; ValidationContext context = new ValidationContext(blog, null, null); List <ValidationResult> results = new List <ValidationResult>(); var isValid = Validator.TryValidateObject(blog, context, results, true); if (isValid) { var db = new BloggingContext(); // check for unique name if (db.Blogs.Any(b => b.Name == blog.Name)) { // generate validation error isValid = false; results.Add(new ValidationResult("Blog name exists", new string[] { "Name" })); } else { logger.Info("Validation passed"); // save blog to db db.AddBlog(blog); logger.Info("Blog added - {name}", blog.Name); } } if (!isValid) { foreach (var result in results) { logger.Error($"{result.MemberNames.First()} : {result.ErrorMessage}"); } } } else if (choice == "3") { // Create Post var db = new BloggingContext(); var query = db.Blogs.OrderBy(b => b.BlogId); Console.WriteLine("Select the blog you would to post to:"); 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 = InputPost(db); if (post != null) { post.BlogId = BlogId; db.AddPost(post); logger.Info("Post added - {title}", post.Title); } } else { logger.Error("There are no Blogs saved with that Id"); } } else { logger.Error("Invalid Blog Id"); } } Console.WriteLine(); } while (choice.ToLower() != "q"); } catch (Exception ex) { logger.Error(ex.Message); } logger.Info("Program ended"); }
static void Main(string[] args) { logger.Info("Program started"); string userchoice = ""; var db = new BloggingContext(); try { do { Console.WriteLine("Please type 1 to create a blog, 2 to display all blogs, 3 to post to a blog, or 4 to display posts"); userchoice = Console.ReadLine(); if (userchoice == "1") { // 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 }; // ill need to assign a blogid to the create blog bit of code and then connect it with the add post method db.AddBlog(blog); logger.Info("Blog added - {name}", name); } if (userchoice == "2") { // 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 (userchoice == "3") { Console.WriteLine("Please enter the id of the blog you wish to post to"); int bloggerId = int.Parse(Console.ReadLine()); Console.WriteLine("What is the title of your post?"); var title = Console.ReadLine(); Console.WriteLine("Please enter all content of the post"); var content = Console.ReadLine(); var post = new Post { Title = title, Content = content, BlogId = bloggerId }; db.AddPost(post); db.SaveChanges(); // Blog blog23 = db.Blogs.FirstOrDefault(c => c.BlogId == bloggerId); // blog23.Posts.Add(post); } if (userchoice == "4") { Console.WriteLine("Type '1' to display all posts from all blogs, or enter 2 to show posts from a specific blog"); int choice = int.Parse(Console.ReadLine()); if (choice == 1) { var query = db.Blogs.OrderBy(b => b.BlogId); foreach (var item in query) { Console.WriteLine(item.Name); foreach (Post p in item.Posts) { Console.WriteLine(p.Title + "\n" + p.Content); } } } else if (choice == 2) { var dbb = new BloggingContext(); var query = db.Blogs.OrderBy(b => b.BlogId); Console.WriteLine("select the blog whose post you would like to see"); foreach (var item in query) { Console.WriteLine(item.BlogId + " " + item.Name); } int id = int.Parse(Console.ReadLine()); Console.Clear(); Blog blog2 = db.Blogs.FirstOrDefault(c => c.BlogId == id); Console.WriteLine(blog2.BlogId + " " + blog2.Name); foreach (Post p in blog2.Posts) { Console.WriteLine(p.Title + "\n" + p.Content); } } } }while (userchoice == "1" || userchoice == "2" || userchoice == "3" || userchoice == "4"); } catch (Exception ex) { logger.Error(ex.Message); } logger.Info("Program ended"); }
public static void Main(string[] args) { logger.Info("Program started"); try { String choice; do { Console.WriteLine("1.) Create blog"); Console.WriteLine("2. Add Post to blog"); Console.WriteLine("3.) Display all blogs"); choice = Console.ReadLine(); if (choice == "1") { // 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(); if ((db.Blogs.Any(b => b.Name == blog.Name))) { logger.Error("Blog name already exists"); } else { db.AddBlog(blog); logger.Info("Blog added - {name}", name); } } else if (choice == "2") { var db = new BloggingContext(); var query = db.Blogs.OrderBy(b => b.BlogId); Post post = new Post(); Console.WriteLine("Select the blog you would like to post to:"); 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.BlogId = BlogId; Console.WriteLine("Enter post title: "); post.Title = Console.ReadLine(); Console.WriteLine("Enter post content: "); post.Content = Console.ReadLine(); db.AddPost(post); logger.Info("Post added - {Title}", post.Title); } else { logger.Error("There are no blogs saved with that id"); } } else { logger.Error("Invalid blog id"); } } else if (choice == "3") { 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); } } }while (choice == "1" || choice == "2" || choice == "3"); } catch (Exception ex) { logger.Error(ex.Message); } logger.Info("Program ended"); }
public static void Main(string[] args) { logger.Info("Program started"); try { Console.WriteLine("1) Display all blogs"); Console.WriteLine("2) Add Blog"); Console.WriteLine("3) Create Post"); Console.WriteLine("4) EXIT"); Console.Write("Please enter your choice (1, 2, 3, 4): "); var MenuChoice = Console.ReadLine(); if (MenuChoice == "1") { // Display all Blogs from the database var query = db.Blogs.OrderBy(b => b.Name); Console.WriteLine("Here's a list of All blogs in the database:"); foreach (var item in query) { Console.WriteLine(item.Name); } } else if (MenuChoice == "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 (MenuChoice == "3") { // Create a blog post Console.WriteLine("Enter the blog you want to create a post for: "); var blogName = Console.ReadLine(); var blog = db.Blogs.FirstOrDefault(b => b.Name == blogName); Console.Write("Enter post title: "); var postTitle = Console.ReadLine(); Console.Write("Enter post content: "); var postContent = Console.ReadLine(); Post newPost = new Post { Title = postTitle, Content = postContent, Blog = blog }; db.AddPost(newPost); } else if (MenuChoice == "4") { logger.Info("Program Ended"); Environment.Exit(0); //Exit the application } else { Console.WriteLine("Please enter a valid menu option"); } } catch (Exception ex) { logger.Error(ex.Message); } logger.Info("Program ended"); }
public static void Main(string[] args) { logger.Info("Program started"); string choice = ""; do { try { // Give user a choice Console.WriteLine("What would you like to do? "); Console.Write("(1) Display all blogs (2) Add a blog or (3) Create a post: "); choice = Console.ReadLine(); if (choice == "1") { // Display all Blogs and Posts from the database var db = new BloggingContext(); var query = db.Blogs.OrderBy(b => b.BlogId); Console.WriteLine("All blogs in the database:"); foreach (var item in query) { Console.WriteLine(item.BlogId + "\t" + item.Name); // Display all Blogs from the database var showPostDb = new BloggingContext(); var blogger = showPostDb.Posts.Where(b => b.BlogId.Equals(item.BlogId)); foreach (var posts in blogger) { Console.WriteLine("\t\t" + posts.Title); Console.WriteLine("\t\t\t" + posts.Content); } } } else 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 == "3") { // Display all Blogs from the database var showDb = new BloggingContext(); var query = showDb.Blogs.OrderBy(b => b.BlogId); Console.WriteLine("All blogs in the database:"); foreach (var item in query) { Console.WriteLine(item.BlogId + "\t " + item.Name); } // Select the Blog Console.Write("Select the id of the blog you want to post to? "); int blogId = int.Parse(Console.ReadLine()); var blogCntxtDb = new BloggingContext(); var name = blogCntxtDb.Blogs.Where(b => b.BlogId.Equals(blogId)); foreach (var thing in name) { Console.WriteLine("Title your post for " + thing.Name + "\t" + thing.BlogId); var postTitle = Console.ReadLine(); Console.WriteLine("Enter your post to " + thing.Name); var postContent = Console.ReadLine(); // var blogDb = new Blog { Name = thing.Name }; var postDb = new Post { Title = postTitle, Content = postContent, BlogId = thing.BlogId }; var db = new BloggingContext(); db.AddPost(postDb); } logger.Info("Post added - {name}", name); } } catch (Exception ex) { logger.Error(ex.Message); } } while (choice != ""); // Give error message Console.Write("You've made an invalid selection. "); Console.WriteLine("Press enter to quit"); string x = Console.ReadLine(); logger.Info("Program ended"); }
public static void Main(string[] args) { logger.Info("Program started"); var programIsRunning = true; while (programIsRunning == true) { Console.WriteLine("1) Display All Blogs"); Console.WriteLine("2) Add Blogs"); Console.WriteLine("3) Create Post"); Console.WriteLine("4) Exit"); int response = Convert.ToInt32(Console.ReadLine()); if (response == 1) { try { 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); } } catch (Exception ex) { logger.Error(ex.Message); } } else if (response == 2) { try { // 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); // 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); } } catch (Exception ex) { logger.Error(ex.Message); } } else if (response == 3) { try { var db = new BloggingContext(); var query = db.Blogs.OrderBy(b => b.BlogId); List <int> blogIds = new List <int> { }; Console.WriteLine("All blogs in the database:"); foreach (var item in query) { Console.WriteLine(item.BlogId.ToString() + ") " + item.Name); blogIds.Add(item.BlogId); } var waitingForValidAnswer = true; while (waitingForValidAnswer == true) { Console.WriteLine("\nWhich Blog Would You Like To Add To?"); int answer = Convert.ToInt32(Console.ReadLine()); int chosenBlogId = 0; for (int i = 0; i < blogIds.Count; i++) { if (answer == blogIds.ElementAt(i)) { waitingForValidAnswer = false; chosenBlogId = blogIds.IndexOf(blogIds.ElementAt(i)); } } if (waitingForValidAnswer == true) { Console.WriteLine("Please Enter A Valid ID"); } else { try { // Create and save a new Blog Console.Write("Enter a title for a new Post:"); var title = Console.ReadLine(); Console.WriteLine("Enter The Post's Content:"); var content = Console.ReadLine(); var newPostId = 1; try { newPostId = db.Posts.Select(p => p.PostId).Last() + 1; } catch// (Exception ex) { Console.WriteLine("No Posts In Database"); } var post = new Post { Title = title, Content = content, BlogId = chosenBlogId + 1, PostId = newPostId }; db.AddPost(post); logger.Info("Post added - {title}", post); // Display all Blogs from the database var query2 = db.Posts.OrderBy(b => b.Title); Console.WriteLine("All posts in the database:"); foreach (var item in query2) { Console.WriteLine(item.Title); } } catch (Exception ex) { logger.Error(ex.Message); } } } } catch (Exception ex) { logger.Error(ex.Message); } } else if (response == 4) { programIsRunning = false; } } logger.Info("Program ended"); }
public static void Main(string[] args) { logger.Info("Program started"); try { string userChoice; do { //ask user for input 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 Post"); Console.WriteLine("6) Edit Post"); Console.WriteLine("Enter x to exit"); userChoice = Console.ReadLine(); //clear user input Console.Clear(); //log user input logger.Info("Option {userChoice} selected", userChoice); BloggingContext choice = new BloggingContext(); if (userChoice == "1") { //display all blogs choice.DisplayBlog(); } else if (userChoice == "2") { //add blog choice.AddBlog(); } else if (userChoice == "3") { //create post choice.CreatePost(); } else if (userChoice == "4") { //display post choice.DisplayPost(); } else if (userChoice == "5") { //delete post choice.DeletePost(); } else if (userChoice == "6") { //edit post choice.EditPost(); } } while (userChoice.ToLower() != "x"); } catch (Exception ex) { logger.Error(ex.Message); } logger.Info("Program ended"); }
static void Main(string[] args) { logger.Info("Program started"); var db = new BloggingContext(); var userInput = "0"; 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("Enter q to quit"); try { var userInputCheck = Int32.Parse(userInput = Console.ReadLine()); if (userInputCheck > 4 || userInputCheck < 1) { logger.Error("Invalid option"); } else { logger.Info("Option {userInput} selected", userInput); } }catch (FormatException) { logger.Error("Invalid option"); } // Display Blogs if (userInput == "1") { var blogs = db.Blogs.OrderBy(b => b.Name); Console.WriteLine(db.Blogs.Count() + " Blogs returned"); foreach (var item in blogs) { Console.WriteLine(item.Name); } } // Create Blog else if (userInput == "2") { Console.Write("Enter a name for a new Blog: "); var name = Console.ReadLine(); if (name != "") { var blog = new Blog { Name = name }; db.AddBlog(blog); logger.Info("Blog added - {name}", name); } else { logger.Error("Blog name cannot be null"); } } //Create Post else if (userInput == "3") { if (db.Blogs.Count() != 0) { int blogId = 0; Console.WriteLine("Select the blog you would like to post to:"); var blogs = db.Blogs.OrderBy(b => b.BlogId); foreach (var item in blogs) { Console.WriteLine(item.BlogId + ")" + " " + item.Name); } try { blogId = Int32.Parse(Console.ReadLine()); if (blogId > db.Blogs.Count()) { logger.Error("There are no Blogs saved with that Id"); } else { Console.WriteLine("Enter the Post title"); string title = Console.ReadLine(); if (title != "") { Console.WriteLine("Enter the Post content"); string content = Console.ReadLine(); var post = new Post { Title = title, Content = content, BlogId = blogId }; db.AddPost(post); logger.Info("Post added - {title}", title); } else { logger.Error("Post title cannot be null"); } } }catch (FormatException) { logger.Error("Invalid Blog Id"); } } else { Console.WriteLine(db.Blogs.Count() + " Blogs returned"); } } //Display Posts else if (userInput == "4") { if (db.Blogs.Count() != 0) { int blogId = 0; Console.WriteLine("Select the blog's posts to display:"); var blogs = db.Blogs.OrderBy(b => b.BlogId); Console.WriteLine("0) Posts from all blogs"); foreach (var item in blogs) { Console.WriteLine(item.BlogId + ")" + " Posts from " + item.Name); } try { blogId = Int32.Parse(Console.ReadLine()); if (blogId > db.Blogs.Count()) { logger.Error("There are no Blogs saved with that Id"); } else { if (blogId == 0) { Console.WriteLine(db.Posts.Count() + " post(s) returned"); foreach (var item in db.Posts) { Console.WriteLine("Blog: " + item.Blog.Name + "\nTitle: " + item.Title + "\nContent: " + item.Content); } } else { var posts = db.Posts.Where(p => p.BlogId == blogId); Console.WriteLine(posts.Count() + " post(s) returned"); foreach (var item in posts) { Console.WriteLine("Blog: " + item.Blog.Name + "\nTitle: " + item.Title + "\nContent: " + item.Content); } } } }catch (FormatException) { logger.Error("Invalid Blog Id"); } } else { Console.WriteLine(db.Blogs.Count() + " Blogs returned"); } } Console.WriteLine();//spacer }while(userInput != "q"); logger.Info("Program ended"); }
public static void Main(string[] args) { logger.Info("Program started"); bool cont = true; do { //Menu Console.WriteLine("Press 1 to display all Blogs: "); Console.WriteLine("Press 2 to add a Blog: "); Console.WriteLine("Press 3 to Create a post: "); Console.WriteLine("Press 4 to Exit Program:"); int input = 0; string fix = Console.ReadLine(); if (!int.TryParse(fix, out input)) { logger.Error("failed to parse int"); } try { if (input == 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 (input == 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 (input == 3) { //Create and save new Post 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); } Console.WriteLine("Select the blog you would like to post to: "); var blog = Console.ReadLine(); var query1 = db.Blogs.Where(b => b.Name.ToLower() == blog.ToLower()).First(); Console.WriteLine("Enter the title of your Post: "); var title = Console.ReadLine(); Console.Write("Enter your Post Content: "); var content = Console.ReadLine(); var post = new Post { Title = title, Content = content, BlogId = query1.BlogId }; db.AddPost(post); logger.Info("Post added - {content}", content); } else if (input == 4) { cont = false; } } catch (Exception ex) { logger.Error(ex.Message); } logger.Info("Program ended"); } while (cont); }
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"); }
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.Blog.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: { var nameIsValid = false; do { Console.Write("Enter a name for your new blog"); var name = Console.ReadLine(); 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); } } 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.Blog; 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.Blog.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 }; } } 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.Blog; 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.Post; 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.Blog.Where(p => p.BlogId == postChoice).Count() == 0) { logger.Info("\n{choice} is not a valid Blog Id", postChoice); } else { var posts = db.Post.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); }
public static void Main(string[] args) { logger.Info("Program started"); string menuOption; var db = new BloggingContext(); do { //menu for app Console.WriteLine("Blogs-Consol Application"); Console.WriteLine("========================"); Console.WriteLine("1-Display all blogs"); Console.WriteLine("2-Add blog"); Console.WriteLine("3-Create post"); Console.WriteLine("5-Exit program"); menuOption = Console.ReadLine(); if (menuOption == "1") { try { // Display all Blogs from the database - this is LINQ var query = db.Blogs.OrderBy(b => b.BlogId); Console.WriteLine("All blogs in the database:"); foreach (var item in query) { Console.Write(item.BlogId + "-"); Console.WriteLine(item.Name); } } catch (Exception ex) { logger.Error(ex.Message); } } if (menuOption == "2") { try { // 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); } catch (Exception ex) { logger.Error(ex.Message); } } if (menuOption == "3") { try { // Create a post for a selected blog // display list of current blogs // display all Blogs from the database - this is LINQ var query = db.Blogs.OrderBy(b => b.BlogId); Console.WriteLine("All blogs in the database:"); foreach (var item in query) { Console.Write(item.BlogId + "-"); Console.WriteLine(item.Name); } Console.Write("Enter blog id to write a post:"); var i = Console.ReadLine(); var id = System.Convert.ToInt32(i); logger.Info("Blog ID seklected - {id}", id); var blog = db.Blogs.FirstOrDefault(b => b.BlogId == id); var name = blog.Name; Console.Write("Enter a title for a new post to blog " + name + ": "); var title = Console.ReadLine(); Console.WriteLine("Enter your post content below: "); var content = Console.ReadLine(); var post = new Post { Title = title, Content = content, BlogId = id, Blog = blog }; db.AddPost(post); logger.Info("Post added to BlogID {id} - Title {title}", id, title); } catch (Exception ex) { logger.Error(ex.Message); } } } while (menuOption == "1" || menuOption == "2" || menuOption == "3"); logger.Info("Program ended"); }
public static void Main(string[] args) { logger.Info("Program started"); try { bool cont = true; int i; while (cont) { var db = new BloggingContext(); Console.Write("Welcome to the Blog/Console:\n" + "=1 Display all Blogs\n" + "=2 Create Blog\n" + "=3 Create Post\n" + "=4 Display Posts\n" + "=5 Exit\n" + "==="); string input = Console.ReadLine(); switch (Validate.ValidateMenuSelection(input, 1, 5)) { case "1": // Display all Blogs from the database var query = db.Blogs.OrderBy(b => b.Name); Console.WriteLine("Blogs in database: " + db.Blogs.Count()); i = 1; foreach (var item in query) { Console.WriteLine(i++ + ") " + item); } break; case "2": // Create and save a new Blog Console.Write("Enter a name for the new Blog:\n" + "==="); var name = Validate.ValidateBlank(Console.ReadLine()); db.AddBlog(new Blog { Name = name }); logger.Info("Blog added - {name}", name); break; case "3": //Query Blogs and sort by name var listBlogs = db.Blogs.OrderBy(b => b.Name); //Make list of custom menu sorting objects List <MenuBlogId> menuBlogIds = new List <MenuBlogId>(); i = 1; Console.WriteLine("Blogs in database: " + db.Blogs.Count()); foreach (var item in listBlogs) { //create menu sorting object with index value i, and corresponding blogId menuBlogIds.Add(new MenuBlogId { menuId = i, blogId = item.BlogId }); //Display index value and Blog name Console.WriteLine(i++ + ") " + item.Name); } Console.Write("Enter the menu number of the Blog to post to:\n" + "==="); input = Validate.ValidateMenuSelection(Console.ReadLine(), 1, i); var intInput = int.Parse(input); //Get element at specific index from the custom list, //and find the blog in the UNSORTED db with the blog ID that matches int mBlogId = 0; foreach (MenuBlogId m in menuBlogIds) { if (m.menuId == intInput) { mBlogId = m.blogId; } } Blog chosenBlog = db.Blogs.Find(mBlogId); Console.Write("Enter post title:\n" + "="); var title = Validate.ValidateBlank(Console.ReadLine()); logger.Info("Title chosen - {title}", title); Console.Write("Enter post content:\n" + "="); var content = Validate.ValidateBlank(Console.ReadLine()); logger.Info("Content chosen - {content}", content); var post = new Post() { Title = title, Content = content, BlogId = chosenBlog.BlogId, Blog = chosenBlog }; db.AddPost(post); logger.Info("Post added - {name}", post.PostId); break; case "4": // Display all Blogs from the database query = db.Blogs.OrderBy(b => b.Name); menuBlogIds = new List <MenuBlogId>(); Console.WriteLine("Blogs in database: " + db.Blogs.Count()); i = 1; Console.WriteLine("0) Posts from all blogs"); foreach (var item in query) { menuBlogIds.Add(new MenuBlogId { menuId = i, blogId = item.BlogId }); Console.WriteLine(i++ + ") Posts from " + item); } Console.Write("Enter the menu number of the Blog to get the posts of:\n" + "==="); input = Validate.ValidateMenuSelection(Console.ReadLine(), 0, i); intInput = int.Parse(input); if (intInput == 0) { Console.WriteLine($"{db.Posts.Count()} posts(s) returned"); foreach (var item in db.Posts) { Console.WriteLine("=====\n" + item + "====="); } } else { mBlogId = 0; foreach (MenuBlogId m in menuBlogIds) { if (m.menuId == intInput) { mBlogId = m.blogId; } } var posts = db.Posts.Where(p => p.BlogId == mBlogId); chosenBlog = db.Blogs.Find(mBlogId); Console.WriteLine($"{posts.Count()} post(s) returned"); foreach (var item in posts) { Console.WriteLine("=====\n" + item + "====="); } } break; case "5": cont = false; break; } } } catch (Exception ex) { logger.Error(ex.Message); } logger.Info("Program ended"); }
public static void Main(string[] args) { logger.Info("Program started"); try { string answer; do { Console.WriteLine("Select action: \n1. Display Blogs\n2. Add Blog\n3. Create Post\nPress any other key to Quit"); answer = Console.ReadLine(); Console.Clear(); if (answer == "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); } Console.WriteLine(); } if (answer == "2") { var db = new BloggingContext(); var query = db.Blogs.OrderBy(b => b.Name); Console.Write("Enter a name for a new Blog: "); var name = Console.ReadLine(); bool valid = true; foreach (var item in query) { if (name.Equals(item.Name)) { valid = false; } } if (name.Equals("") || valid == false) { logger.Error($"INVALID: {name} is already blog name or is null."); Console.WriteLine($"INVALID: {name} is an invalid blog name or is null."); } else { var blog = new Blog { Name = name }; db.AddBlog(blog); logger.Info("Blog added - {name}", name); } } if (answer == "3") { var db = new BloggingContext(); var query = db.Blogs.OrderBy(b => b.BlogId); Console.WriteLine("Which blog would you like to enter a post in?"); int tempMax = 0; foreach (var item in query) { Console.WriteLine($"{item.BlogId}. {item.Name}"); } if (int.TryParse(Console.ReadLine(), out int blogNum)) { foreach (var item in query) { if (tempMax < item.BlogId) { tempMax = item.BlogId; } } if ((blogNum < tempMax) && (blogNum > 0)) { Post post = new Post(); Console.WriteLine("Title: "); post.Title = Console.ReadLine(); Console.WriteLine("Content: "); post.Content = Console.ReadLine(); post.BlogId = blogNum; db.AddPost(post); logger.Info($"Post {post.Title} added in BlogId: {blogNum}"); } else { Console.WriteLine("Invalid Blog Number"); logger.Error("Invalid Blog Number"); } } else { Console.WriteLine("Blog is NaN"); logger.Error("Blog is Nan"); } } } while (answer == "1" || answer == "2" || answer == "3"); } catch (Exception ex) { logger.Error(ex.Message); } logger.Info("Program ended"); }
static void Main(string[] args) { logger.Info("Program started"); int choice = 0; int blogId; var db = new BloggingContext(); while (choice != -1) { Console.WriteLine("1) Display all blogs \n2) Add Blog \n3) Create Post \n4) Display Posts\n-1) exit the program"); choice = Int32.Parse(Console.ReadLine()); switch (choice) { case 1: Console.WriteLine("All blogs in the database:"); DisplayBlogs(db); break; case 2: try { // Create and save a new Blog Console.Write("Enter a name for a new Blog: "); string name = Console.ReadLine(); if (name != "") { var blog = new Blog { Name = name }; db.AddBlog(blog); logger.Info("Blog added - {name}", name); } else { logger.Error("You did not add a name."); } } catch (Exception ex) { logger.Error(ex.Message); } break; case 3: try { Console.WriteLine("Select the blog you would like to post to: "); DisplayBlogs(db); blogId = Int32.Parse(Console.ReadLine()); var check = db.Blogs.Where(b => b.BlogId == blogId); if (check.Count() == 1) { Console.WriteLine("Enter the post title: "); string title = Console.ReadLine(); if (title != "") { Console.WriteLine("Enter the post content:"); string content = Console.ReadLine(); var post = new Post { Title = title, Content = content, BlogId = blogId }; db.AddPost(post); logger.Info("Post added - {title}", title); } else { logger.Error("You did not add a title."); } } else { logger.Error("There are no Blogs saved with that ID"); } } catch (Exception ex) { logger.Error(ex.Message); } break; case 4: try { Console.WriteLine("Select the blog you would like to post to: "); Console.WriteLine("0 - to get all posts"); DisplayBlogs(db); blogId = Int32.Parse(Console.ReadLine()); if (blogId == 0) { int RecordTotals = db.Posts.Count(); Console.WriteLine("There are " + RecordTotals + " posts"); var query = db.Posts; foreach (var item in query) { Console.WriteLine("Post title: " + item.Title + "\nPost content: " + item.Content + "\n Blog Title: " + item.Blog.Name); } } else { DisplayPosts(db, blogId); } } catch (Exception ex) { logger.Error(ex.Message); } break; } } logger.Info("Program ended"); }
static void Main(string[] args) { string choice; do { logger.Info("Program started"); Console.WriteLine("*****************************************"); Console.WriteLine("** Welcome to the Blog Database System **"); Console.WriteLine("*****************************************"); Console.WriteLine("Choose an Option"); Console.WriteLine("1) Display all Blogs"); Console.WriteLine("2) Add Blog"); Console.WriteLine("3) Create Post"); Console.WriteLine("4) Display Posts"); Console.WriteLine("Press enter to quit"); choice = Console.ReadLine(); var db = new BloggingContext(); if (choice == "1") { logger.Info("User choice: 1 - Display all Blogs"); try { // Display all Blogs from the database var query = db.Blogs.OrderBy(b => b.Name); Console.WriteLine("All blogs in the database:"); Console.WriteLine($"{query.Count()} blogs returned"); if (query.Count() != 0) { foreach (var item in query) { Console.WriteLine(item.Name); Console.WriteLine(""); } } else { Console.WriteLine("No blogs exist"); logger.Info("No blogs exist"); } } catch (Exception ex) { logger.Error(ex.Message); } } else if (choice == "2") { logger.Info("User choice: 2 - Add Blogs"); try { // Create and save a new Blog Console.Write("Enter a name for a new Blog: "); var name = Console.ReadLine(); if (name != "" && name != null) { var blog = new Blog { Name = name }; db.AddBlog(blog); logger.Info("Blog added - {name}", name); } else { logger.Info("Invalid name entered"); Console.WriteLine("Enter a valid blog name"); } } catch (Exception ex) { logger.Error(ex.Message); } } else if (choice == "3") { logger.Info("User choice: 3 - Create Post"); //promt user to select the blog they are posting to try { Console.WriteLine("Select the Blog for your Post"); //display blogs for options var query = db.Blogs.OrderBy(b => b.BlogId); foreach (var item in query) { Console.WriteLine($@"{item.BlogId}) {item.Name}"); } //create the post Post post = new Post(); //link post to blog using foreign key string blogID = Console.ReadLine(); char firstCharacter = blogID[0]; bool isNumber = Char.IsDigit(firstCharacter); if (!isNumber) { Console.WriteLine("Enter a valid integer"); logger.Info("Entry was not an integer"); } else { int postBlogID = int.Parse(blogID); post.BlogId = postBlogID; //get a list of blogIDs var blogList = db.Blogs.Select(b => b.BlogId); if (blogList.Contains <int>(postBlogID)) { //Once the blog is selected, the post details can be entered (title/content) Console.WriteLine("Enter Post title"); string title = Console.ReadLine(); if (title != "" && title != null) { post.Title = title; } else { Console.WriteLine("Invalid title entered"); logger.Info("Invalid title entry"); } Console.WriteLine("Enter Post content"); string content = Console.ReadLine(); if (content != "" && content != null) { post.Content = content; } else { Console.WriteLine("Post must contain content"); logger.Info("No post contents entered"); } //Posts should be saved to the Posts table - add post to posts list in blog db.AddPost(post); logger.Info($"Post added - {post.Title}"); } else { Console.WriteLine("That blog doesn't exist"); logger.Info("Invalide blog selection"); } } } catch (Exception ex) { logger.Error(ex.Message); logger.Error(ex.StackTrace); } } else if (choice == "4") { logger.Info("User choice: 4 - Display Posts"); //Prompt the user to select the blog they want to view Console.WriteLine("Select the Blog to see Posts"); //display blogs for options var query = db.Blogs.OrderBy(b => b.BlogId); foreach (var item in query) { Console.WriteLine($@"{item.BlogId}) {item.Name}"); } int blogChoice = int.Parse(Console.ReadLine()); //TODO: Once the Blog is selected, all Posts related to the selected blog should be display as well as the number of Posts var postDisplay = db.Posts.Where(p => p.BlogId.Equals(blogChoice)); var numberOfPosts = postDisplay.Count(); var blogName = db.Blogs.Where(b => b.BlogId.Equals(blogChoice)).Select(b => b.Name).FirstOrDefault(); //TODO: For each Post, display the Blog name, Post title and Post content Console.WriteLine($"All posts in the {blogName} blog:"); Console.WriteLine($"Number of Posts: {numberOfPosts}"); if (numberOfPosts != 0) { foreach (var item in postDisplay) { Console.WriteLine($"Blog: {blogName}\nPost title: {item.Title}\nPost Contents: {item.Content}"); } } else { Console.WriteLine("There are no posts for this blog"); logger.Info("No posts exist"); } } }while (choice == "1" || choice == "2" || choice == "3" || choice == "4"); logger.Info("Program ended"); }
public static void Main(string[] args) { logger.Info("Program started"); try { // Create and save a new Blog Console.Write("Blog Manager\n1. Display all blogs\n2. Add Blog\n3. Create Post"); var name = Console.ReadLine(); var db = new BloggingContext(); switch (name) { case "1": var blog = new Blog { Name = name }; db.AddBlog(blog); logger.Info("Blog added - {name}", name); break; case "2": // 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 "3": Console.WriteLine("Please select the blog to post to:"); var search = db.Blogs.OrderBy(b => b.Name); 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(); var post = new Post { Title = title }; Console.WriteLine("Enter the content of the Post"); var content = Console.ReadLine(); post.Content = content; db.AddPost(post); } } break; default: break; } } catch (Exception ex) { logger.Error(ex.Message); } logger.Info("Program ended"); }
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"); }
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"); }
public static void Main(string[] args) { Boolean blogIsRunning = true; logger.Info("Program started"); try { while (blogIsRunning == true) { Console.WriteLine("Select An Option"); Console.WriteLine("1.)Add A Blog"); Console.WriteLine("2.)Display All Blogs"); Console.WriteLine("3.)Create Post"); Console.WriteLine("4.)Display Posts"); int response = Convert.ToInt32(Console.ReadLine()); if (response == 1) { logger.Info("Option 1 Selected"); // Create and save a new Blog var nameLoop = true; var name = ""; while (nameLoop == true) { Console.Write("Enter a name for a new Blog: "); name = Console.ReadLine(); if (name.Trim() == "") { logger.Info("No Blog Name Found"); Console.WriteLine("Enter a valid Blog Name"); } else { nameLoop = false; } } var blog = new Blog { Name = name }; var db = new BloggingContext(); db.AddBlog(blog); logger.Info("Blog added - {name}", name); } else if (response == 2) { logger.Info("Option 2 Selected"); var db = new BloggingContext(); // Display all Blogs from the database var query = db.Blogs.OrderBy(b => b.BlogId); var counter = db.Blogs.Count(); if (counter > 0) { Console.WriteLine("All blogs in the database:"); Console.WriteLine(counter + " Blogs Found"); foreach (var item in query) { Console.WriteLine(item.Name); } } else { Console.WriteLine("No Blogs Stored"); logger.Error("No Blogs Stored"); } } else if (response == 3) { logger.Info("Option 3 Selected"); var db = new BloggingContext(); var query = db.Blogs.OrderBy(b => b.BlogId); Console.WriteLine("All blogs in the database:"); List <int> blogIDArray = new List <int> { }; foreach (var item in query) { Console.WriteLine("ID: " + item.BlogId.ToString() + " NAME: " + item.Name); blogIDArray.Add(item.BlogId); } Boolean valid = true; while (valid == true) { Console.WriteLine("Select Blog You Would Like To Change: "); int userChange = Convert.ToInt32(Console.ReadLine()); int chosenBlogID = 0; for (int i = 0; i < blogIDArray.Count; i++) { if (userChange == blogIDArray.ElementAt(i)) { valid = false; chosenBlogID = blogIDArray.IndexOf(blogIDArray.ElementAt(i)); } } if (valid == true) { Console.WriteLine("PLEASE ENTER A VALID ID"); } else { var validLoop = true; var title = ""; while (validLoop == true) { Console.WriteLine("Enter Post Title: "); title = Console.ReadLine(); if (title.Trim() == "") { logger.Info("No Title Added For Post"); Console.WriteLine("Please Add a Post Title"); } else { validLoop = false; } } Console.WriteLine("Enter Post Details: "); var details = Console.ReadLine(); var newID = 1; try { newID = db.Posts.Select(p => p.PostId).Last() + 1; } catch { Console.WriteLine("Post Stored"); } var post = new Post { Title = title, Content = details, BlogId = chosenBlogID + 1, PostId = newID }; db.AddPost(post); logger.Info("Post added - {title}", post); // Display all Blogs from the database var query2 = db.Blogs.OrderBy(b => b.Name); Console.WriteLine("Posts In Database:"); foreach (var item in query2) { Console.WriteLine(item.Name); } } } } else if (response == 4) { logger.Info("Option 4 Selected"); var validLoop = true; while (validLoop == true) { var db = new BloggingContext(); var query = db.Blogs.OrderBy(b => b.BlogId); Console.WriteLine("All blogs in the database:"); List <int> blogIDArray = new List <int> { }; Console.WriteLine("0.) Display All Posts"); foreach (var item in query) { Console.WriteLine("ID: " + item.BlogId.ToString() + " NAME: " + item.Name); blogIDArray.Add(item.BlogId); } var userResponse = Convert.ToInt32(Console.ReadLine()); if (userResponse == 0) { // Display all Blogs from the database var query3 = db.Posts.OrderBy(b => b.PostId); var counter = db.Posts.Count(); if (counter > 0) { Console.WriteLine("All Posts in the database:"); Console.WriteLine(counter + " Posts Found"); foreach (var item in query3) { Console.WriteLine("Title: " + item.Title + "\nContent: " + item.Content); } } else { Console.WriteLine("No Posts Stored"); logger.Error("No Posts Stored"); } validLoop = false; } else { int chosenBlogID = 0; for (int i = 0; i < blogIDArray.Count; i++) { if (userResponse == blogIDArray.ElementAt(i)) { validLoop = false; chosenBlogID = blogIDArray.IndexOf(blogIDArray.ElementAt(i)); } } if (validLoop == true) { Console.WriteLine("PLEASE ENTER A VALID ID"); } else { var postByBlog = db.Posts.OrderBy(b => b.Title).Where(b => b.BlogId == chosenBlogID); foreach (var item in postByBlog) { Console.WriteLine("Title: " + item.Title + "\nContent: " + item.Content); } } } } } } } catch (Exception ex) { logger.Error(ex.Message); } Console.WriteLine("Press enter to quit"); string x = Console.ReadLine(); logger.Info("Program ended"); }
public static void Main(string[] args) { logger.Info("Program started"); try { string choice = "1"; while (choice == "1" || choice == "2" || choice == "3") { Console.WriteLine("What would you want to do?"); Console.WriteLine("1) Display all blogs"); Console.WriteLine("2) Create a new blog"); Console.WriteLine("3) Post to a blog"); Console.WriteLine("4) Exit"); choice = Console.ReadLine(); 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(); 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") { BloggingContext context = new BloggingContext(); Post newpost = new Post(); Console.Write("Enter the name of blog you want to post in: "); string ablog = Console.ReadLine(); var blog = context.Blogs .Where(b => b.Name == ablog) .FirstOrDefault(); if (blog == null) { Console.WriteLine("You cannot post to this blog because it might be misspelled or an error occured."); logger.Warn("Query returned with {0}", blog); } else { Console.Write("Enter title of post: "); string title = Console.ReadLine(); Console.WriteLine("Enter post content: "); string content = Console.ReadLine(); newpost.Blog = blog; newpost.Title = title; newpost.Content = content; logger.Info("New blog post with title " + title + " on the blog " + blog); } } else if (choice == "4") { } else { Console.WriteLine("Invalid choice."); choice = "1"; } } } // end of try catch (Exception ex) { logger.Error(ex.Message); } Console.WriteLine("Press enter to Quit"); Console.ReadLine(); logger.Info("Program ended"); }
static void Main(string[] args) { logger.Info("Program started"); try { string choice; do { Console.WriteLine("Choose an option:"); Console.WriteLine("1) Display all blogs"); Console.WriteLine("2) Add Blog"); Console.WriteLine("3) Create Post"); Console.WriteLine("4) Display Posts"); Console.WriteLine("Enter q to quit"); choice = Console.ReadLine(); Console.Clear(); 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") { Console.Write("Enter a name for a new Blog: "); var blog = new Blog { Name = Console.ReadLine() }; ValidationContext context = new ValidationContext(blog, null, null); List <ValidationResult> results = new List <ValidationResult>(); var isValid = Validator.TryValidateObject(blog, context, results, true); if (isValid) { var db = new BloggingContext(); if (db.Blogs.Any(b => b.Name == blog.Name)) { isValid = false; results.Add(new ValidationResult("Blog name exists", new string[] { "Name" })); } else { logger.Info("Validation passed"); db.AddBlog(blog); logger.Info("Blog added - {name}", blog.Name); } } if (!isValid) { foreach (var result in results) { logger.Error($"{result.MemberNames.First()} : {result.ErrorMessage}"); } } } else if (choice == "3") { var db = new BloggingContext(); var query = db.Blogs.OrderBy(b => b.BlogId); Console.WriteLine("Select the blog you would to post to:"); 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 = CreatePost(db); if (post != null) { post.BlogId = BlogId; db.AddPost(post); logger.Info("Post added - {title}", post.Title); } } else { logger.Error("There are no Blogs saved with that Id"); } } else { logger.Error("Invalid Blog Id"); } } else if (choice == "4") { var db = new BloggingContext(); var query = db.Blogs.OrderBy(b => b.BlogId); foreach (var item in query) { Console.WriteLine($"{item.BlogId}) Posts from {item.Name}"); } if (int.TryParse("0", out int BlogId)) { IEnumerable <Post> Posts; if (BlogId != 0 && db.Blogs.Count(b => b.BlogId == BlogId) == 0) { logger.Error("No matching Blogs"); } else { Posts = db.Posts.OrderBy(p => p.Title); if (BlogId == 0) { Posts = db.Posts.OrderBy(p => p.Title); } else { 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.ToLower() != "q"); } catch (Exception ex) { logger.Error(ex.Message); } logger.Info("Program ended"); }
static void Main(string[] args) { logger.Info("Program started"); var db = new BloggingContext(); var query = db.Blogs.OrderBy(b => b.Name); Console.WriteLine("\nWhich would you like to do?\n1. Display all blogs\n2. Add blog\n3. Create post\n4. Display posts\n"); try { int choice = Int32.Parse(Console.ReadLine()); if (choice == 1) { // Display all Blogs from the database Console.WriteLine("All blogs in the database:"); foreach (var item in query) { Console.WriteLine(item.Name); } } else 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); } else if (choice == 3) { Post post = new Post(); Console.WriteLine("Available blogs:"); foreach (var item in query) { Console.WriteLine(item.Name); } Console.WriteLine("\nEnter the name of the blog you would like to post to:"); try { string selection = Console.ReadLine(); Blog selectedBlog = db.Blogs.First(b => b.Name.Contains(selection)); Console.Write("What is the title of the post: "); post.Title = Console.ReadLine(); Console.Write("What is the content of the post: "); post.Content = Console.ReadLine(); post.BlogId = selectedBlog.BlogId; post.Blog = selectedBlog; db.AddPost(post); } catch (Exception xx) { logger.Error(xx.Message); Console.WriteLine("Invalid choice"); } } else if (choice == 4) { var blogs = query.ToArray(); int count = 1; int counter = 0; foreach (var item in blogs) { Console.WriteLine(count + ". " + item.Name); count++; } Console.Write("\nEnter the number of the blog you would like to view the posts from: "); try { int selection = Convert.ToInt32(Console.ReadLine()); var selectedPosts = db.Posts.Where(b => b.BlogId.Equals(blogs[selection - 1].BlogId)); foreach (Post post in selectedPosts) { Console.WriteLine($"Blog name: {post.Blog.Name}, Title: {post.Title}, Content: {post.Content}"); counter++; } Console.WriteLine("\nNumber of posts in this blog: " + counter + "\n"); } catch (Exception xz) { logger.Error(xz.Message); } } logger.Info("\nProgram ended"); } catch (Exception xy) { logger.Error(xy.Message); } }