конец

This commit is contained in:
Павел Путилин 2023-04-19 16:40:54 +04:00
parent d59f55d549
commit 783e5f5997
3 changed files with 163 additions and 2 deletions

60
News/News/Controller.cs Normal file
View File

@ -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);
}
}
}
}

7
News/News/EnterText.txt Normal file
View File

@ -0,0 +1,7 @@
0 Выход
1 Получить все новости
2 Получить все комментарии
3 Получить всех авторов
4 Получить всех пользователей
5 Добавить пользователя
6 Запустить тестирование всех функций

View File

@ -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;
}
}
}