From 9a3f4f5271f88417d2040bd4a6cf19493bf39d5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BE=D0=BB=D0=BE=D0=B4=D1=8F?= Date: Wed, 19 Apr 2023 17:50:19 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B9=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B5=D0=BA=D1=82=20=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20+=20?= =?UTF-8?q?=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AutomobilePlant/AutomobilePlant.sln | 6 + .../AutomobilePlant/AutomobilePlant.csproj | 4 + .../AutoPlantDataBase.cs | 31 ++++++ .../AutomobilePlantDataBaseImplements.csproj | 22 ++++ .../Models/Car.cs | 104 ++++++++++++++++++ .../Models/CarComponent.cs | 27 +++++ .../Models/Component.cs | 68 ++++++++++++ .../Models/Order.cs | 73 ++++++++++++ 8 files changed, 335 insertions(+) create mode 100644 AutomobilePlant/AutomobilePlantDataBaseImplements/AutoPlantDataBase.cs create mode 100644 AutomobilePlant/AutomobilePlantDataBaseImplements/AutomobilePlantDataBaseImplements.csproj create mode 100644 AutomobilePlant/AutomobilePlantDataBaseImplements/Models/Car.cs create mode 100644 AutomobilePlant/AutomobilePlantDataBaseImplements/Models/CarComponent.cs create mode 100644 AutomobilePlant/AutomobilePlantDataBaseImplements/Models/Component.cs create mode 100644 AutomobilePlant/AutomobilePlantDataBaseImplements/Models/Order.cs diff --git a/AutomobilePlant/AutomobilePlant.sln b/AutomobilePlant/AutomobilePlant.sln index 97cd30b..70f52cd 100644 --- a/AutomobilePlant/AutomobilePlant.sln +++ b/AutomobilePlant/AutomobilePlant.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutomobilePlant", "Automobi EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomomilePlantFileImplement", "AutomomilePlantFileImplement\AutomomilePlantFileImplement.csproj", "{3EC099D5-0C5C-43F5-AC6D-09B4D7CE9D72}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlantDataBaseImplements", "AutomobilePlantDataBaseImplements\AutomobilePlantDataBaseImplements.csproj", "{166A9D0A-A545-4634-811B-7BC2BC33631D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {3EC099D5-0C5C-43F5-AC6D-09B4D7CE9D72}.Debug|Any CPU.Build.0 = Debug|Any CPU {3EC099D5-0C5C-43F5-AC6D-09B4D7CE9D72}.Release|Any CPU.ActiveCfg = Release|Any CPU {3EC099D5-0C5C-43F5-AC6D-09B4D7CE9D72}.Release|Any CPU.Build.0 = Release|Any CPU + {166A9D0A-A545-4634-811B-7BC2BC33631D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {166A9D0A-A545-4634-811B-7BC2BC33631D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {166A9D0A-A545-4634-811B-7BC2BC33631D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {166A9D0A-A545-4634-811B-7BC2BC33631D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj b/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj index b635bae..cf7ebd2 100644 --- a/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj +++ b/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj @@ -19,6 +19,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/AutomobilePlant/AutomobilePlantDataBaseImplements/AutoPlantDataBase.cs b/AutomobilePlant/AutomobilePlantDataBaseImplements/AutoPlantDataBase.cs new file mode 100644 index 0000000..1c5ee02 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDataBaseImplements/AutoPlantDataBase.cs @@ -0,0 +1,31 @@ +using AutomobilePlantDataBaseImplements.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantDataBaseImplements +{ + public class AutoPlantDataBase : DbContext + { + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql("Server=PostgreSQL;Host=localhost;Port=5432;Database=AutoPlantDataBase;Username=postgres;Password=postgres"); + } + base.OnConfiguring(optionsBuilder); + } + + public virtual DbSet Components { set; get; } + + public virtual DbSet Cars { set; get; } + + public virtual DbSet CarComponents { set; get; } + + public virtual DbSet Orders { set; get; } + } +} diff --git a/AutomobilePlant/AutomobilePlantDataBaseImplements/AutomobilePlantDataBaseImplements.csproj b/AutomobilePlant/AutomobilePlantDataBaseImplements/AutomobilePlantDataBaseImplements.csproj new file mode 100644 index 0000000..2766c38 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDataBaseImplements/AutomobilePlantDataBaseImplements.csproj @@ -0,0 +1,22 @@ + + + + net6.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/AutomobilePlant/AutomobilePlantDataBaseImplements/Models/Car.cs b/AutomobilePlant/AutomobilePlantDataBaseImplements/Models/Car.cs new file mode 100644 index 0000000..6fa5f51 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDataBaseImplements/Models/Car.cs @@ -0,0 +1,104 @@ +using AutomobilePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModel; + +namespace AutomobilePlantDataBaseImplements.Models +{ + public class Car : ICarModel + { + public int Id { get; set; } + + [Required] + public string CarName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + private Dictionary? _carComponents = null; + + [NotMapped] + public Dictionary CarComponents + { + get + { + if (_carComponents == null) + { + _carComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _carComponents; + } + } + + [ForeignKey("CarId")] + public virtual List Components { get; set; } = new(); + + [ForeignKey("CarId")] + public virtual List Orders { get; set; } = new(); + + public static Car Create(AutoPlantDataBase context, CarBindingModel model) + { + return new Car() + { + Id = model.Id, + CarName = model.CarName, + Price = model.Price, + Components = model.CarComponents.Select(x => new CarComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(CarBindingModel model) + { + CarName = model.CarName; + Price = model.Price; + } + + public CarViewModel GetViewModel => new() + { + Id = Id, + CarName = CarName, + Price = Price, + CarComponents = CarComponents + }; + + public void UpdateComponents(AutoPlantDataBase context, CarBindingModel model) + { + var carComponents = context.CarComponents.Where(rec => rec.CarId == model.Id).ToList(); + if (carComponents != null && carComponents.Count > 0) + { + context.CarComponents.RemoveRange(carComponents.Where(rec => !model.CarComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + + foreach (var updateComponent in carComponents) + { + updateComponent.Count = model.CarComponents[updateComponent.ComponentId].Item2; + model.CarComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var car = context.Cars.First(x => x.Id == Id); + foreach (var pc in model.CarComponents) + { + context.CarComponents.Add(new CarComponent + { + Car = car, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _carComponents = null; + } + } +} diff --git a/AutomobilePlant/AutomobilePlantDataBaseImplements/Models/CarComponent.cs b/AutomobilePlant/AutomobilePlantDataBaseImplements/Models/CarComponent.cs new file mode 100644 index 0000000..cddaea7 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDataBaseImplements/Models/CarComponent.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantDataBaseImplements.Models +{ + public class CarComponent + { + public int Id { get; set; } + + [Required] + public int CarId { get; set; } + + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Component Component { get; set; } = new(); + + public virtual Car Car { get; set; } = new(); + } +} diff --git a/AutomobilePlant/AutomobilePlantDataBaseImplements/Models/Component.cs b/AutomobilePlant/AutomobilePlantDataBaseImplements/Models/Component.cs new file mode 100644 index 0000000..5e17946 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDataBaseImplements/Models/Component.cs @@ -0,0 +1,68 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModel; +using AutomobilePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantDataBaseImplements.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 CarComponents { get; set; } = new(); + + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + + public static Component Create(ComponentViewModel model) + { + 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, + ComponentName = ComponentName, + Cost = Cost + }; + } +} diff --git a/AutomobilePlant/AutomobilePlantDataBaseImplements/Models/Order.cs b/AutomobilePlant/AutomobilePlantDataBaseImplements/Models/Order.cs new file mode 100644 index 0000000..f64799c --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDataBaseImplements/Models/Order.cs @@ -0,0 +1,73 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModel; +using AutomobilePlantDataModels.Enums; +using AutomobilePlantDataModels.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; + +namespace AutomobilePlantDataBaseImplements.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int CarId { get; private set; } + public string CarName { get; private set; } + public Car car { get; private set; } + [Required] + public int Count { get; private set; } + [Required] + public double Sum { get; private set; } + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + [Required] + public DateTime DateCreate { get; private set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); + public DateTime? DateImplement { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + CarId = model.CarId, + CarName = model.CarName, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + CarId = CarId, + CarName = CarName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +}