From 332c58bda5595ae7e4c29b1d24ef0d6b1f3e31e5 Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Tue, 2 Apr 2024 22:00:25 +0400 Subject: [PATCH] fix DataBaseImplement/Models --- .../Implements/ComponentStorage.cs | 1 + .../Implements/OrderStorage.cs | 22 +++++++++---------- .../Implements/RepairStorage.cs | 4 ++-- AutoWorkshopDatabaseImplement/Models/Order.cs | 14 ++++++------ .../Models/Repair.cs | 3 ++- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/AutoWorkshopDatabaseImplement/Implements/ComponentStorage.cs b/AutoWorkshopDatabaseImplement/Implements/ComponentStorage.cs index 3436c8a..4bf7dec 100644 --- a/AutoWorkshopDatabaseImplement/Implements/ComponentStorage.cs +++ b/AutoWorkshopDatabaseImplement/Implements/ComponentStorage.cs @@ -36,6 +36,7 @@ namespace AutoWorkshopDatabaseImplement.Implements .FirstOrDefault(x => (!string.IsNullOrEmpty(Model.ComponentName) && x.ComponentName == Model.ComponentName) || (Model.Id.HasValue && x.Id == Model.Id))?.GetViewModel; } + public ComponentViewModel? Insert(ComponentBindingModel Model) { var NewComponent = Component.Create(Model); diff --git a/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs b/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs index fb14da0..b42ccd7 100644 --- a/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs +++ b/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -36,31 +36,31 @@ namespace AutoWorkshopDatabaseImplement.Implements public OrderViewModel? GetElement(OrderSearchModel Model) { if (!Model.Id.HasValue) - return new(); + return null; using var Context = new AutoWorkshopDatabase(); return Context.Orders .Include(x => x.Repair) - .FirstOrDefault(x => x.Id == Model.Id) - ?.GetViewModel; + .FirstOrDefault(x => Model.Id.HasValue && x.Id == Model.Id)? + .GetViewModel; } public OrderViewModel? Insert(OrderBindingModel Model) - { - using var Context = new AutoWorkshopDatabase(); - + { if (Model == null) - return null; - - var NewOrder = Order.Create(Context, Model); + return null; + + var NewOrder = Order.Create(Model); if (NewOrder == null) return null; + using var Context = new AutoWorkshopDatabase(); + Context.Orders.Add(NewOrder); Context.SaveChanges(); - return NewOrder.GetViewModel; + return Context.Orders.Include(x => x.Repair).FirstOrDefault(x => x.Id == NewOrder.Id)?.GetViewModel; } public OrderViewModel? Update(OrderBindingModel Model) @@ -74,7 +74,7 @@ namespace AutoWorkshopDatabaseImplement.Implements Order.Update(Model); Context.SaveChanges(); - return Order.GetViewModel; + return Context.Orders.Include(x => x.Repair).FirstOrDefault(x => x.Id == Model.Id)?.GetViewModel; } public OrderViewModel? Delete(OrderBindingModel Model) diff --git a/AutoWorkshopDatabaseImplement/Implements/RepairStorage.cs b/AutoWorkshopDatabaseImplement/Implements/RepairStorage.cs index 205e538..41be0a8 100644 --- a/AutoWorkshopDatabaseImplement/Implements/RepairStorage.cs +++ b/AutoWorkshopDatabaseImplement/Implements/RepairStorage.cs @@ -47,8 +47,8 @@ namespace AutoWorkshopDatabaseImplement.Implements .Include(x => x.Components) .ThenInclude(x => x.Component) .FirstOrDefault(x => (!string.IsNullOrEmpty(Model.RepairName) && x.RepairName == Model.RepairName) || - (Model.Id.HasValue && x.Id == Model.Id)) - ?.GetViewModel; + (Model.Id.HasValue && x.Id == Model.Id))? + .GetViewModel; } public RepairViewModel? Insert(RepairBindingModel Model) diff --git a/AutoWorkshopDatabaseImplement/Models/Order.cs b/AutoWorkshopDatabaseImplement/Models/Order.cs index e1c8828..e0264a1 100644 --- a/AutoWorkshopDatabaseImplement/Models/Order.cs +++ b/AutoWorkshopDatabaseImplement/Models/Order.cs @@ -1,11 +1,12 @@ using AutoWorkshopContracts.BindingModels; using AutoWorkshopContracts.ViewModels; using AutoWorkshopDataModels.Enums; +using AutoWorkshopDataModels.Models; using System.ComponentModel.DataAnnotations; namespace AutoWorkshopDatabaseImplement.Models { - public class Order + public class Order : IOrderModel { public int Id { get; private set; } @@ -28,8 +29,11 @@ namespace AutoWorkshopDatabaseImplement.Models public DateTime? DateImplement { get; private set; } - public static Order Create(AutoWorkshopDatabase context, OrderBindingModel Model) + public static Order? Create(OrderBindingModel Model) { + if (Model is null) + return null; + return new Order() { Id = Model.Id, @@ -44,14 +48,10 @@ namespace AutoWorkshopDatabaseImplement.Models public void Update(OrderBindingModel Model) { - if (Model == null) + if (Model is null) return; - Id = Model.Id; - RepairId = Model.RepairId; - Sum = Model.Sum; Status = Model.Status; - DateCreate = Model.DateCreate; DateImplement = Model.DateImplement; } diff --git a/AutoWorkshopDatabaseImplement/Models/Repair.cs b/AutoWorkshopDatabaseImplement/Models/Repair.cs index 86fad65..e4707ba 100644 --- a/AutoWorkshopDatabaseImplement/Models/Repair.cs +++ b/AutoWorkshopDatabaseImplement/Models/Repair.cs @@ -6,7 +6,7 @@ using System.ComponentModel.DataAnnotations.Schema; namespace AutoWorkshopDatabaseImplement.Models { - public class Repair + public class Repair : IRepairModel { public int Id { get; set; } @@ -28,6 +28,7 @@ namespace AutoWorkshopDatabaseImplement.Models _repairComponents = Components.ToDictionary(RepComp => RepComp.ComponentId, RepComp => (RepComp.Component as IComponentModel, RepComp.Count)); } + return _repairComponents; } }