From 845f442d9595ee15f9a9fc342395457241e8ed88 Mon Sep 17 00:00:00 2001 From: Leozillo73 <76777036+Leozillo73@users.noreply.github.com> Date: Sat, 27 Apr 2024 21:41:07 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B5=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/RouteBindingModel.cs | 4 +- .../SearchModels/RouteSearchModel.cs | 3 +- .../StorageContracts/IClientStorage.cs | 6 +- .../StorageContracts/IRouteStorage.cs | 6 +- .../ZooContracts/ViewModels/RouteViewModel.cs | 10 +- .../Implements/ClientStorage.cs | 84 +++++++++++++ .../Implements/RouteStorage.cs | 116 ++++++++++++++++++ .../ZooDataBaseImplement/Models/Preserve.cs | 6 +- .../ZooDataBaseImplement/Models/Route.cs | 31 ++++- .../ZooDataModels/Enums/RouteStatus.cs | 6 +- .../ZooDataModels/Models/IRouteModel.cs | 2 +- 11 files changed, 253 insertions(+), 21 deletions(-) create mode 100644 git/JurasicZoo/ZooDataBaseImplement/Implements/ClientStorage.cs create mode 100644 git/JurasicZoo/ZooDataBaseImplement/Implements/RouteStorage.cs diff --git a/git/JurasicZoo/ZooContracts/BindingModels/RouteBindingModel.cs b/git/JurasicZoo/ZooContracts/BindingModels/RouteBindingModel.cs index 017f53b..8c3b466 100644 --- a/git/JurasicZoo/ZooContracts/BindingModels/RouteBindingModel.cs +++ b/git/JurasicZoo/ZooContracts/BindingModels/RouteBindingModel.cs @@ -14,10 +14,10 @@ namespace ZooContracts.BindingModels public int ClientId { get; set; } public string RouteName { get; set; } = string.Empty; public double RoutePrice { get; set; } - public RouteStatus Status { get; set; } = RouteStatus.Неизвестен; + public RouteStatus Status { get; set; } = RouteStatus.Неоплачен; public DateTime DateStart { get; set; } public DateTime DateFinish { get; set; } - public Dictionary Preserves + public Dictionary RoutePreserves { get; set; diff --git a/git/JurasicZoo/ZooContracts/SearchModels/RouteSearchModel.cs b/git/JurasicZoo/ZooContracts/SearchModels/RouteSearchModel.cs index abb4ff3..668b57d 100644 --- a/git/JurasicZoo/ZooContracts/SearchModels/RouteSearchModel.cs +++ b/git/JurasicZoo/ZooContracts/SearchModels/RouteSearchModel.cs @@ -10,9 +10,10 @@ namespace ZooContracts.SearchModels public class RouteSearchModel { public int? Id { get; set; } + public int? ClientId { get; set; } public string? RouteName { get; set; } = string.Empty; public double? RoutePrice { get; set; } - public RouteStatus Status { get; set; } = RouteStatus.Неизвестен; + public RouteStatus Status { get; set; } = RouteStatus.Неоплачен; public DateTime? DateStart { get; set; } public DateTime? DateFinish { get; set; } } diff --git a/git/JurasicZoo/ZooContracts/StorageContracts/IClientStorage.cs b/git/JurasicZoo/ZooContracts/StorageContracts/IClientStorage.cs index 85658e3..1f11a86 100644 --- a/git/JurasicZoo/ZooContracts/StorageContracts/IClientStorage.cs +++ b/git/JurasicZoo/ZooContracts/StorageContracts/IClientStorage.cs @@ -14,8 +14,8 @@ namespace ZooContracts.StorageContracts List GetFullList(); List GetFilteredList(ClientSearchModel model); ClientViewModel? GetElement(ClientSearchModel model); - ClientViewModel? Insert(ClientSearchModel model); - ClientViewModel? Update(ClientSearchModel model); - ClientViewModel? Delete(ClientSearchModel model); + ClientViewModel? Insert(ClientBindingModel model); + ClientViewModel? Update(ClientBindingModel model); + ClientViewModel? Delete(ClientBindingModel model); } } diff --git a/git/JurasicZoo/ZooContracts/StorageContracts/IRouteStorage.cs b/git/JurasicZoo/ZooContracts/StorageContracts/IRouteStorage.cs index 4420727..925a0f1 100644 --- a/git/JurasicZoo/ZooContracts/StorageContracts/IRouteStorage.cs +++ b/git/JurasicZoo/ZooContracts/StorageContracts/IRouteStorage.cs @@ -14,8 +14,8 @@ namespace ZooContracts.StorageContracts List GetFullList(); List GetFilteredList(RouteSearchModel model); RouteViewModel? GetElement(RouteSearchModel model); - RouteViewModel? Insert(RouteSearchModel model); - RouteViewModel? Update(RouteSearchModel model); - RouteViewModel? Delete(RouteSearchModel model); + RouteViewModel? Insert(RouteBindingModel model); + RouteViewModel? Update(RouteBindingModel model); + RouteViewModel? Delete(RouteBindingModel model); } } diff --git a/git/JurasicZoo/ZooContracts/ViewModels/RouteViewModel.cs b/git/JurasicZoo/ZooContracts/ViewModels/RouteViewModel.cs index 9992e9e..1ab8dbf 100644 --- a/git/JurasicZoo/ZooContracts/ViewModels/RouteViewModel.cs +++ b/git/JurasicZoo/ZooContracts/ViewModels/RouteViewModel.cs @@ -12,16 +12,22 @@ namespace ZooContracts.ViewModels { public class RouteViewModel { + [DisplayName("Номер")] public int Id { get; set; } + public int ClientId { get; set; } + [DisplayName("ФИО клиента")] + public string ClientFIO { get; set; } = string.Empty; + [DisplayName("Название маршрута")] public string RouteName { get; set; } = string.Empty; [DisplayName("Стоимость маршрута")] public double RoutePrice { get; set; } [DisplayName("Статус")] - public RouteStatus Status { get; set; } = RouteStatus.Неизвестен; + public RouteStatus Status { get; set; } = RouteStatus.Неоплачен; [DisplayName("Дата начала")] - public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime DateStart { get; set; } [DisplayName("Дата окончания")] + public DateTime DateFinish { get; set; } public Dictionary Preserves { get; diff --git a/git/JurasicZoo/ZooDataBaseImplement/Implements/ClientStorage.cs b/git/JurasicZoo/ZooDataBaseImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..4af68ca --- /dev/null +++ b/git/JurasicZoo/ZooDataBaseImplement/Implements/ClientStorage.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ZooContracts.BindingModels; +using ZooContracts.SearchModels; +using ZooContracts.StorageContracts; +using ZooContracts.ViewModels; +using ZooDataBaseImplement.Models; + +namespace ZooDataBaseImplement.Implements +{ + public class ClientStorage : IClientStorage + { + public List GetFullList() + { + using var context = new ZooDatabase(); + return context.Clients.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.ClientFIO)) + { + return new(); + } + using var context = new ZooDatabase(); + return context.Clients.Where(x => x.ClientFIO.Contains(model.ClientFIO)).Select(x => x.GetViewModel).ToList(); + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue) + { + return null; + } + using var context = new ZooDatabase(); + return context.Clients.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Email) && x.EMail == model.Email && !string.IsNullOrEmpty(model.Password) && x.Password == model.Password) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + var newComponent = Client.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new ZooDatabase(); + context.Clients.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new ZooDatabase(); + var component = context.Clients.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + using var context = new ZooDatabase(); + var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Clients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/git/JurasicZoo/ZooDataBaseImplement/Implements/RouteStorage.cs b/git/JurasicZoo/ZooDataBaseImplement/Implements/RouteStorage.cs new file mode 100644 index 0000000..8c49ef9 --- /dev/null +++ b/git/JurasicZoo/ZooDataBaseImplement/Implements/RouteStorage.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ZooContracts.StorageContracts; +using ZooContracts.ViewModels; +using Microsoft.EntityFrameworkCore; +using ZooContracts.SearchModels; +using ZooContracts.BindingModels; +using System.Threading.Channels; +using ZooDataBaseImplement.Models; + +namespace ZooDataBaseImplement.Implements +{ + public class RouteStorage : IRouteStorage + { + public List GetFullList() + { + using var context = new ZooDatabase(); + return context.Routes + .Include(x => x.Preserves) + .ThenInclude(x => x.Preserve) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(RouteSearchModel model) + { + if (string.IsNullOrEmpty(model.RouteName)) + { + return new(); + } + using var context = new ZooDatabase(); + return context.Routes + .Include(x => x.Preserves) + .ThenInclude(x => x.Preserve) + .Where(x => x.RouteName.Contains(model.RouteName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public RouteViewModel? GetElement(RouteSearchModel model) + { + if (string.IsNullOrEmpty(model.RouteName) && + !model.Id.HasValue) + { + return null; + } + using var context = new ZooDatabase(); + return context.Routes + .Include(x => x.Preserves) + .ThenInclude(x => x.Preserve) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.RouteName) && + x.RouteName == model.RouteName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + + public RouteViewModel? Insert(RouteBindingModel model) + { + using var context = new ZooDatabase(); + var newCanned = Route.Create(context, model); + if (newCanned == null) + { + return null; + } + context.Routes.Add(newCanned); + context.SaveChanges(); + return newCanned.GetViewModel; + } + + public RouteViewModel? Update(RouteBindingModel model) + { + using var context = new ZooDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var route = context.Routes.FirstOrDefault(rec => + rec.Id == model.Id); + if (route == null) + { + return null; + } + route.Update(model); + context.SaveChanges(); + transaction.Commit(); + return route.GetViewModel; + } + + catch + { + transaction.Rollback(); + throw; + } + } + + public RouteViewModel? Delete(RouteBindingModel model) + { + using var context = new ZooDatabase(); + var element = context.Routes + .Include(x => x.Preserves) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Routes.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/git/JurasicZoo/ZooDataBaseImplement/Models/Preserve.cs b/git/JurasicZoo/ZooDataBaseImplement/Models/Preserve.cs index 6af4a0b..a051819 100644 --- a/git/JurasicZoo/ZooDataBaseImplement/Models/Preserve.cs +++ b/git/JurasicZoo/ZooDataBaseImplement/Models/Preserve.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -17,7 +18,10 @@ namespace ZooDataBaseImplement.Models public string PreserveName { get; set; } = string.Empty; [Required] public double PreservePrice { get; set; } - public static Preserve? Create(PreserveBindingModel model) + [ForeignKey("PreserveId")] + public virtual List RoutePreserves{ get; set; } = + new(); + public static Preserve? Create(PreserveBindingModel model) { if (model == null) { diff --git a/git/JurasicZoo/ZooDataBaseImplement/Models/Route.cs b/git/JurasicZoo/ZooDataBaseImplement/Models/Route.cs index d350638..7a94946 100644 --- a/git/JurasicZoo/ZooDataBaseImplement/Models/Route.cs +++ b/git/JurasicZoo/ZooDataBaseImplement/Models/Route.cs @@ -19,9 +19,9 @@ namespace ZooDataBaseImplement.Models [Required] public double RoutePrice { get; set; } [Required] - public RouteStatus Status { get; private set; } = RouteStatus.Неизвестен; + public RouteStatus Status { get; private set; } = RouteStatus.Неоплачен; public DateTime DateStart { get; private set; } - public DateTime? DateFinish { get; private set; } + public DateTime DateFinish { get; private set; } private Dictionary? _routepreserves = null; @@ -41,6 +41,7 @@ namespace ZooDataBaseImplement.Models } [ForeignKey("PreserveID")] public virtual List Preserves { get; set; } = new(); + public static Route Create(ZooDatabase context, RouteBindingModel model) { return new Route() @@ -48,19 +49,39 @@ namespace ZooDataBaseImplement.Models Id = model.Id, ClientId = model.ClientId, Client = context.Clients.First(x => x.Id == model.ClientId), - + RouteName = model.RouteName, + RoutePrice = model.RoutePrice, + Status = model.Status, + DateStart = model.DateStart, + DateFinish = model.DateFinish, + Preserves = model.RoutePreserves.Select(x => new RoutePreserve + { + Preserve = context.Preserves.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() }; } - public void Update(PreserveBindingModel model) + public void Update(RouteBindingModel model) { if (model == null) { return; } + Status = model.Status; + DateStart = model.DateStart; + DateFinish = model.DateFinish; } - public PreserveViewModel GetViewModel => new() + public RouteViewModel GetViewModel => new() { + Id = Id, + ClientId = ClientId, + ClientFIO = Client.ClientFIO, + RouteName = RouteName, + RoutePrice = RoutePrice, + Status = Status, + DateStart= DateStart, + DateFinish=DateFinish, }; } } diff --git a/git/JurasicZoo/ZooDataModels/Enums/RouteStatus.cs b/git/JurasicZoo/ZooDataModels/Enums/RouteStatus.cs index dd50ba2..12a1c95 100644 --- a/git/JurasicZoo/ZooDataModels/Enums/RouteStatus.cs +++ b/git/JurasicZoo/ZooDataModels/Enums/RouteStatus.cs @@ -8,8 +8,8 @@ namespace ZooDataModels.Enums { public enum RouteStatus { - Неизвестен = -1, - Принят = 0, - Завершен =1 + Неоплачен = -1, + Неполностью = 0, + Оплачен = 1 } } diff --git a/git/JurasicZoo/ZooDataModels/Models/IRouteModel.cs b/git/JurasicZoo/ZooDataModels/Models/IRouteModel.cs index 69435e9..5bc8d7f 100644 --- a/git/JurasicZoo/ZooDataModels/Models/IRouteModel.cs +++ b/git/JurasicZoo/ZooDataModels/Models/IRouteModel.cs @@ -15,6 +15,6 @@ namespace ZooDataModels.Models RouteStatus Status { get; } DateTime DateStart { get; } DateTime DateFinish { get; } - Dictionary Preserves{ get; } + Dictionary RoutePreserves{ get; } } }