From e56dbb7f4b8c5c55b776eed1cff1e42307f7d328 Mon Sep 17 00:00:00 2001 From: kamilia Date: Fri, 21 Jun 2024 14:01:04 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=B0=D1=88=D0=B5=D1=82=20=D0=BF=D0=B0?= =?UTF-8?q?=D1=88=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractShopListImplement/OrderStorage.cs | 15 +++-- .../Implements/ComponentStorage.cs | 33 +++++++--- .../Implements/OrderStorage.cs | 63 +++++++++---------- .../Implements/PlaneStorage.cs | 49 ++++++++------- .../Models/Component.cs | 17 +++-- .../Models/Order.cs | 26 ++++---- .../Models/Plane.cs | 23 ++++--- .../Models/Shop.cs | 9 +-- 8 files changed, 126 insertions(+), 109 deletions(-) diff --git a/AircraftPlant/AbstractShopListImplement/OrderStorage.cs b/AircraftPlant/AbstractShopListImplement/OrderStorage.cs index 2dd4cfc..cdd1b93 100644 --- a/AircraftPlant/AbstractShopListImplement/OrderStorage.cs +++ b/AircraftPlant/AbstractShopListImplement/OrderStorage.cs @@ -28,6 +28,7 @@ namespace AircraftPlantListImplement.Implements { _source = DataListSingleton.GetInstance(); } + /// /// Получение полного списка /// @@ -41,6 +42,7 @@ namespace AircraftPlantListImplement.Implements } return result; } + /// /// Получение фильтрованного списка /// @@ -63,6 +65,7 @@ namespace AircraftPlantListImplement.Implements } return result; } + /// /// Получение элемента /// @@ -84,6 +87,7 @@ namespace AircraftPlantListImplement.Implements } return null; } + /// /// Добавление элемента /// @@ -109,6 +113,7 @@ namespace AircraftPlantListImplement.Implements _source.Orders.Add(newOrder); return GetViewModel(newOrder); } + /// /// Редактирование элемента /// @@ -126,6 +131,7 @@ namespace AircraftPlantListImplement.Implements } return null; } + /// /// Удаление элемента /// @@ -144,6 +150,7 @@ namespace AircraftPlantListImplement.Implements } return null; } + /// /// Получение модели заказа /// @@ -152,15 +159,15 @@ namespace AircraftPlantListImplement.Implements private OrderViewModel GetViewModel(Order order) { var viewModel = order.GetViewModel; - foreach (var plane in _source.Planes) + foreach (var sushi in _source.Planes) { - if (plane.Id == order.PlaneId) + if (sushi.Id == order.PlaneId) { - viewModel.PlaneName = plane.PlaneName; + viewModel.PlaneName = sushi.PlaneName; break; } } return viewModel; } } -} \ No newline at end of file +} diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/ComponentStorage.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/ComponentStorage.cs index c36a155..39f7cf5 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/ComponentStorage.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/ComponentStorage.cs @@ -16,7 +16,9 @@ namespace AircraftPlantDatabaseImplement.Implements public List GetFullList() { using var context = new AircraftPlantDatabase(); - return context.Components.Select(x => x.GetViewModel).ToList(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); } public List GetFilteredList(ComponentSearchModel model) { @@ -24,8 +26,12 @@ namespace AircraftPlantDatabaseImplement.Implements { return new(); } + using var context = new AircraftPlantDatabase(); - return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + return context.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); } public ComponentViewModel? GetElement(ComponentSearchModel model) { @@ -33,9 +39,13 @@ namespace AircraftPlantDatabaseImplement.Implements { return null; } + using var context = new AircraftPlantDatabase(); - return context.Components.FirstOrDefault(x =>(!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == - model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return context.Components + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && + x.ComponentName == model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; } public ComponentViewModel? Insert(ComponentBindingModel model) { @@ -44,6 +54,7 @@ namespace AircraftPlantDatabaseImplement.Implements { return null; } + using var context = new AircraftPlantDatabase(); context.Components.Add(newComponent); context.SaveChanges(); @@ -57,6 +68,7 @@ namespace AircraftPlantDatabaseImplement.Implements { return null; } + component.Update(model); context.SaveChanges(); return component.GetViewModel; @@ -65,13 +77,14 @@ namespace AircraftPlantDatabaseImplement.Implements { using var context = new AircraftPlantDatabase(); var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) + if (element == null) { - context.Components.Remove(element); - context.SaveChanges(); - return element.GetViewModel; + return null; } - return null; + + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; } } -} +} \ No newline at end of file diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs index cad61b5..a38c526 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/OrderStorage.cs @@ -3,7 +3,11 @@ using AircraftPlantContracts.SearchModels; using AircraftPlantContracts.StoragesContracts; using AircraftPlantContracts.ViewModels; using AircraftPlantDatabaseImplement.Models; -using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; namespace AircraftPlantDatabaseImplement.Implements { @@ -13,38 +17,32 @@ namespace AircraftPlantDatabaseImplement.Implements { using var context = new AircraftPlantDatabase(); return context.Orders - .Include(x => x.Plane) - .Select(x => x.GetViewModel) - .ToList(); + .Select(x => GetViewModel(x)) + .ToList(); } - public List GetFilteredList(OrderSearchModel model) { if (!model.Id.HasValue) { return new(); } + using var context = new AircraftPlantDatabase(); return context.Orders - .Include(x => x.Plane) - .Where(x => x.Id == model.Id) - .Select(x => x.GetViewModel) - .ToList(); + .Where(x => x.Id.Equals(model.Id)) + .Select(x => GetViewModel(x)) + .ToList(); } - public OrderViewModel? GetElement(OrderSearchModel model) { if (!model.Id.HasValue) { return null; } - using var context = new AircraftPlantDatabase(); - return context.Orders - .Include(x => x.Plane) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; - } + using var context = new AircraftPlantDatabase(); + return GetViewModel(context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); + } public OrderViewModel? Insert(OrderBindingModel model) { var newOrder = Order.Create(model); @@ -52,15 +50,12 @@ namespace AircraftPlantDatabaseImplement.Implements { return null; } + using var context = new AircraftPlantDatabase(); context.Orders.Add(newOrder); context.SaveChanges(); - return context.Orders - .Include(x => x.Plane) - .FirstOrDefault(x => x.Id == newOrder.Id) - ?.GetViewModel; + return GetViewModel(newOrder); } - public OrderViewModel? Update(OrderBindingModel model) { using var context = new AircraftPlantDatabase(); @@ -69,29 +64,33 @@ namespace AircraftPlantDatabaseImplement.Implements { return null; } + order.Update(model); context.SaveChanges(); - return context.Orders - .Include(x => x.Plane) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; + return GetViewModel(order); } - public OrderViewModel? Delete(OrderBindingModel model) { using var context = new AircraftPlantDatabase(); - var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + var element = context.Orders.FirstOrDefault(x => x.Id == model.Id); if (element != null) { - var deletedElement = context.Orders - .Include(x => x.Plane) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; context.Orders.Remove(element); context.SaveChanges(); - return deletedElement; + return GetViewModel(element); } return null; } + private static OrderViewModel GetViewModel(Order order) + { + using var context = new AircraftPlantDatabase(); + var viewModel = order.GetViewModel; + var plane = context.Planes.FirstOrDefault(x => x.Id == order.PlaneId); + if (plane != null) + { + viewModel.PlaneName = plane.PlaneName; + } + return viewModel; + } } } \ No newline at end of file diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/PlaneStorage.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/PlaneStorage.cs index 8befae3..a4d6007 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Implements/PlaneStorage.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Implements/PlaneStorage.cs @@ -18,11 +18,11 @@ namespace AircraftPlantDatabaseImplement.Implements { using var context = new AircraftPlantDatabase(); return context.Planes - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .ToList() - .Select(x => x.GetViewModel) - .ToList(); + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); } public List GetFilteredList(PlaneSearchModel model) { @@ -30,31 +30,31 @@ namespace AircraftPlantDatabaseImplement.Implements { return new(); } + using var context = new AircraftPlantDatabase(); return context.Planes - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .Where(x => x.PlaneName.Contains(model.PlaneName)) - .ToList() - .Select(x => x.GetViewModel) - .ToList(); + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.PlaneName.Contains(model.PlaneName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); } public PlaneViewModel? GetElement(PlaneSearchModel model) { - if (string.IsNullOrEmpty(model.PlaneName) && - !model.Id.HasValue) + if (string.IsNullOrEmpty(model.PlaneName) && !model.Id.HasValue) { return null; } + using var context = new AircraftPlantDatabase(); return context.Planes - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PlaneName) && - x.PlaneName == model.PlaneName) || - (model.Id.HasValue && x.Id == - model.Id)) - ?.GetViewModel; + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PlaneName) && + x.PlaneName == model.PlaneName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; } public PlaneViewModel? Insert(PlaneBindingModel model) { @@ -64,6 +64,7 @@ namespace AircraftPlantDatabaseImplement.Implements { return null; } + context.Planes.Add(newPlane); context.SaveChanges(); return newPlane.GetViewModel; @@ -74,12 +75,12 @@ namespace AircraftPlantDatabaseImplement.Implements using var transaction = context.Database.BeginTransaction(); try { - var plane = context.Planes.FirstOrDefault(rec => - rec.Id == model.Id); + var plane = context.Planes.FirstOrDefault(rec => rec.Id == model.Id); if (plane == null) { return null; } + plane.Update(model); context.SaveChanges(); plane.UpdateComponents(context, model); @@ -96,8 +97,8 @@ namespace AircraftPlantDatabaseImplement.Implements { using var context = new AircraftPlantDatabase(); var element = context.Planes - .Include(x => x.Components) - .FirstOrDefault(rec => rec.Id == model.Id); + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Planes.Remove(element); diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Component.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Component.cs index 7ffeebf..e7710c4 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Component.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Component.cs @@ -5,28 +5,29 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics.Contracts; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; namespace AircraftPlantDatabaseImplement.Models { public class Component : IComponentModel { public int Id { get; private set; } - - [Required] public string ComponentName { get; private set; } = string.Empty; - [Required] public double Cost { get; set; } - [ForeignKey("ComponentId")] public virtual List PlaneComponents { get; set; } = new(); - public static Component? Create(ComponentBindingModel model) { if (model == null) { return null; } + return new Component() { Id = model.Id, @@ -34,27 +35,25 @@ namespace AircraftPlantDatabaseImplement.Models Cost = model.Cost }; } - public static Component Create(ComponentViewModel model) { - return new Component + return new Component() { Id = model.Id, ComponentName = model.ComponentName, Cost = model.Cost }; } - public void Update(ComponentBindingModel model) { if (model == null) { return; } + ComponentName = model.ComponentName; Cost = model.Cost; } - public ComponentViewModel GetViewModel => new() { Id = Id, diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs index 64e7110..c308568 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs @@ -2,40 +2,40 @@ using AircraftPlantContracts.ViewModels; using AircraftPlantDataModels.Enums; using AircraftPlantDataModels.Models; +using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; namespace AircraftPlantDatabaseImplement.Models { + /// + /// Сущность "Заказ" + /// public class Order : IOrderModel { public int Id { get; set; } - [Required] public int PlaneId { get; set; } - [Required] public int Count { get; set; } - [Required] public double Sum { get; set; } - [Required] public OrderStatus Status { get; set; } - [Required] public DateTime DateCreate { get; set; } - public DateTime? DateImplement { get; set; } - - public virtual Plane Plane { get; set; } - public static Order? Create(OrderBindingModel? model) { if (model == null) { return null; } - return new Order() + + return new Order { Id = model.Id, PlaneId = model.PlaneId, @@ -46,17 +46,16 @@ namespace AircraftPlantDatabaseImplement.Models DateImplement = model.DateImplement }; } - public void Update(OrderBindingModel? model) { if (model == null) { return; } + Status = model.Status; DateImplement = model.DateImplement; } - public OrderViewModel GetViewModel => new() { Id = Id, @@ -65,8 +64,7 @@ namespace AircraftPlantDatabaseImplement.Models Sum = Sum, Status = Status, DateCreate = DateCreate, - DateImplement = DateImplement, - PlaneName = Plane.PlaneName + DateImplement = DateImplement }; } } \ No newline at end of file diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Plane.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Plane.cs index 3108c85..0b047e5 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Plane.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Plane.cs @@ -1,13 +1,13 @@ -using System; +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.ViewModels; +using AircraftPlantDataModels.Models; +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; -using AircraftPlantContracts.BindingModels; -using AircraftPlantContracts.ViewModels; -using AircraftPlantDataModels.Models; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; namespace AircraftPlantDatabaseImplement.Models { @@ -19,6 +19,7 @@ namespace AircraftPlantDatabaseImplement.Models [Required] public double Price { get; set; } private Dictionary? _planeComponents = null; + [NotMapped] public Dictionary PlaneComponents { @@ -27,7 +28,7 @@ namespace AircraftPlantDatabaseImplement.Models if (_planeComponents == null) { _planeComponents = Components - .ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + .ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); } return _planeComponents; } @@ -55,6 +56,7 @@ namespace AircraftPlantDatabaseImplement.Models PlaneName = model.PlaneName; Price = model.Price; } + public PlaneViewModel GetViewModel => new() { Id = Id, @@ -66,10 +68,11 @@ namespace AircraftPlantDatabaseImplement.Models { var planeComponents = context.PlaneComponents.Where(rec => rec.PlaneId == model.Id).ToList(); if (planeComponents != null && planeComponents.Count > 0) - { // удалили те, которых нет в модели + { + // Удаление компонентов, которых нет в модели context.PlaneComponents.RemoveRange(planeComponents.Where(rec => !model.PlaneComponents.ContainsKey(rec.ComponentId))); context.SaveChanges(); - // обновили количество у существующих записей + // Обновление количества у существующих записей foreach (var updateComponent in planeComponents) { updateComponent.Count = model.PlaneComponents[updateComponent.ComponentId].Item2; @@ -91,4 +94,4 @@ namespace AircraftPlantDatabaseImplement.Models _planeComponents = null; } } -} +} \ No newline at end of file diff --git a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Shop.cs b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Shop.cs index 86f99b5..15b6801 100644 --- a/AircraftPlant/AircraftPlantDatabaseImplement/Models/Shop.cs +++ b/AircraftPlant/AircraftPlantDatabaseImplement/Models/Shop.cs @@ -12,19 +12,18 @@ using System.Threading.Tasks; namespace AircraftPlantDatabaseImplement.Models { + /// + /// Сущность "Магазин" + /// public class Shop : IShopModel { public int Id { get; set; } - [Required] public string ShopName { get; set; } = string.Empty; - [Required] public string Address { get; set; } = string.Empty; - [Required] public DateTime DateOpening { get; set; } - private Dictionary? _shopPlanes = null; [NotMapped] @@ -41,10 +40,8 @@ namespace AircraftPlantDatabaseImplement.Models } } public int MaxPlanes { get; set; } - [ForeignKey("ShopId")] public List Planes { get; set; } = new(); - public static Shop Create(AircraftPlantDatabase context, ShopBindingModel model) { return new Shop