From 15c9409f760a21845d8e6c76d1fa01494fdb4ea9 Mon Sep 17 00:00:00 2001 From: Factorino73 Date: Tue, 2 Apr 2024 02:15:17 +0400 Subject: [PATCH] DatabaseImplement WIP --- RouteGuide/RouteGuide.sln | 10 +- RouteGuide/RouteGuideBusinessLogics/Class1.cs | 7 + .../RouteGuideBusinessLogics.csproj | 9 + .../StoragesContracts/IRouteStorage.cs | 2 +- .../StoragesContracts/IScheduleStorage.cs | 2 +- .../StoragesContracts/ITransportStorage.cs | 2 +- .../Implements/DriverStorage.cs | 125 ++++++++++++++ .../Implements/RouteStorage.cs | 150 +++++++++++++++++ .../Implements/ScheduleStorage.cs | 129 +++++++++++++++ .../Implements/StopStorage.cs | 125 ++++++++++++++ .../Implements/TransportStorage.cs | 129 +++++++++++++++ .../Models/Driver.cs | 109 +++++++++++++ .../Models/Route.cs | 154 ++++++++++++++++++ .../Models/RouteStop.cs | 48 ++++++ .../Models/Schedule.cs | 115 +++++++++++++ .../Models/Stop.cs | 111 +++++++++++++ .../Models/Transport.cs | 105 ++++++++++++ .../RouteGuideDatabase.cs | 64 ++++++++ .../RouteGuideDatabaseImplement.csproj | 23 +++ 19 files changed, 1414 insertions(+), 5 deletions(-) create mode 100644 RouteGuide/RouteGuideBusinessLogics/Class1.cs create mode 100644 RouteGuide/RouteGuideBusinessLogics/RouteGuideBusinessLogics.csproj create mode 100644 RouteGuide/RouteGuideDatabaseImplement/Implements/DriverStorage.cs create mode 100644 RouteGuide/RouteGuideDatabaseImplement/Implements/RouteStorage.cs create mode 100644 RouteGuide/RouteGuideDatabaseImplement/Implements/ScheduleStorage.cs create mode 100644 RouteGuide/RouteGuideDatabaseImplement/Implements/StopStorage.cs create mode 100644 RouteGuide/RouteGuideDatabaseImplement/Implements/TransportStorage.cs create mode 100644 RouteGuide/RouteGuideDatabaseImplement/Models/Driver.cs create mode 100644 RouteGuide/RouteGuideDatabaseImplement/Models/Route.cs create mode 100644 RouteGuide/RouteGuideDatabaseImplement/Models/RouteStop.cs create mode 100644 RouteGuide/RouteGuideDatabaseImplement/Models/Schedule.cs create mode 100644 RouteGuide/RouteGuideDatabaseImplement/Models/Stop.cs create mode 100644 RouteGuide/RouteGuideDatabaseImplement/Models/Transport.cs create mode 100644 RouteGuide/RouteGuideDatabaseImplement/RouteGuideDatabase.cs create mode 100644 RouteGuide/RouteGuideDatabaseImplement/RouteGuideDatabaseImplement.csproj diff --git a/RouteGuide/RouteGuide.sln b/RouteGuide/RouteGuide.sln index c9aaa6c..d51dd79 100644 --- a/RouteGuide/RouteGuide.sln +++ b/RouteGuide/RouteGuide.sln @@ -5,9 +5,11 @@ VisualStudioVersion = 17.8.34525.116 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RouteGuideView", "RouteGuideView\RouteGuideView.csproj", "{4116AC9B-4035-4701-9C35-049AA6A83B96}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RouteGuideDataModels", "RouteGuideDataModels\RouteGuideDataModels.csproj", "{734B62E6-F4F0-4356-A4BC-067F52416282}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RouteGuideDataModels", "RouteGuideDataModels\RouteGuideDataModels.csproj", "{734B62E6-F4F0-4356-A4BC-067F52416282}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RouteGuideContracts", "RouteGuideContracts\RouteGuideContracts.csproj", "{895094AD-E3AC-4E1F-83DC-283E65EB072F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RouteGuideContracts", "RouteGuideContracts\RouteGuideContracts.csproj", "{895094AD-E3AC-4E1F-83DC-283E65EB072F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RouteGuideDatabaseImplement", "RouteGuideDatabaseImplement\RouteGuideDatabaseImplement.csproj", "{5C2BBCAA-01A8-4B4C-B296-E7CF8DFBA884}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -27,6 +29,10 @@ Global {895094AD-E3AC-4E1F-83DC-283E65EB072F}.Debug|Any CPU.Build.0 = Debug|Any CPU {895094AD-E3AC-4E1F-83DC-283E65EB072F}.Release|Any CPU.ActiveCfg = Release|Any CPU {895094AD-E3AC-4E1F-83DC-283E65EB072F}.Release|Any CPU.Build.0 = Release|Any CPU + {5C2BBCAA-01A8-4B4C-B296-E7CF8DFBA884}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5C2BBCAA-01A8-4B4C-B296-E7CF8DFBA884}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5C2BBCAA-01A8-4B4C-B296-E7CF8DFBA884}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5C2BBCAA-01A8-4B4C-B296-E7CF8DFBA884}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/RouteGuide/RouteGuideBusinessLogics/Class1.cs b/RouteGuide/RouteGuideBusinessLogics/Class1.cs new file mode 100644 index 0000000..b9837c8 --- /dev/null +++ b/RouteGuide/RouteGuideBusinessLogics/Class1.cs @@ -0,0 +1,7 @@ +namespace RouteGuideBusinessLogics +{ + public class Class1 + { + + } +} diff --git a/RouteGuide/RouteGuideBusinessLogics/RouteGuideBusinessLogics.csproj b/RouteGuide/RouteGuideBusinessLogics/RouteGuideBusinessLogics.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/RouteGuide/RouteGuideBusinessLogics/RouteGuideBusinessLogics.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/RouteGuide/RouteGuideContracts/StoragesContracts/IRouteStorage.cs b/RouteGuide/RouteGuideContracts/StoragesContracts/IRouteStorage.cs index 3523e38..7f00cae 100644 --- a/RouteGuide/RouteGuideContracts/StoragesContracts/IRouteStorage.cs +++ b/RouteGuide/RouteGuideContracts/StoragesContracts/IRouteStorage.cs @@ -39,7 +39,7 @@ namespace RouteGuideContracts.StoragesContracts /// /// /// - TransportViewModel? Insert(RouteBindingModel model); + RouteViewModel? Insert(RouteBindingModel model); /// /// Редактирование элемента diff --git a/RouteGuide/RouteGuideContracts/StoragesContracts/IScheduleStorage.cs b/RouteGuide/RouteGuideContracts/StoragesContracts/IScheduleStorage.cs index 833961f..ef6782a 100644 --- a/RouteGuide/RouteGuideContracts/StoragesContracts/IScheduleStorage.cs +++ b/RouteGuide/RouteGuideContracts/StoragesContracts/IScheduleStorage.cs @@ -32,7 +32,7 @@ namespace RouteGuideContracts.StoragesContracts /// /// /// - ScheduleViewModel? GetElement(ScheduleBindingModel model); + ScheduleViewModel? GetElement(ScheduleSearchModel model); /// /// Добавление элемента diff --git a/RouteGuide/RouteGuideContracts/StoragesContracts/ITransportStorage.cs b/RouteGuide/RouteGuideContracts/StoragesContracts/ITransportStorage.cs index 2e14283..2e723bc 100644 --- a/RouteGuide/RouteGuideContracts/StoragesContracts/ITransportStorage.cs +++ b/RouteGuide/RouteGuideContracts/StoragesContracts/ITransportStorage.cs @@ -39,7 +39,7 @@ namespace RouteGuideContracts.StoragesContracts /// /// /// - DriverViewModel? Insert(TransportBindingModel model); + TransportViewModel? Insert(TransportBindingModel model); /// /// Редактирование элемента diff --git a/RouteGuide/RouteGuideDatabaseImplement/Implements/DriverStorage.cs b/RouteGuide/RouteGuideDatabaseImplement/Implements/DriverStorage.cs new file mode 100644 index 0000000..948576e --- /dev/null +++ b/RouteGuide/RouteGuideDatabaseImplement/Implements/DriverStorage.cs @@ -0,0 +1,125 @@ +using RouteGuideContracts.BindingModels; +using RouteGuideContracts.SearchModels; +using RouteGuideContracts.StoragesContracts; +using RouteGuideContracts.ViewModels; +using RouteGuideDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RouteGuideDatabaseImplement.Implements +{ + /// + /// Хранилище для сущности "Водитель" + /// + public class DriverStorage : IDriverStorage + { + /// + /// Получение полного списка + /// + /// + public List GetFullList() + { + using var context = new RouteGuideDatabase(); + return context.Drivers + .Select(x => x.GetViewModel) + .ToList(); + } + + /// + /// Получение фильтрованного списка + /// + /// + /// + public List GetFilteredList(DriverSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new RouteGuideDatabase(); + return context.Drivers + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + /// + /// Получение элемента + /// + /// + /// + public DriverViewModel? GetElement(DriverSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new RouteGuideDatabase(); + return context.Drivers + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + /// + /// Добавление элемента + /// + /// + /// + public DriverViewModel? Insert(DriverBindingModel model) + { + var newDriver = Driver.Create(model); + if (newDriver == null) + { + return null; + } + + using var context = new RouteGuideDatabase(); + context.Drivers.Add(newDriver); + context.SaveChanges(); + return newDriver.GetViewModel; + } + + /// + /// Редактирование элемента + /// + /// + /// + public DriverViewModel? Update(DriverBindingModel model) + { + using var context = new RouteGuideDatabase(); + var driver = context.Drivers.FirstOrDefault(x => x.Id == model.Id); + if (driver == null) + { + return null; + } + + driver.Update(model); + context.SaveChanges(); + return driver.GetViewModel; + } + + /// + /// Удаление элемента + /// + /// + /// + public DriverViewModel? Delete(DriverBindingModel model) + { + using var context = new RouteGuideDatabase(); + var driver = context.Drivers.FirstOrDefault(x => x.Id == model.Id); + if (driver == null) + { + return null; + } + + context.Drivers.Remove(driver); + context.SaveChanges(); + return driver.GetViewModel; + } + } +} diff --git a/RouteGuide/RouteGuideDatabaseImplement/Implements/RouteStorage.cs b/RouteGuide/RouteGuideDatabaseImplement/Implements/RouteStorage.cs new file mode 100644 index 0000000..0ae8099 --- /dev/null +++ b/RouteGuide/RouteGuideDatabaseImplement/Implements/RouteStorage.cs @@ -0,0 +1,150 @@ +using Microsoft.EntityFrameworkCore; +using RouteGuideContracts.BindingModels; +using RouteGuideContracts.SearchModels; +using RouteGuideContracts.StoragesContracts; +using RouteGuideContracts.ViewModels; +using RouteGuideDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RouteGuideDatabaseImplement.Implements +{ + /// + /// Хранилище для сущности "Маршрут" + /// + public class RouteStorage : IRouteStorage + { + /// + /// Получение полного списка + /// + /// + public List GetFullList() + { + using var context = new RouteGuideDatabase(); + return context.Routes + .Include(x => x.Transport) + .Include(x => x.Stops) + .ThenInclude(x => x.Stop) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + /// + /// Получение фильтрованного списка + /// + /// + /// + public List GetFilteredList(RouteSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new RouteGuideDatabase(); + return context.Routes + .Include(x => x.Transport) + .Include(x => x.Stops) + .ThenInclude(x => x.Stop) + .ToList() + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + /// + /// Получение элемента + /// + /// + /// + public RouteViewModel? GetElement(RouteSearchModel model) + { + if (!model.Id.HasValue && string.IsNullOrEmpty(model.Name)) + { + return null; + } + + using var context = new RouteGuideDatabase(); + return context.Routes + .Include(x => x.Transport) + .Include(x => x.Stops) + .ThenInclude(x => x.Stop) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name.Contains(model.Name)) || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + /// + /// Добавление элемента + /// + /// + /// + public RouteViewModel? Insert(RouteBindingModel model) + { + using var context = new RouteGuideDatabase(); + var newRoute = Route.Create(context, model); + if (newRoute == null) + { + return null; + } + + context.Routes.Add(newRoute); + context.SaveChanges(); + return newRoute.GetViewModel; + } + + /// + /// Редактирование элемента + /// + /// + /// + public RouteViewModel? Update(RouteBindingModel model) + { + using var context = new RouteGuideDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var route = context.Routes.FirstOrDefault(x => x.Id == model.Id); + if (route == null) + { + return null; + } + + route.Update(model); + context.SaveChanges(); + route.UpdateStops(context, model); + transaction.Commit(); + return route.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + /// + /// Удаление элемента + /// + /// + /// + public RouteViewModel? Delete(RouteBindingModel model) + { + using var context = new RouteGuideDatabase(); + var route = context.Routes + .Include(x => x.Stops) + .FirstOrDefault(rec => rec.Id == model.Id); + if (route == null) + { + return null; + } + + context.Routes.Remove(route); + context.SaveChanges(); + return route.GetViewModel; + } + } +} diff --git a/RouteGuide/RouteGuideDatabaseImplement/Implements/ScheduleStorage.cs b/RouteGuide/RouteGuideDatabaseImplement/Implements/ScheduleStorage.cs new file mode 100644 index 0000000..5abbb54 --- /dev/null +++ b/RouteGuide/RouteGuideDatabaseImplement/Implements/ScheduleStorage.cs @@ -0,0 +1,129 @@ +using Microsoft.EntityFrameworkCore; +using RouteGuideContracts.BindingModels; +using RouteGuideContracts.SearchModels; +using RouteGuideContracts.StoragesContracts; +using RouteGuideContracts.ViewModels; +using RouteGuideDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RouteGuideDatabaseImplement.Implements +{ + /// + /// Хранилище для сущности "Расписание" + /// + public class ScheduleStorage : IScheduleStorage + { + /// + /// Получение полного списка + /// + /// + public List GetFullList() + { + using var context = new RouteGuideDatabase(); + return context.Schedules + .Include(x => x.Route) + .Select(x => x.GetViewModel) + .ToList(); + } + + /// + /// Получение фильтрованного списка + /// + /// + /// + public List GetFilteredList(ScheduleSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new RouteGuideDatabase(); + return context.Schedules + .Include(x => x.Route) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + /// + /// Получение элемента + /// + /// + /// + public ScheduleViewModel? GetElement(ScheduleSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new RouteGuideDatabase(); + return context.Schedules + .Include(x => x.Route) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + /// + /// Добавление элемента + /// + /// + /// + public ScheduleViewModel? Insert(ScheduleBindingModel model) + { + using var context = new RouteGuideDatabase(); + var newSchedule = Schedule.Create(context, model); + if (newSchedule == null) + { + return null; + } + + context.Schedules.Add(newSchedule); + context.SaveChanges(); + return newSchedule.GetViewModel; + } + + /// + /// Редактирование элемента + /// + /// + /// + public ScheduleViewModel? Update(ScheduleBindingModel model) + { + using var context = new RouteGuideDatabase(); + var schedule = context.Schedules.FirstOrDefault(x => x.Id == model.Id); + if (schedule == null) + { + return null; + } + + schedule.Update(model); + context.SaveChanges(); + return schedule.GetViewModel; + } + + /// + /// Удаление элемента + /// + /// + /// + public ScheduleViewModel? Delete(ScheduleBindingModel model) + { + using var context = new RouteGuideDatabase(); + var schedule = context.Schedules.FirstOrDefault(x => x.Id == model.Id); + if (schedule == null) + { + return null; + } + + context.Schedules.Remove(schedule); + context.SaveChanges(); + return schedule.GetViewModel; + } + } +} diff --git a/RouteGuide/RouteGuideDatabaseImplement/Implements/StopStorage.cs b/RouteGuide/RouteGuideDatabaseImplement/Implements/StopStorage.cs new file mode 100644 index 0000000..46914ba --- /dev/null +++ b/RouteGuide/RouteGuideDatabaseImplement/Implements/StopStorage.cs @@ -0,0 +1,125 @@ +using RouteGuideContracts.BindingModels; +using RouteGuideContracts.SearchModels; +using RouteGuideContracts.StoragesContracts; +using RouteGuideContracts.ViewModels; +using RouteGuideDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RouteGuideDatabaseImplement.Implements +{ + /// + /// Хранилище для сущности "Остановка" + /// + public class StopStorage : IStopStorage + { + /// + /// Получение полного списка + /// + /// + public List GetFullList() + { + using var context = new RouteGuideDatabase(); + return context.Stops + .Select(x => x.GetViewModel) + .ToList(); + } + + /// + /// Получение фильтрованного списка + /// + /// + /// + public List GetFilteredList(StopSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new RouteGuideDatabase(); + return context.Stops + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + /// + /// Получение элемента + /// + /// + /// + public StopViewModel? GetElement(StopSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new RouteGuideDatabase(); + return context.Stops + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + /// + /// Добавление элемента + /// + /// + /// + public StopViewModel? Insert(StopBindingModel model) + { + var newStop = Stop.Create(model); + if (newStop == null) + { + return null; + } + + using var context = new RouteGuideDatabase(); + context.Stops.Add(newStop); + context.SaveChanges(); + return newStop.GetViewModel; + } + + /// + /// Редактирование элемента + /// + /// + /// + public StopViewModel? Update(StopBindingModel model) + { + using var context = new RouteGuideDatabase(); + var stop = context.Stops.FirstOrDefault(x => x.Id == model.Id); + if (stop == null) + { + return null; + } + + stop.Update(model); + context.SaveChanges(); + return stop.GetViewModel; + } + + /// + /// Удаление элемента + /// + /// + /// + public StopViewModel? Delete(StopBindingModel model) + { + using var context = new RouteGuideDatabase(); + var stop = context.Stops.FirstOrDefault(x => x.Id == model.Id); + if (stop == null) + { + return null; + } + + context.Stops.Remove(stop); + context.SaveChanges(); + return stop.GetViewModel; + } + } +} diff --git a/RouteGuide/RouteGuideDatabaseImplement/Implements/TransportStorage.cs b/RouteGuide/RouteGuideDatabaseImplement/Implements/TransportStorage.cs new file mode 100644 index 0000000..fbc8ace --- /dev/null +++ b/RouteGuide/RouteGuideDatabaseImplement/Implements/TransportStorage.cs @@ -0,0 +1,129 @@ +using Microsoft.EntityFrameworkCore; +using RouteGuideContracts.BindingModels; +using RouteGuideContracts.SearchModels; +using RouteGuideContracts.StoragesContracts; +using RouteGuideContracts.ViewModels; +using RouteGuideDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RouteGuideDatabaseImplement.Implements +{ + /// + /// Хранилище для сущности "Транспорт" + /// + public class TransportStorage : ITransportStorage + { + /// + /// Получение полного списка + /// + /// + public List GetFullList() + { + using var context = new RouteGuideDatabase(); + return context.Transport + .Include(x => x.Driver) + .Select(x => x.GetViewModel) + .ToList(); + } + + /// + /// Получение фильтрованного списка + /// + /// + /// + public List GetFilteredList(TransportSearchModel model) + { + if (string.IsNullOrEmpty(model.License)) + { + return new(); + } + + using var context = new RouteGuideDatabase(); + return context.Transport + .Include(x => x.Driver) + .Where(x => x.License.Contains(model.License)) + .Select(x => x.GetViewModel) + .ToList(); + } + + /// + /// Получение элемента + /// + /// + /// + public TransportViewModel? GetElement(TransportSearchModel model) + { + if (!model.Id.HasValue && string.IsNullOrEmpty(model.License)) + { + return null; + } + + using var context = new RouteGuideDatabase(); + return context.Transport + .Include(x => x.Driver) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.License) && x.License.Contains(model.License)) || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + /// + /// Добавление элемента + /// + /// + /// + public TransportViewModel? Insert(TransportBindingModel model) + { + using var context = new RouteGuideDatabase(); + var newTransport = Transport.Create(context, model); + if (newTransport == null) + { + return null; + } + + context.Transport.Add(newTransport); + context.SaveChanges(); + return newTransport.GetViewModel; + } + + /// + /// Редактирование элемента + /// + /// + /// + public TransportViewModel? Update(TransportBindingModel model) + { + using var context = new RouteGuideDatabase(); + var transport = context.Transport.FirstOrDefault(x => x.Id == model.Id); + if (transport == null) + { + return null; + } + + transport.Update(model); + context.SaveChanges(); + return transport.GetViewModel; + } + + /// + /// Удаление элемента + /// + /// + /// + public TransportViewModel? Delete(TransportBindingModel model) + { + using var context = new RouteGuideDatabase(); + var transport = context.Transport.FirstOrDefault(x => x.Id == model.Id); + if (transport == null) + { + return null; + } + + context.Transport.Remove(transport); + context.SaveChanges(); + return transport.GetViewModel; + } + } +} diff --git a/RouteGuide/RouteGuideDatabaseImplement/Models/Driver.cs b/RouteGuide/RouteGuideDatabaseImplement/Models/Driver.cs new file mode 100644 index 0000000..2c1df73 --- /dev/null +++ b/RouteGuide/RouteGuideDatabaseImplement/Models/Driver.cs @@ -0,0 +1,109 @@ +using RouteGuideContracts.BindingModels; +using RouteGuideContracts.ViewModels; +using RouteGuideDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RouteGuideDatabaseImplement.Models +{ + /// + /// Сущность "Водитель" + /// + public class Driver : IDriverModel + { + /// + /// Идентификатор + /// + public int Id { get; private set; } + + /// + /// ФИО водителя + /// + [Required] + public string FullName { get; private set; } = string.Empty; + + /// + /// Номер телефона водителя + /// + [Required] + public string Phone { get; private set; } = string.Empty; + + /// + /// Опыт работы водителя + /// + public int? Experience { get; private set; } + + /// + /// Создание модели + /// + /// + /// + public static Driver? Create(DriverBindingModel model) + { + if (model == null) + { + return null; + } + + return new Driver() + { + Id = model.Id, + FullName = model.FullName, + Phone = model.Phone, + Experience = model.Experience + }; + } + + /// + /// Создание модели + /// + /// + /// + public static Driver? Create(DriverViewModel model) + { + if (model == null) + { + return null; + } + + return new Driver() + { + Id = model.Id, + FullName = model.FullName, + Phone = model.Phone, + Experience = model.Experience + }; + } + + /// + /// Изменение модели + /// + /// + public void Update(DriverBindingModel model) + { + if (model == null) + { + return; + } + + FullName = model.FullName; + Phone = model.Phone; + Experience = model.Experience; + } + + /// + /// Получение модели + /// + public DriverViewModel GetViewModel => new() + { + Id = Id, + FullName = FullName, + Phone = Phone, + Experience = Experience + }; + } +} diff --git a/RouteGuide/RouteGuideDatabaseImplement/Models/Route.cs b/RouteGuide/RouteGuideDatabaseImplement/Models/Route.cs new file mode 100644 index 0000000..db6ce88 --- /dev/null +++ b/RouteGuide/RouteGuideDatabaseImplement/Models/Route.cs @@ -0,0 +1,154 @@ +using RouteGuideContracts.BindingModels; +using RouteGuideContracts.ViewModels; +using RouteGuideDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; + +namespace RouteGuideDatabaseImplement.Models +{ + /// + /// Сущность "Маршрут" + /// + public class Route : IRouteModel + { + /// + /// Идентификатор + /// + public int Id { get; private set; } + + /// + /// Название маршрута + /// + [Required] + public string Name { get; private set; } = string.Empty; + + /// + /// Идентификатор транспорта + /// + [ForeignKey("TransportId")] + public int TransportId { get; private set; } + + /// + /// Сущность "Транспорт" + /// + public virtual Transport Transport { get; private set; } = new(); + + /// + /// Коллекция остановок маршрута + /// + private Dictionary? _routeStops = null; + + /// + /// Коллекция остановок маршрута + /// + [NotMapped] + public Dictionary RouteStops + { + get + { + if (_routeStops == null) + { + _routeStops = Stops + .ToDictionary(recRS => recRS.StopId, recRS => (recRS.Stop as IStopModel, recRS.Number)); + } + return _routeStops; + } + } + + /// + /// Связь с классом связи маршрутов и остановок + /// + [ForeignKey("RouteId")] + public virtual List Stops { get; set; } = new(); + + /// + /// Созданме модели + /// + /// + /// + /// + public static Route Create(RouteGuideDatabase context, RouteBindingModel model) + { + return new Route() + { + Id = model.Id, + Name = model.Name, + TransportId = model.TransportId, + Transport = context.Transport + .FirstOrDefault(x => x.Id == model.TransportId), + Stops = model.RouteStops.Select(x => new RouteStop + { + Stop = context.Stops.First(y => y.Id == x.Key), + Number = x.Value.Item2 + }).ToList() + }; + } + + /// + /// Изменение модели + /// + /// + public void Update(RouteBindingModel model) + { + if (model == null) + { + return; + } + + Name = model.Name; + } + + /// + /// Получение модели + /// + public RouteViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + TransportId = TransportId, + RouteStops = RouteStops + }; + + /// + /// Обновление списка связей + /// + /// + /// + public void UpdateStops(RouteGuideDatabase context, RouteBindingModel model) + { + var routeStops = context.RouteStops.Where(rec => rec.RouteId == model.Id).ToList(); + if (routeStops != null && routeStops.Count > 0) + { + // Удаление остановок, которых нет в маршруте + context.RouteStops.RemoveRange(routeStops.Where(rec => !model.RouteStops.ContainsKey(rec.StopId))); + context.SaveChanges(); + // Обновление количества у существующих записей + foreach (var updateStop in routeStops) + { + updateStop.Number = model.RouteStops[updateStop.StopId].Item2; + model.RouteStops.Remove(updateStop.StopId); + } + context.SaveChanges(); + } + + var route = context.Routes.First(x => x.Id == Id); + foreach (var rs in model.RouteStops) + { + context.RouteStops.Add(new RouteStop + { + Route = route, + Stop = context.Stops.First(x => x.Id == rs.Key), + Number = rs.Value.Item2 + }); + context.SaveChanges(); + } + _routeStops = null; + } + } +} diff --git a/RouteGuide/RouteGuideDatabaseImplement/Models/RouteStop.cs b/RouteGuide/RouteGuideDatabaseImplement/Models/RouteStop.cs new file mode 100644 index 0000000..759b5b3 --- /dev/null +++ b/RouteGuide/RouteGuideDatabaseImplement/Models/RouteStop.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RouteGuideDatabaseImplement.Models +{ + /// + /// Класс связи сущностей "Маршрут" и "Остановка" + /// + public class RouteStop + { + /// + /// Идентификатор + /// + public int Id { get; set; } + + /// + /// Идентификатор маршрута + /// + [Required] + public int RouteId { get; set; } + + /// + /// Сущность "Маршрут" + /// + public virtual Route Route { get; set; } = new(); + + /// + /// Идентификатор остановки + /// + [Required] + public int StopId { get; set; } + + /// + /// Сущность "Остановка" + /// + public virtual Stop Stop { get; set; } = new(); + + /// + /// Номер остановки в маршруте + /// + [Required] + public int Number { get; set; } + } +} diff --git a/RouteGuide/RouteGuideDatabaseImplement/Models/Schedule.cs b/RouteGuide/RouteGuideDatabaseImplement/Models/Schedule.cs new file mode 100644 index 0000000..aa31332 --- /dev/null +++ b/RouteGuide/RouteGuideDatabaseImplement/Models/Schedule.cs @@ -0,0 +1,115 @@ +using RouteGuideContracts.BindingModels; +using RouteGuideContracts.ViewModels; +using RouteGuideDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RouteGuideDatabaseImplement.Models +{ + /// + /// Сущность "Расписание" + /// + public class Schedule : IScheduleModel + { + /// + /// Идентификатор + /// + public int Id { get; private set; } + + /// + /// Дата записи расписания + /// + [Required] + public DateTime Date { get; private set; } = DateTime.Now; + + /// + /// Время отправления + /// + [Required] + public DateTime DepartureTime { get; private set; } = DateTime.MinValue; + + /// + /// Время прибытия + /// + [Required] + public DateTime ArrivalTime { get; private set; } = DateTime.MinValue; + + /// + /// Периодичность движения транспорта + /// + [Required] + public DateTime Frequency { get; private set; } = DateTime.MinValue; + + /// + /// Идентификатор маршрута + /// + [ForeignKey("RouteId")] + public int RouteId { get; private set; } + + /// + /// Сущность "Маршрут" + /// + public virtual Route Route { get; private set; } = new(); + + /// + /// Создание модели + /// + /// + /// + public static Schedule? Create(RouteGuideDatabase context, ScheduleBindingModel model) + { + if (model == null) + { + return null; + } + + return new Schedule() + { + Id = model.Id, + Date = model.Date, + DepartureTime = model.DepartureTime, + ArrivalTime = model.ArrivalTime, + Frequency = model.Frequency, + RouteId = model.RouteId, + Route = context.Routes + .FirstOrDefault(x => x.Id == model.RouteId) + }; + } + + /// + /// Изменение модели + /// + /// + public void Update(ScheduleBindingModel model) + { + if (model == null) + { + return; + } + + Date = model.Date; + DepartureTime = model.DepartureTime; + ArrivalTime = model.ArrivalTime; + Frequency = model.Frequency; + } + + /// + /// Получение модели + /// + public ScheduleViewModel GetViewModel => new() + { + Id = Id, + Date = Date, + DepartureTime = DepartureTime, + ArrivalTime = ArrivalTime, + Frequency = Frequency, + RouteId = RouteId + }; + } +} diff --git a/RouteGuide/RouteGuideDatabaseImplement/Models/Stop.cs b/RouteGuide/RouteGuideDatabaseImplement/Models/Stop.cs new file mode 100644 index 0000000..722b0c7 --- /dev/null +++ b/RouteGuide/RouteGuideDatabaseImplement/Models/Stop.cs @@ -0,0 +1,111 @@ +using RouteGuideContracts.BindingModels; +using RouteGuideContracts.ViewModels; +using RouteGuideDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; + +namespace RouteGuideDatabaseImplement.Models +{ + /// + /// Сущность "Остановка" + /// + public class Stop : IStopModel + { + /// + /// Идентификатор + /// + public int Id { get; private set; } + + /// + /// Название остановки + /// + [Required] + public string Name { get; private set; } = string.Empty; + + /// + /// Название улицы + /// + [Required] + public string Street { get; private set; } = string.Empty; + + /// + /// Номер дома + /// + [Required] + public int Number { get; private set; } + + /// + /// Создание модели + /// + /// + /// + public static Stop? Create(StopBindingModel model) + { + if (model == null) + { + return null; + } + + return new Stop() + { + Id = model.Id, + Name = model.Name, + Street = model.Street, + Number = model.Number + }; + } + + /// + /// Создание модели + /// + /// + /// + public static Stop? Create(StopViewModel model) + { + if (model == null) + { + return null; + } + + return new Stop() + { + Id = model.Id, + Name = model.Name, + Street = model.Street, + Number = model.Number + }; + } + + /// + /// Изменение модели + /// + /// + public void Update(StopBindingModel model) + { + if (model == null) + { + return; + } + + Name = model.Name; + Street = model.Street; + Number = model.Number; + } + + /// + /// Получение модели + /// + public StopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Street = Street, + Number = Number + }; + } +} diff --git a/RouteGuide/RouteGuideDatabaseImplement/Models/Transport.cs b/RouteGuide/RouteGuideDatabaseImplement/Models/Transport.cs new file mode 100644 index 0000000..f40b9bf --- /dev/null +++ b/RouteGuide/RouteGuideDatabaseImplement/Models/Transport.cs @@ -0,0 +1,105 @@ +using RouteGuideContracts.BindingModels; +using RouteGuideContracts.ViewModels; +using RouteGuideDataModels.Enums; +using RouteGuideDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; + +namespace RouteGuideDatabaseImplement.Models +{ + /// + /// Сущность "Транспорт" + /// + public class Transport : ITransportModel + { + /// + /// Идентификатор + /// + public int Id { get; private set; } + + /// + /// Номерной знак + /// + [Required] + public string License { get; private set; } = string.Empty; + + /// + /// Тип транспортного средства + /// + [Required] + public TransportType Type { get; private set; } = TransportType.Автобус; + + /// + /// Вместимость (количество пассажиров) + /// + [Required] + public int Capacity { get; private set; } + + /// + /// Идентификатор водителя + /// + [ForeignKey("DriverId")] + public int DriverId { get; private set; } + + /// + /// Сущность "Водитель" + /// + public virtual Driver Driver { get; private set; } = new(); + + /// + /// Создание модели + /// + /// + /// + public static Transport? Create(RouteGuideDatabase context, TransportBindingModel model) + { + if (model == null) + { + return null; + } + + return new Transport() + { + Id = model.Id, + License = model.License, + Type = model.Type, + Capacity = model.Capacity, + DriverId = model.DriverId, + Driver = context.Drivers + .FirstOrDefault(x => x.Id == model.DriverId) + }; + } + + /// + /// Изменение модели + /// + /// + public void Update(TransportBindingModel model) + { + if (model == null) + { + return; + } + + License = model.License; + Capacity = model.Capacity; + } + + /// + /// Получение модели + /// + public TransportViewModel GetViewModel => new() + { + Id = Id, + License = License, + Type = Type, + DriverId = DriverId + }; + } +} diff --git a/RouteGuide/RouteGuideDatabaseImplement/RouteGuideDatabase.cs b/RouteGuide/RouteGuideDatabaseImplement/RouteGuideDatabase.cs new file mode 100644 index 0000000..d1d4841 --- /dev/null +++ b/RouteGuide/RouteGuideDatabaseImplement/RouteGuideDatabase.cs @@ -0,0 +1,64 @@ +using Microsoft.EntityFrameworkCore; +using RouteGuideDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RouteGuideDatabaseImplement +{ + /// + /// Класс для взаимодействия с базой данных + /// + public class RouteGuideDatabase : DbContext + { + /// + /// Параметры подключения к базе данных + /// + private string _dbConnectionString = "Host=192.168.0.108;Port=5432;Database=RouteGuide;Username=postgres;Password=2004"; + + /// + /// Подключение к базе данных + /// + /// + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql(_dbConnectionString); + } + base.OnConfiguring(optionsBuilder); + } + + /// + /// Таблица "Водители" + /// + public virtual DbSet Drivers { get; set; } + + /// + /// Таблица "Транспорт" + /// + public virtual DbSet Transport { get; set; } + + /// + /// Таблица "Маршруты" + /// + public virtual DbSet Routes { get; set; } + + /// + /// Таблица "Остановки" + /// + public virtual DbSet Stops { get; set; } + + /// + /// Связь для сущностей "Маршруты" и "Остановки" + /// + public virtual DbSet RouteStops { get; set; } + + /// + /// Таблица "Расписания" + /// + public virtual DbSet Schedules { get; set; } + } +} diff --git a/RouteGuide/RouteGuideDatabaseImplement/RouteGuideDatabaseImplement.csproj b/RouteGuide/RouteGuideDatabaseImplement/RouteGuideDatabaseImplement.csproj new file mode 100644 index 0000000..9884a37 --- /dev/null +++ b/RouteGuide/RouteGuideDatabaseImplement/RouteGuideDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + +