добавление контролера и замеры времени
This commit is contained in:
parent
ac09bc4156
commit
f9133c282b
76
Subd/Controller.cs
Normal file
76
Subd/Controller.cs
Normal file
@ -0,0 +1,76 @@
|
||||
using ConsoleTableExt;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Subd
|
||||
{
|
||||
internal class Controller
|
||||
{
|
||||
private readonly Onlinecinema2Context _context;
|
||||
|
||||
public Controller(Onlinecinema2Context feedContext)
|
||||
{
|
||||
_context = feedContext;
|
||||
}
|
||||
|
||||
public void GetFilms()
|
||||
{
|
||||
ConsoleTableBuilder.From(_context.Films.Select(x => new { x.IdFilm, x.NameFilm, x.Genre }).ToList())
|
||||
.WithTitle("Films")
|
||||
.ExportAndWriteLine();
|
||||
}
|
||||
|
||||
public void GetDirectors()
|
||||
{
|
||||
ConsoleTableBuilder.From(_context.Directors.Select(x => new { x.IdDirector, x.Name, x.Date }).ToList())
|
||||
.WithTitle("Directors")
|
||||
.ExportAndWriteLine();
|
||||
}
|
||||
|
||||
public void GetStudios()
|
||||
{
|
||||
ConsoleTableBuilder.From(_context.Studios.Select(x => new { x.IdStudio, x.Name }).ToList())
|
||||
.WithTitle("Studios")
|
||||
.ExportAndWriteLine();
|
||||
}
|
||||
|
||||
public void GetUsers()
|
||||
{
|
||||
ConsoleTableBuilder.From(_context.Users.Select(x => new { x.IdUser, x.Nikname }).ToList())
|
||||
.WithTitle("Users")
|
||||
.ExportAndWriteLine();
|
||||
}
|
||||
|
||||
public void AddUser(User User)
|
||||
{
|
||||
try
|
||||
{
|
||||
_context.Users.Add(User);
|
||||
_context.SaveChanges();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.InnerException.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public UserRating? GetReview(int IdUser, int IdFilm)
|
||||
{
|
||||
return _context.UserRatings.FirstOrDefault(x => x.IdUser == IdUser && x.IdFilm == IdFilm);
|
||||
}
|
||||
|
||||
public void RemoveReview(UserRating Review)
|
||||
{
|
||||
_context.UserRatings.Remove(Review);
|
||||
_context.SaveChanges();
|
||||
}
|
||||
|
||||
public void UpdateReview(UserRating Review)
|
||||
{
|
||||
_context.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
8
Subd/EnterText.txt
Normal file
8
Subd/EnterText.txt
Normal file
@ -0,0 +1,8 @@
|
||||
0 Выход
|
||||
1 Получить все фильмы
|
||||
2 Получить всех режисёров
|
||||
3 Получить все студии
|
||||
4 Получить всех пользователей
|
||||
5 Добавить пользователя
|
||||
6 Запустить тестирование всех функций
|
||||
7 Изменить текст ревью или удалить ревью
|
107
Subd/Program.cs
107
Subd/Program.cs
@ -1,2 +1,105 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
Console.WriteLine("Hello, World!");
|
||||
using ConsoleTableExt;
|
||||
using Subd;
|
||||
using System.Diagnostics;
|
||||
|
||||
class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
|
||||
string text;
|
||||
using (var sr = new StreamReader("D:\\сборник говуза\\СУБД\\prog\\Subd\\EnterText.txt"))
|
||||
{
|
||||
text = sr.ReadToEnd();
|
||||
}
|
||||
|
||||
using var context = new Onlinecinema2Context();
|
||||
Controller controller = new(context);
|
||||
string operation = "";
|
||||
do
|
||||
{
|
||||
Console.WriteLine(text);
|
||||
operation = Console.ReadLine() ?? "";
|
||||
switch (operation)
|
||||
{
|
||||
case "0":
|
||||
break;
|
||||
case "1":
|
||||
controller.GetFilms();
|
||||
break;
|
||||
case "2":
|
||||
controller.GetDirectors();
|
||||
break;
|
||||
case "3":
|
||||
controller.GetStudios();
|
||||
break;
|
||||
case "4":
|
||||
controller.GetUsers();
|
||||
break;
|
||||
case "5":
|
||||
Console.WriteLine("Введите никнейм)");
|
||||
User User = new()
|
||||
{
|
||||
Nikname = Console.ReadLine(),
|
||||
};
|
||||
controller.AddUser(User);
|
||||
break;
|
||||
case "6":
|
||||
Test(controller, context);
|
||||
break;
|
||||
case "7":
|
||||
Console.WriteLine($"Введите Id фильма и Id пользователя");
|
||||
var IdUser = Convert.ToInt32(Console.ReadLine());
|
||||
var IdFilm = Convert.ToInt32(Console.ReadLine());
|
||||
var review = controller.GetReview(IdUser, IdFilm);
|
||||
Console.WriteLine($"1 для изменения, 2 для удаления");
|
||||
var r = Console.ReadLine();
|
||||
if (r == "1")
|
||||
{
|
||||
Console.WriteLine($"Введите новый текст ревью");
|
||||
var s = Console.ReadLine();
|
||||
review.Reviev = s;
|
||||
controller.UpdateReview(review);
|
||||
}
|
||||
if (r == "2")
|
||||
{
|
||||
controller.RemoveReview(review);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Такой команды нет");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine("Некорректно введенный номер операции");
|
||||
break;
|
||||
}
|
||||
} while (operation != "0");
|
||||
}
|
||||
public static void Test(Controller controller, Onlinecinema2Context context)
|
||||
{
|
||||
var result = new[]
|
||||
{
|
||||
new { Name = "1", timems = MeasureTime(controller.GetFilms)},
|
||||
new { Name = "2", timems = MeasureTime(controller.GetDirectors)},
|
||||
new { Name = "3", timems = MeasureTime(controller.GetStudios)},
|
||||
new { Name = "4", timems = MeasureTime(controller.GetUsers)},
|
||||
|
||||
}.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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user