From 231063ed164559b8b7dbf7b33e94fc62291e579b Mon Sep 17 00:00:00 2001 From: dasha Date: Wed, 29 Mar 2023 22:56:00 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=A0=D0=A3=D0=94=D0=AB=D0=AB=20=D0=A0?= =?UTF-8?q?=D0=90=D0=91=D0=9E=D0=A2=D0=90=D0=AE=D0=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BeautySaloon/BeautySaloon.sln | 6 ++--- .../BeautySaloonBusinessLogic.csproj | 8 ++++++ .../BeautySaloonContracts.csproj | 8 ++++++ .../BeautySaloonDataModels.csproj | 8 ++++++ .../Implements/OrderStorage.cs | 27 ++++++++++++++++--- .../NewdbContext.cs | 26 ++++++++---------- .../BeautySaloonDatabaseImplement/Order.cs | 17 +++++------- .../BeautySaloonDatabaseImplement/Service.cs | 3 +-- .../BeautySaloonView/BeautySaloonView.csproj | 5 ++++ BeautySaloon/BeautySaloonView/FormEmployee.cs | 1 - 10 files changed, 74 insertions(+), 35 deletions(-) diff --git a/BeautySaloon/BeautySaloon.sln b/BeautySaloon/BeautySaloon.sln index aa3a7cb..07a84e4 100644 --- a/BeautySaloon/BeautySaloon.sln +++ b/BeautySaloon/BeautySaloon.sln @@ -7,11 +7,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BeautySaloonView", "BeautyS EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BeautySaloonDataModels", "BeautySaloonDataModels\BeautySaloonDataModels.csproj", "{A83AC6FD-F6BD-4BD2-AC69-3BB5BA30FF7D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeautySaloonContracts", "BeautySaloonContracts\BeautySaloonContracts.csproj", "{7494D3AF-2581-4128-9183-BB87A9DC4B10}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BeautySaloonContracts", "BeautySaloonContracts\BeautySaloonContracts.csproj", "{7494D3AF-2581-4128-9183-BB87A9DC4B10}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeautySaloonBusinessLogic", "BeautySaloonBusinessLogic\BeautySaloonBusinessLogic.csproj", "{E43A1394-BC9A-430B-B984-BCCD828FFF45}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BeautySaloonBusinessLogic", "BeautySaloonBusinessLogic\BeautySaloonBusinessLogic.csproj", "{E43A1394-BC9A-430B-B984-BCCD828FFF45}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeautySaloonDatabaseImplement", "BeautySaloonDatabaseImplement\BeautySaloonDatabaseImplement.csproj", "{BB7AD640-FF4A-415B-A09B-BB802D64CE27}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BeautySaloonDatabaseImplement", "BeautySaloonDatabaseImplement\BeautySaloonDatabaseImplement.csproj", "{BB7AD640-FF4A-415B-A09B-BB802D64CE27}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/BeautySaloon/BeautySaloonBusinessLogic/BeautySaloonBusinessLogic.csproj b/BeautySaloon/BeautySaloonBusinessLogic/BeautySaloonBusinessLogic.csproj index e696bb1..e054a8c 100644 --- a/BeautySaloon/BeautySaloonBusinessLogic/BeautySaloonBusinessLogic.csproj +++ b/BeautySaloon/BeautySaloonBusinessLogic/BeautySaloonBusinessLogic.csproj @@ -6,6 +6,14 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/BeautySaloon/BeautySaloonContracts/BeautySaloonContracts.csproj b/BeautySaloon/BeautySaloonContracts/BeautySaloonContracts.csproj index b46dcf3..c14a1b2 100644 --- a/BeautySaloon/BeautySaloonContracts/BeautySaloonContracts.csproj +++ b/BeautySaloon/BeautySaloonContracts/BeautySaloonContracts.csproj @@ -6,6 +6,14 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/BeautySaloon/BeautySaloonDataModels/BeautySaloonDataModels.csproj b/BeautySaloon/BeautySaloonDataModels/BeautySaloonDataModels.csproj index 27ac386..1bc0028 100644 --- a/BeautySaloon/BeautySaloonDataModels/BeautySaloonDataModels.csproj +++ b/BeautySaloon/BeautySaloonDataModels/BeautySaloonDataModels.csproj @@ -6,4 +6,12 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/BeautySaloon/BeautySaloonDatabaseImplement/Implements/OrderStorage.cs b/BeautySaloon/BeautySaloonDatabaseImplement/Implements/OrderStorage.cs index 3cc04b2..1b61d07 100644 --- a/BeautySaloon/BeautySaloonDatabaseImplement/Implements/OrderStorage.cs +++ b/BeautySaloon/BeautySaloonDatabaseImplement/Implements/OrderStorage.cs @@ -2,6 +2,7 @@ using BeautySaloonContracts.SearchModels; using BeautySaloonContracts.StoragesContracts; using BeautySaloonContracts.ViewModels; +using Microsoft.EntityFrameworkCore; namespace BeautySaloonDatabaseImplement.Implements { @@ -11,6 +12,8 @@ namespace BeautySaloonDatabaseImplement.Implements { using var context = new NewdbContext(); var element = context.Orders + .Include(x => x.Employee) + .Include(x => x.Client) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { @@ -26,6 +29,8 @@ namespace BeautySaloonDatabaseImplement.Implements using var context = new NewdbContext(); if (model.Id.HasValue) return context.Orders + .Include(x => x.Employee) + .Include(x => x.Client) .FirstOrDefault(x => x.Id == model.Id)? .GetViewModel; return null; @@ -36,16 +41,22 @@ namespace BeautySaloonDatabaseImplement.Implements using var context = new NewdbContext(); if (model.ClientId.HasValue) return context.Orders + .Include(x => x.Employee) + .Include(x => x.Client) .Where(x => x.ClientId == model.ClientId) .Select(x => x.GetViewModel) .ToList(); if (model.EmployeeId.HasValue) return context.Orders + .Include(x => x.Employee) + .Include(x => x.Client) .Where(x => x.EmployeeId == model.EmployeeId) .Select(x => x.GetViewModel) .ToList(); if (model.Date.HasValue) return context.Orders + .Include(x => x.Employee) + .Include(x => x.Client) .Where(x => x.Date == model.Date) .Select(x => x.GetViewModel) .ToList(); @@ -56,14 +67,18 @@ namespace BeautySaloonDatabaseImplement.Implements { using var context = new NewdbContext(); return context.Orders - .Select(x => x.GetViewModel) - .ToList(); + .Include(x => x.Employee) + .Include(x => x.Client) + .Select(x => x.GetViewModel) + .ToList(); } public OrderViewModel? Insert(OrderBindingModel model) { using var context = new NewdbContext(); model.Id = context.Orders + .Include(x => x.Employee) + .Include(x => x.Client) .Count() > 0 ? context.Orders.Max(x => x.Id) + 1 : 1; var newOrder = Order.Create(context, model); if (newOrder == null) @@ -72,7 +87,10 @@ namespace BeautySaloonDatabaseImplement.Implements } context.Orders.Add(newOrder); context.SaveChanges(); - return newOrder.GetViewModel; + return context.Orders + .Include(x => x.Employee) + .Include(x => x.Client).FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) @@ -81,7 +99,8 @@ namespace BeautySaloonDatabaseImplement.Implements using var transaction = context.Database.BeginTransaction(); try { - var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + var order = context.Orders.Include(x => x.Employee) + .Include(x => x.Client).FirstOrDefault(rec => rec.Id == model.Id); if (order == null) { return null; diff --git a/BeautySaloon/BeautySaloonDatabaseImplement/NewdbContext.cs b/BeautySaloon/BeautySaloonDatabaseImplement/NewdbContext.cs index c1e3ae8..def7f31 100644 --- a/BeautySaloon/BeautySaloonDatabaseImplement/NewdbContext.cs +++ b/BeautySaloon/BeautySaloonDatabaseImplement/NewdbContext.cs @@ -174,10 +174,6 @@ public partial class NewdbContext : DbContext entity.ToTable("service_order", tb => tb.HasComment("Сущность-связь услуги с заказом")); - entity.HasIndex(e => e.OrderId, "service_order_orderid_key").IsUnique(); - - entity.HasIndex(e => e.ServiceId, "service_order_serviceid_key").IsUnique(); - entity.Property(e => e.ServiceId) .HasComment("Составной первичный ключ: идентификатор услуги") .HasColumnName("service_id"); @@ -194,22 +190,22 @@ public partial class NewdbContext : DbContext entity.HasOne(d => d.Employee).WithMany(p => p.ServiceOrders) .HasForeignKey(d => d.EmployeeId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("employee_fk"); + .OnDelete(DeleteBehavior.Restrict) + .HasConstraintName("service_order_employee_id_fkey"); - entity.HasOne(d => d.Order).WithOne(p => p.ServiceOrder) - .HasForeignKey(d => d.OrderId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("order_fk"); + entity.HasOne(d => d.Order).WithMany(p => p.ServiceOrders) + .HasForeignKey(d => d.OrderId) + .OnDelete(DeleteBehavior.Restrict) + .HasConstraintName("service_order_order_id_fkey"); - entity.HasOne(d => d.Service).WithOne(p => p.ServiceOrder) - .HasForeignKey(d => d.ServiceId) - .OnDelete(DeleteBehavior.ClientSetNull) - .HasConstraintName("service_fk"); + entity.HasOne(d => d.Service).WithMany(p => p.ServiceOrders) + .HasForeignKey(d => d.ServiceId) + .OnDelete(DeleteBehavior.Restrict) + .HasConstraintName("service_order_service_id_fkey"); }); OnModelCreatingPartial(modelBuilder); } partial void OnModelCreatingPartial(ModelBuilder modelBuilder); -} +} \ No newline at end of file diff --git a/BeautySaloon/BeautySaloonDatabaseImplement/Order.cs b/BeautySaloon/BeautySaloonDatabaseImplement/Order.cs index d1cf920..579b882 100644 --- a/BeautySaloon/BeautySaloonDatabaseImplement/Order.cs +++ b/BeautySaloon/BeautySaloonDatabaseImplement/Order.cs @@ -1,6 +1,7 @@ using BeautySaloonContracts.BindingModels; using BeautySaloonContracts.ViewModels; using BeautySaloonDataModels; +using System.ComponentModel.DataAnnotations.Schema; namespace BeautySaloonDatabaseImplement; @@ -38,23 +39,19 @@ public partial class Order : IOrderModel public virtual Employee Employee { get; set; } = null!; - public virtual ServiceOrder? ServiceOrder { get; set; } - + public virtual List ServiceOrders { get; set; } = new List(); public Dictionary _orderServices = null; - - public virtual List Services { get; set; } = new(); - + public Dictionary OrderServices { get { if (_orderServices == null) { - _orderServices = Services - .ToDictionary(serviceOrd => serviceOrd.ServiceId, - serviceOrd => (serviceOrd.Date, serviceOrd.Service as IServiceModel, - serviceOrd.EmployeeId)); + _orderServices = ServiceOrders + .ToDictionary(recPC => recPC.ServiceId, + recPC => (recPC.Date, recPC.Service as IServiceModel, recPC.EmployeeId)); } return _orderServices; } @@ -73,7 +70,7 @@ public partial class Order : IOrderModel Sum = model.Sum, ClientId = model.ClientId, EmployeeId = model.EmployeeId, - Services = model.OrderServices.Select(x => new ServiceOrder + ServiceOrders = model.OrderServices.Select(x => new ServiceOrder { Service = context.Services.First(y => y.Id == x.Key), Date = x.Value.Item1, diff --git a/BeautySaloon/BeautySaloonDatabaseImplement/Service.cs b/BeautySaloon/BeautySaloonDatabaseImplement/Service.cs index c8a8a7b..36d5092 100644 --- a/BeautySaloon/BeautySaloonDatabaseImplement/Service.cs +++ b/BeautySaloon/BeautySaloonDatabaseImplement/Service.cs @@ -25,8 +25,7 @@ public partial class Service : IServiceModel /// Цена /// public decimal Price { get; set; } - - public virtual ServiceOrder? ServiceOrder { get; set; } + public virtual ICollection ServiceOrders { get; } = new List(); public static Service Create(ServiceBindingModel model) { diff --git a/BeautySaloon/BeautySaloonView/BeautySaloonView.csproj b/BeautySaloon/BeautySaloonView/BeautySaloonView.csproj index 2fdce82..16107c0 100644 --- a/BeautySaloon/BeautySaloonView/BeautySaloonView.csproj +++ b/BeautySaloon/BeautySaloonView/BeautySaloonView.csproj @@ -13,7 +13,12 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/BeautySaloon/BeautySaloonView/FormEmployee.cs b/BeautySaloon/BeautySaloonView/FormEmployee.cs index 76cb44b..c090b97 100644 --- a/BeautySaloon/BeautySaloonView/FormEmployee.cs +++ b/BeautySaloon/BeautySaloonView/FormEmployee.cs @@ -1,7 +1,6 @@ using BeautySaloonContracts.BindingModels; using BeautySaloonContracts.BusinessLogicsContracts; using BeautySaloonContracts.SearchModels; -using Microsoft.IdentityModel.Tokens; namespace BeautySaloonView {