From 783e5f5997b1c794960c26909ee4bc150f108b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB=20=D0=9F=D1=83=D1=82=D0=B8?= =?UTF-8?q?=D0=BB=D0=B8=D0=BD?= Date: Wed, 19 Apr 2023 16:40:54 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D0=B5=D1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- News/News/Controller.cs | 60 +++++++++++++++++++++++++ News/News/EnterText.txt | 7 +++ News/News/Program.cs | 98 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 News/News/Controller.cs create mode 100644 News/News/EnterText.txt diff --git a/News/News/Controller.cs b/News/News/Controller.cs new file mode 100644 index 0000000..c7f5e97 --- /dev/null +++ b/News/News/Controller.cs @@ -0,0 +1,60 @@ +using ConsoleTableExt; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace News +{ + internal class Controller + { + private readonly NewsContext _context; + + public Controller(NewsContext feedContext) + { + _context = feedContext; + } + + public void GetNews() + { + ConsoleTableBuilder.From(_context.News.Select(x => new { x.NewsId, x.NewsName, x.NewsDate }).ToList()) + .WithTitle("News") + .ExportAndWriteLine(); + } + + public void GetComments() + { + ConsoleTableBuilder.From(_context.Comments.Select(x => new { x.UserId, x.NewsId, x.CommentText }).ToList()) + .WithTitle("Comments") + .ExportAndWriteLine(); + } + + public void GetAuthors() + { + ConsoleTableBuilder.From(_context.Authors.Select(x => new { x.AuthorId, x.AuthorName}).ToList()) + .WithTitle("Authors") + .ExportAndWriteLine(); + } + + public void GetUsers() + { + ConsoleTableBuilder.From(_context.Users.Select(x => new { x.UserId, x.UserName }).ToList()) + .WithTitle("Users") + .ExportAndWriteLine(); + } + + public void AddUser(User User) + { + try + { + _context.Users.Add(User); + _context.SaveChanges(); + } + catch (Exception e) + { + Console.WriteLine(e.InnerException.Message); + } + } + } +} diff --git a/News/News/EnterText.txt b/News/News/EnterText.txt new file mode 100644 index 0000000..c7e2d48 --- /dev/null +++ b/News/News/EnterText.txt @@ -0,0 +1,7 @@ +0 +1 +2 +3 +4 +5 +6 \ No newline at end of file diff --git a/News/News/Program.cs b/News/News/Program.cs index 3751555..201c34b 100644 --- a/News/News/Program.cs +++ b/News/News/Program.cs @@ -1,2 +1,96 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using ConsoleTableExt; +using System.Diagnostics; + +namespace News +{ + class Program + { + public static void Main(string[] args) + { + string text; + using (var sr = new StreamReader("EnterText.txt")) + { + text = sr.ReadToEnd(); + } + + using var context = new NewsContext(); + Controller controller = new(context); + string operation = ""; + do + { + Console.WriteLine(text); + operation = Console.ReadLine() ?? ""; + switch (operation) + { + case "0": + break; + case "1": + controller.GetNews(); + break; + case "2": + controller.GetComments(); + break; + case "3": + controller.GetAuthors(); + break; + case "4": + controller.GetUsers(); + break; + case "5": + Console.WriteLine("Введите Имя"); + User user = new() + { + UserName = Console.ReadLine(), + }; + controller.AddUser(user); + break; + case "6": + Test(controller, context); + break; + default: + Console.WriteLine("Такой операции нет"); + break; + } + } while (operation != "0"); + } + public static void Test(Controller controller, NewsContext context) + { + + + + var result = new[] + { + new { Name = "1", timems = MeasureTime(controller.GetNews)}, + new { Name = "2", timems = MeasureTime(controller.GetComments)}, + new { Name = "3", timems = MeasureTime(controller.GetAuthors)}, + new { Name = "4", timems = MeasureTime(() => controller.GetUsers())}, + new { Name = "5", timems = MeasureTime(() => + { + Random r = new Random(); + string name = ""; + for (int i = 0; i < 7; i++) + name += (char)r.Next(65, 89); + User user = new() { + UserName = name[..7], + }; + controller.AddUser(user); + })}, + }.ToList(); + ConsoleTableBuilder.From(result).ExportAndWriteLine(); + } + + public static double MeasureTime(Action action, int count = 10) + { + long sumTime = 0; + Stopwatch stopwatch = new(); + for (int i = 0; i < count; i++) + { + stopwatch.Start(); + action.Invoke(); + stopwatch.Stop(); + sumTime += stopwatch.ElapsedMilliseconds; + } + return sumTime / count; + } + } +}