From 80e6284d643ef9590a617f8c301df2ee68b33103 Mon Sep 17 00:00:00 2001 From: Safgerd Date: Sun, 12 Mar 2023 16:32:00 +0400 Subject: [PATCH 1/5] =?UTF-8?q?LabWork03=5FBase:=20=D0=92=D1=80=D0=BE?= =?UTF-8?q?=D0=B4=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AutomobilePlant/AutomobilePlant.sln | 6 + .../AutomobilePlant/AutomobilePlant.csproj | 5 + AutomobilePlant/AutomobilePlant/Program.cs | 2 +- .../AutomobilePlantDatabase.cs | 28 +++ .../AutomobilePlantDatabaseImplement.csproj | 23 +++ .../Implements/CarStorage.cs | 129 +++++++++++++ .../Implements/ComponentStorage.cs | 87 +++++++++ .../Implements/OrderStorage.cs | 85 +++++++++ .../20230312012119_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20230312012119_InitialCreate.cs | 125 +++++++++++++ .../AutomobilePlantDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Car.cs | 101 +++++++++++ .../Models/CarComponent.cs | 24 +++ .../Models/Component.cs | 57 ++++++ .../Models/Order.cs | 73 ++++++++ 15 files changed, 1083 insertions(+), 1 deletion(-) create mode 100644 AutomobilePlant/AutomobilePlantDatabaseImplement/AutomobilePlantDatabase.cs create mode 100644 AutomobilePlant/AutomobilePlantDatabaseImplement/AutomobilePlantDatabaseImplement.csproj create mode 100644 AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/CarStorage.cs create mode 100644 AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/OrderStorage.cs create mode 100644 AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230312012119_InitialCreate.Designer.cs create mode 100644 AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230312012119_InitialCreate.cs create mode 100644 AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/AutomobilePlantDatabaseModelSnapshot.cs create mode 100644 AutomobilePlant/AutomobilePlantDatabaseImplement/Models/Car.cs create mode 100644 AutomobilePlant/AutomobilePlantDatabaseImplement/Models/CarComponent.cs create mode 100644 AutomobilePlant/AutomobilePlantDatabaseImplement/Models/Component.cs create mode 100644 AutomobilePlant/AutomobilePlantDatabaseImplement/Models/Order.cs diff --git a/AutomobilePlant/AutomobilePlant.sln b/AutomobilePlant/AutomobilePlant.sln index 93ebfba..f9515e0 100644 --- a/AutomobilePlant/AutomobilePlant.sln +++ b/AutomobilePlant/AutomobilePlant.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutomobilePlantListImplemen EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlantFileImplement", "AutomobilePlantFileImplement\AutomobilePlantFileImplement.csproj", "{10818526-654C-4928-985F-9C02A2A02B15}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlantDatabaseImplement", "AutomobilePlantDatabaseImplement\AutomobilePlantDatabaseImplement.csproj", "{6C1976A0-354A-4700-9CDA-3242782F5632}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {10818526-654C-4928-985F-9C02A2A02B15}.Debug|Any CPU.Build.0 = Debug|Any CPU {10818526-654C-4928-985F-9C02A2A02B15}.Release|Any CPU.ActiveCfg = Release|Any CPU {10818526-654C-4928-985F-9C02A2A02B15}.Release|Any CPU.Build.0 = Release|Any CPU + {6C1976A0-354A-4700-9CDA-3242782F5632}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6C1976A0-354A-4700-9CDA-3242782F5632}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6C1976A0-354A-4700-9CDA-3242782F5632}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6C1976A0-354A-4700-9CDA-3242782F5632}.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 18528cf..c64daed 100644 --- a/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj +++ b/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -16,6 +20,7 @@ + diff --git a/AutomobilePlant/AutomobilePlant/Program.cs b/AutomobilePlant/AutomobilePlant/Program.cs index 3b193cf..1255289 100644 --- a/AutomobilePlant/AutomobilePlant/Program.cs +++ b/AutomobilePlant/AutomobilePlant/Program.cs @@ -1,7 +1,7 @@ using AutomobilePlantBusinessLogic.BusinessLogics; using AutomobilePlantContracts.BusinessLogicContracts; using AutomobilePlantContracts.StorageContracts; -using AutomobilePlantFileImplement.Implements; +using AutomobilePlantDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/AutomobilePlantDatabase.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/AutomobilePlantDatabase.cs new file mode 100644 index 0000000..f960a93 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/AutomobilePlantDatabase.cs @@ -0,0 +1,28 @@ +using AutomobilePlantDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Reflection.Metadata; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantDatabaseImplement +{ + public class AutomobilePlantDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Initial Catalog=AutomobilePlantDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;TrustServerCertificate=True"); + } + 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/AutomobilePlantDatabaseImplement/AutomobilePlantDatabaseImplement.csproj b/AutomobilePlant/AutomobilePlantDatabaseImplement/AutomobilePlantDatabaseImplement.csproj new file mode 100644 index 0000000..35acf42 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/AutomobilePlantDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/CarStorage.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/CarStorage.cs new file mode 100644 index 0000000..5a6e21a --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/CarStorage.cs @@ -0,0 +1,129 @@ +using AutomobilePlantContracts.StorageContracts; +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Metadata; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace AutomobilePlantDatabaseImplement.Implements +{ + public class CarStorage : ICarStorage + { + public CarViewModel? GetElement(CarSearchModel model) + { + if (string.IsNullOrEmpty(model.CarName) && !model.Id.HasValue) + { + return null; + } + using var context = new AutomobilePlantDatabase(); + return context.Cars.Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.CarName) && + x.CarName == model.CarName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(CarSearchModel model) + { + if (string.IsNullOrEmpty(model.CarName)) + { + return new(); + } + using var context = new AutomobilePlantDatabase(); + return context.Cars + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.CarName.Contains(model.CarName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new AutomobilePlantDatabase(); + return context.Cars + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public CarViewModel? Insert(CarBindingModel model) + { + using var context = new AutomobilePlantDatabase(); + using var transaction = context.Database.BeginTransaction(); + { + try + { + var newCar = Car.Create(context, model); + if (newCar == null) + { + transaction.Rollback(); + return null; + } + context.Cars.Add(newCar); + + context.SaveChanges(); + context.Database.CommitTransaction(); + + return newCar.GetViewModel; + } + catch (Exception) + { + transaction.Rollback(); + return null; + } + } + } + + public CarViewModel? Update(CarBindingModel model) + { + using var context = new AutomobilePlantDatabase(); + using var transaction = context.Database.BeginTransaction(); + { + try + { + var car = context.Cars.FirstOrDefault(x => x.Id == model.Id); + if (car == null) + { + transaction.Rollback(); + return null; + } + + car.Update(model); + + context.SaveChanges(); + context.Database.CommitTransaction(); + + return car.GetViewModel; + } + catch (Exception) + { + transaction.Rollback(); + return null; + } + } + } + public CarViewModel? Delete(CarBindingModel model) + { + using var context = new AutomobilePlantDatabase(); + var element = context.Cars.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Cars.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/ComponentStorage.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..fe5e394 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,87 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.StorageContracts; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + using var context = new AutomobilePlantDatabase(); + return context.Components.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) + || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new AutomobilePlantDatabase(); + return context.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new AutomobilePlantDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new AutomobilePlantDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new AutomobilePlantDatabase(); + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new AutomobilePlantDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/OrderStorage.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..36959d7 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,85 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.StorageContracts; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new AutomobilePlantDatabase(); + return context.Orders.Include(x => x.Car).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new AutomobilePlantDatabase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Include(x => x.Car) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new AutomobilePlantDatabase(); + return context.Orders.Include(x => x.Car).Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new AutomobilePlantDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders.Include(x => x.Car).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new AutomobilePlantDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return context.Orders.Include(x => x.Car).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new AutomobilePlantDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230312012119_InitialCreate.Designer.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230312012119_InitialCreate.Designer.cs new file mode 100644 index 0000000..85c0715 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230312012119_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using AutomobilePlantDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AutomobilePlantDatabaseImplement.Migrations +{ + [DbContext(typeof(AutomobilePlantDatabase))] + [Migration("20230312012119_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Car", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CarName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Cars"); + }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.CarComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CarId") + .HasColumnType("int"); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("CarId"); + + b.HasIndex("ComponentId"); + + b.ToTable("CarComponents"); + }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CarId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("CarId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.CarComponent", b => + { + b.HasOne("AutomobilePlantDatabaseImplement.Models.Car", "Car") + .WithMany("Components") + .HasForeignKey("CarId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AutomobilePlantDatabaseImplement.Models.Component", "Component") + .WithMany("CarComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Car"); + + b.Navigation("Component"); + }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Order", b => + { + b.HasOne("AutomobilePlantDatabaseImplement.Models.Car", "Car") + .WithMany("Orders") + .HasForeignKey("CarId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Car"); + }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Car", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Component", b => + { + b.Navigation("CarComponents"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230312012119_InitialCreate.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230312012119_InitialCreate.cs new file mode 100644 index 0000000..8dd6d3e --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230312012119_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace AutomobilePlantDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Cars", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + CarName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Cars", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + CarId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Cars_CarId", + column: x => x.CarId, + principalTable: "Cars", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "CarComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + CarId = table.Column(type: "int", nullable: false), + ComponentId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_CarComponents", x => x.Id); + table.ForeignKey( + name: "FK_CarComponents_Cars_CarId", + column: x => x.CarId, + principalTable: "Cars", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_CarComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_CarComponents_CarId", + table: "CarComponents", + column: "CarId"); + + migrationBuilder.CreateIndex( + name: "IX_CarComponents_ComponentId", + table: "CarComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_CarId", + table: "Orders", + column: "CarId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "CarComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Cars"); + } + } +} diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/AutomobilePlantDatabaseModelSnapshot.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/AutomobilePlantDatabaseModelSnapshot.cs new file mode 100644 index 0000000..918d6ae --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/AutomobilePlantDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using AutomobilePlantDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AutomobilePlantDatabaseImplement.Migrations +{ + [DbContext(typeof(AutomobilePlantDatabase))] + partial class AutomobilePlantDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Car", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CarName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Cars"); + }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.CarComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CarId") + .HasColumnType("int"); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("CarId"); + + b.HasIndex("ComponentId"); + + b.ToTable("CarComponents"); + }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CarId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("CarId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.CarComponent", b => + { + b.HasOne("AutomobilePlantDatabaseImplement.Models.Car", "Car") + .WithMany("Components") + .HasForeignKey("CarId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AutomobilePlantDatabaseImplement.Models.Component", "Component") + .WithMany("CarComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Car"); + + b.Navigation("Component"); + }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Order", b => + { + b.HasOne("AutomobilePlantDatabaseImplement.Models.Car", "Car") + .WithMany("Orders") + .HasForeignKey("CarId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Car"); + }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Car", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Component", b => + { + b.Navigation("CarComponents"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/Models/Car.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Models/Car.cs new file mode 100644 index 0000000..b84c10e --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Models/Car.cs @@ -0,0 +1,101 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Reflection.Metadata; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantDatabaseImplement.Models +{ + public class Car : ICarModel + { + public int Id { get; private set; } + [Required] + public string CarName { get; private set; } = string.Empty; + [Required] + public double Price { get; private 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(AutomobilePlantDatabase context, CarBindingModel model) + { + var components = context.Components; + 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(AutomobilePlantDatabase 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/AutomobilePlantDatabaseImplement/Models/CarComponent.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Models/CarComponent.cs new file mode 100644 index 0000000..3ded20f --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Models/CarComponent.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Numerics; +using System.Reflection.Metadata; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantDatabaseImplement.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/AutomobilePlantDatabaseImplement/Models/Component.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..aa0abd4 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Models/Component.cs @@ -0,0 +1,57 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantDatabaseImplement.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 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/AutomobilePlantDatabaseImplement/Models/Order.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..82e3e88 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Models/Order.cs @@ -0,0 +1,73 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantDataModels.Enums; +using AutomobilePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Runtime.ConstrainedExecution; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int CarId { 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.Now; + + public DateTime? DateImplement { get; private set; } + public virtual Car Car { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + CarId = model.CarId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Id = model.Id, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + CarId = CarId, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status, + CarName = Car.CarName + }; + } +} -- 2.25.1 From 9de5f87f2aa0f7289623a0f3329d8dc0306c6689 Mon Sep 17 00:00:00 2001 From: Safgerd Date: Mon, 27 Mar 2023 02:56:45 +0400 Subject: [PATCH 2/5] =?UTF-8?q?LabWork03=5FHard:=20=D0=A0=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0,=20?= =?UTF-8?q?=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D1=8B=20=D1=84=D0=B8?= =?UTF-8?q?=D0=BA=D1=81=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AutomobilePlantDatabase.cs | 2 + .../AutomobilePlantDatabaseImplement.csproj | 4 + .../Implements/ShopStorage.cs | 140 ++++++++++++++++++ ... => 20230326225239_ShopsAdded.Designer.cs} | 81 +++++++++- ...Create.cs => 20230326225239_ShopsAdded.cs} | 61 +++++++- .../AutomobilePlantDatabaseModelSnapshot.cs | 77 ++++++++++ .../Models/Shop.cs | 113 ++++++++++++++ .../Models/ShopCar.cs | 28 ++++ 8 files changed, 503 insertions(+), 3 deletions(-) create mode 100644 AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/ShopStorage.cs rename AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/{20230312012119_InitialCreate.Designer.cs => 20230326225239_ShopsAdded.Designer.cs} (67%) rename AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/{20230312012119_InitialCreate.cs => 20230326225239_ShopsAdded.cs} (65%) create mode 100644 AutomobilePlant/AutomobilePlantDatabaseImplement/Models/Shop.cs create mode 100644 AutomobilePlant/AutomobilePlantDatabaseImplement/Models/ShopCar.cs diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/AutomobilePlantDatabase.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/AutomobilePlantDatabase.cs index f960a93..f86161e 100644 --- a/AutomobilePlant/AutomobilePlantDatabaseImplement/AutomobilePlantDatabase.cs +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/AutomobilePlantDatabase.cs @@ -24,5 +24,7 @@ namespace AutomobilePlantDatabaseImplement public virtual DbSet Cars { set; get; } public virtual DbSet CarComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopCars { set; get; } } } diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/AutomobilePlantDatabaseImplement.csproj b/AutomobilePlant/AutomobilePlantDatabaseImplement/AutomobilePlantDatabaseImplement.csproj index 35acf42..869eafc 100644 --- a/AutomobilePlant/AutomobilePlantDatabaseImplement/AutomobilePlantDatabaseImplement.csproj +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/AutomobilePlantDatabaseImplement.csproj @@ -20,4 +20,8 @@ + + + + diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/ShopStorage.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..f2b1263 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,140 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.StorageContracts; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantDatabaseImplement.Models; +using AutomobilePlantDataModels.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return new(); + } + using var context = new AutomobilePlantDatabase(); + return context.Shops.Include(x => x.Cars).ThenInclude(x => x.Car).FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new AutomobilePlantDatabase(); + return context.Shops.Include(x => x.Cars).ThenInclude(x => x.Car).Where(x => x.Name.Contains(model.Name)).ToList().Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new AutomobilePlantDatabase(); + return context.Shops.Include(x => x.Cars).ThenInclude(x => x.Car).ToList().Select(x => x.GetViewModel).ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new AutomobilePlantDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + if (context.Shops.Any(x => x.Name == newShop.Name)) + { + throw new Exception("Название магазина уже занято"); + } + + context.Shops.Add(newShop); + context.SaveChanges(); + transaction.Commit(); + return newShop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new AutomobilePlantDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + context.SaveChanges(); + shop.UpdateCars(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new AutomobilePlantDatabase(); + var shop = context.Shops.Include(x => x.Cars).FirstOrDefault(x => x.Id == model.Id); + if (shop != null) + { + context.Shops.Remove(shop); + context.SaveChanges(); + return shop.GetViewModel; + } + return null; + } + + public bool SellCar(ICarModel model, int count) + { + using var context = new AutomobilePlantDatabase(); + using var transaction = context.Database.BeginTransaction(); + foreach (var shopCars in context.ShopCars.Where(x => x.CarId == model.Id)) + { + var min = Math.Min(count, shopCars.Count); + shopCars.Count -= min; + count -= min; + if (count <= 0) + { + break; + } + } + if (count == 0) + { + context.SaveChanges(); + transaction.Commit(); + } + else + { + transaction.Rollback(); + } + if (count > 0) + { + return false; + } + return true; + } + } +} diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230312012119_InitialCreate.Designer.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230326225239_ShopsAdded.Designer.cs similarity index 67% rename from AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230312012119_InitialCreate.Designer.cs rename to AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230326225239_ShopsAdded.Designer.cs index 85c0715..479e937 100644 --- a/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230312012119_InitialCreate.Designer.cs +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230326225239_ShopsAdded.Designer.cs @@ -12,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace AutomobilePlantDatabaseImplement.Migrations { [DbContext(typeof(AutomobilePlantDatabase))] - [Migration("20230312012119_InitialCreate")] - partial class InitialCreate + [Migration("20230326225239_ShopsAdded")] + partial class ShopsAdded { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -124,6 +124,59 @@ namespace AutomobilePlantDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("MaxCountCars") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.ShopCar", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CarId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("CarId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopCars"); + }); + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.CarComponent", b => { b.HasOne("AutomobilePlantDatabaseImplement.Models.Car", "Car") @@ -154,6 +207,25 @@ namespace AutomobilePlantDatabaseImplement.Migrations b.Navigation("Car"); }); + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.ShopCar", b => + { + b.HasOne("AutomobilePlantDatabaseImplement.Models.Car", "Car") + .WithMany() + .HasForeignKey("CarId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AutomobilePlantDatabaseImplement.Models.Shop", "Shop") + .WithMany("Cars") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Car"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Car", b => { b.Navigation("Components"); @@ -165,6 +237,11 @@ namespace AutomobilePlantDatabaseImplement.Migrations { b.Navigation("CarComponents"); }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Shop", b => + { + b.Navigation("Cars"); + }); #pragma warning restore 612, 618 } } diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230312012119_InitialCreate.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230326225239_ShopsAdded.cs similarity index 65% rename from AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230312012119_InitialCreate.cs rename to AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230326225239_ShopsAdded.cs index 8dd6d3e..558cbfe 100644 --- a/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230312012119_InitialCreate.cs +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/20230326225239_ShopsAdded.cs @@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace AutomobilePlantDatabaseImplement.Migrations { /// - public partial class InitialCreate : Migration + public partial class ShopsAdded : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -39,6 +39,22 @@ namespace AutomobilePlantDatabaseImplement.Migrations table.PrimaryKey("PK_Components", x => x.Id); }); + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + OpeningDate = table.Column(type: "datetime2", nullable: false), + MaxCountCars = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + migrationBuilder.CreateTable( name: "Orders", columns: table => new @@ -90,6 +106,33 @@ namespace AutomobilePlantDatabaseImplement.Migrations onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateTable( + name: "ShopCars", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + CarId = table.Column(type: "int", nullable: false), + ShopId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopCars", x => x.Id); + table.ForeignKey( + name: "FK_ShopCars_Cars_CarId", + column: x => x.CarId, + principalTable: "Cars", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopCars_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateIndex( name: "IX_CarComponents_CarId", table: "CarComponents", @@ -104,6 +147,16 @@ namespace AutomobilePlantDatabaseImplement.Migrations name: "IX_Orders_CarId", table: "Orders", column: "CarId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopCars_CarId", + table: "ShopCars", + column: "CarId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopCars_ShopId", + table: "ShopCars", + column: "ShopId"); } /// @@ -115,11 +168,17 @@ namespace AutomobilePlantDatabaseImplement.Migrations migrationBuilder.DropTable( name: "Orders"); + migrationBuilder.DropTable( + name: "ShopCars"); + migrationBuilder.DropTable( name: "Components"); migrationBuilder.DropTable( name: "Cars"); + + migrationBuilder.DropTable( + name: "Shops"); } } } diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/AutomobilePlantDatabaseModelSnapshot.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/AutomobilePlantDatabaseModelSnapshot.cs index 918d6ae..c58fc25 100644 --- a/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/AutomobilePlantDatabaseModelSnapshot.cs +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Migrations/AutomobilePlantDatabaseModelSnapshot.cs @@ -121,6 +121,59 @@ namespace AutomobilePlantDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("MaxCountCars") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.ShopCar", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CarId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("CarId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopCars"); + }); + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.CarComponent", b => { b.HasOne("AutomobilePlantDatabaseImplement.Models.Car", "Car") @@ -151,6 +204,25 @@ namespace AutomobilePlantDatabaseImplement.Migrations b.Navigation("Car"); }); + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.ShopCar", b => + { + b.HasOne("AutomobilePlantDatabaseImplement.Models.Car", "Car") + .WithMany() + .HasForeignKey("CarId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AutomobilePlantDatabaseImplement.Models.Shop", "Shop") + .WithMany("Cars") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Car"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Car", b => { b.Navigation("Components"); @@ -162,6 +234,11 @@ namespace AutomobilePlantDatabaseImplement.Migrations { b.Navigation("CarComponents"); }); + + modelBuilder.Entity("AutomobilePlantDatabaseImplement.Models.Shop", b => + { + b.Navigation("Cars"); + }); #pragma warning restore 612, 618 } } diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/Models/Shop.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..9b640d4 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Models/Shop.cs @@ -0,0 +1,113 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModels; +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 AutomobilePlantDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + + [Required] + public string Address { get; set; } = string.Empty; + + [Required] + public DateTime OpeningDate { get; set; } + + [ForeignKey("ShopId")] + public List Cars { get; set; } = new(); + + private Dictionary? _shopCars = null; + + [NotMapped] + public Dictionary ShopCars + { + get + { + if (_shopCars == null) + { + _shopCars = Cars.ToDictionary(recPC => recPC.CarId, recPC => (recPC.Car as ICarModel, recPC.Count)); + } + return _shopCars; + } + } + + [Required] + public int MaxCountCars { get; set; } + + public static Shop Create(AutomobilePlantDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + Name = model.Name, + Address = model.Address, + OpeningDate = model.OpeningDate, + Cars = model.ShopCars.Select(x => new ShopCar + { + Car = context.Cars.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + MaxCountCars = model.MaxCountCars + }; + } + + public void Update(ShopBindingModel model) + { + Name = model.Name; + Address = model.Address; + OpeningDate = model.OpeningDate; + MaxCountCars = model.MaxCountCars; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Address = Address, + OpeningDate = OpeningDate, + ShopCars = ShopCars, + MaxCountCars = MaxCountCars + }; + + public void UpdateCars(AutomobilePlantDatabase context, ShopBindingModel model) + { + var ShopCars = context.ShopCars.Where(rec => rec.ShopId == model.Id).ToList(); + if (ShopCars != null && ShopCars.Count > 0) + { + // удалили те, которых нет в модели + context.ShopCars.RemoveRange(ShopCars.Where(rec => !model.ShopCars.ContainsKey(rec.CarId))); + context.SaveChanges(); + ShopCars = context.ShopCars.Where(rec => rec.ShopId == model.Id).ToList(); + // обновили количество у существующих записей + foreach (var updateCar in ShopCars) + { + updateCar.Count = model.ShopCars[updateCar.CarId].Item2; + model.ShopCars.Remove(updateCar.CarId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var elem in model.ShopCars) + { + context.ShopCars.Add(new ShopCar + { + Shop = shop, + Car = context.Cars.First(x => x.Id == elem.Key), + Count = elem.Value.Item2 + }); + context.SaveChanges(); + } + _shopCars = null; + } + } +} diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/Models/ShopCar.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Models/ShopCar.cs new file mode 100644 index 0000000..5533560 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Models/ShopCar.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Reflection.Metadata; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantDatabaseImplement.Models +{ + public class ShopCar + { + public int Id { get; set; } + + [Required] + public int CarId { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + + public virtual Car Car { get; set; } = new(); + } +} -- 2.25.1 From 95a10df89d8f95a703fde116cd137deba18c0632 Mon Sep 17 00:00:00 2001 From: Safgerd Date: Sun, 9 Apr 2023 21:23:18 +0400 Subject: [PATCH 3/5] =?UTF-8?q?LabWork03=5FHard:=20=D0=A4=D0=B8=D0=BA?= =?UTF-8?q?=D1=81=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/CarStorage.cs | 1 + .../Implements/ShopStorage.cs | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/CarStorage.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/CarStorage.cs index 5a6e21a..646fdd2 100644 --- a/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/CarStorage.cs +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/CarStorage.cs @@ -100,6 +100,7 @@ namespace AutomobilePlantDatabaseImplement.Implements } car.Update(model); + car.UpdateComponents(context, model); context.SaveChanges(); context.Database.CommitTransaction(); diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/ShopStorage.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/ShopStorage.cs index f2b1263..fa868a7 100644 --- a/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/ShopStorage.cs +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/ShopStorage.cs @@ -77,14 +77,17 @@ namespace AutomobilePlantDatabaseImplement.Implements using var transaction = context.Database.BeginTransaction(); try { - var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id); + var shop = context.Shops.Include(x => x.Cars).FirstOrDefault(x => x.Id == model.Id); if (shop == null) { return null; } shop.Update(model); context.SaveChanges(); - shop.UpdateCars(context, model); + if (model.ShopCars.Count > 0) + { + shop.UpdateCars(context, model); + } transaction.Commit(); return shop.GetViewModel; } -- 2.25.1 From 3a965ffbe30fb7c912eef95a16f592ea762e21f6 Mon Sep 17 00:00:00 2001 From: Safgerd Date: Sun, 9 Apr 2023 21:27:20 +0400 Subject: [PATCH 4/5] =?UTF-8?q?LabWork03=5FBase:=20=D0=A4=D0=B8=D0=BA?= =?UTF-8?q?=D1=81=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AutomobilePlantDatabaseImplement/Implements/CarStorage.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/CarStorage.cs b/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/CarStorage.cs index 5a6e21a..e687073 100644 --- a/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/CarStorage.cs +++ b/AutomobilePlant/AutomobilePlantDatabaseImplement/Implements/CarStorage.cs @@ -10,6 +10,7 @@ using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; +using System.Xml.Linq; namespace AutomobilePlantDatabaseImplement.Implements { @@ -100,6 +101,7 @@ namespace AutomobilePlantDatabaseImplement.Implements } car.Update(model); + car.UpdateComponents(context, model); context.SaveChanges(); context.Database.CommitTransaction(); -- 2.25.1 From 05c44b91b11807e4d4876c366909cd282e5b229b Mon Sep 17 00:00:00 2001 From: Safgerd Date: Fri, 28 Apr 2023 16:57:34 +0400 Subject: [PATCH 5/5] gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ca1c7a3..6192267 100644 --- a/.gitignore +++ b/.gitignore @@ -398,3 +398,4 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml +/AutomobilePlant/ImplementationExtensions \ No newline at end of file -- 2.25.1