From 6775260effc90940c86bea1a42a524029e2ed884 Mon Sep 17 00:00:00 2001 From: eegov Date: Fri, 24 Feb 2023 12:45:01 +0400 Subject: [PATCH] ThirdLaba --- AbstractShop/AbstractShop.sln | 6 + .../AbstractShopDatabase.cs | 25 +++ .../AbstractShopDatabaseImplement.csproj | 23 +++ .../Implements/ComponentStorage.cs | 84 ++++++++++ .../Implements/OrderStorage.cs | 40 +++++ .../Implements/ProductStorage.cs | 106 ++++++++++++ .../20230224083046_InitMigration.Designer.cs | 156 ++++++++++++++++++ .../20230224083046_InitMigration.cs | 114 +++++++++++++ .../AbstractShopDatabaseModelSnapshot.cs | 153 +++++++++++++++++ .../Models/Component.cs | 63 +++++++ .../Models/Order.cs | 38 +++++ .../Models/Product.cs | 96 +++++++++++ .../Models/ProductComponent.cs | 22 +++ .../AbstractShopView/AbstractShopView.csproj | 5 + AbstractShop/AbstractShopView/Program.cs | 2 +- 15 files changed, 932 insertions(+), 1 deletion(-) create mode 100644 AbstractShop/AbstractShopDatabaseImplement/AbstractShopDatabase.cs create mode 100644 AbstractShop/AbstractShopDatabaseImplement/AbstractShopDatabaseImplement.csproj create mode 100644 AbstractShop/AbstractShopDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 AbstractShop/AbstractShopDatabaseImplement/Implements/OrderStorage.cs create mode 100644 AbstractShop/AbstractShopDatabaseImplement/Implements/ProductStorage.cs create mode 100644 AbstractShop/AbstractShopDatabaseImplement/Migrations/20230224083046_InitMigration.Designer.cs create mode 100644 AbstractShop/AbstractShopDatabaseImplement/Migrations/20230224083046_InitMigration.cs create mode 100644 AbstractShop/AbstractShopDatabaseImplement/Migrations/AbstractShopDatabaseModelSnapshot.cs create mode 100644 AbstractShop/AbstractShopDatabaseImplement/Models/Component.cs create mode 100644 AbstractShop/AbstractShopDatabaseImplement/Models/Order.cs create mode 100644 AbstractShop/AbstractShopDatabaseImplement/Models/Product.cs create mode 100644 AbstractShop/AbstractShopDatabaseImplement/Models/ProductComponent.cs diff --git a/AbstractShop/AbstractShop.sln b/AbstractShop/AbstractShop.sln index 0667114..b07bd87 100644 --- a/AbstractShop/AbstractShop.sln +++ b/AbstractShop/AbstractShop.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractShopBusinessLogic", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractShopFileImplement", "AbstractShopFileImplement\AbstractShopFileImplement.csproj", "{3ECD0091-7E52-48C4-9529-D0BE6D6EF1DE}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractShopDatabaseImplement", "AbstractShopDatabaseImplement\AbstractShopDatabaseImplement.csproj", "{C76CA7A5-B719-4A01-865F-BD6FD679ECCC}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {3ECD0091-7E52-48C4-9529-D0BE6D6EF1DE}.Debug|Any CPU.Build.0 = Debug|Any CPU {3ECD0091-7E52-48C4-9529-D0BE6D6EF1DE}.Release|Any CPU.ActiveCfg = Release|Any CPU {3ECD0091-7E52-48C4-9529-D0BE6D6EF1DE}.Release|Any CPU.Build.0 = Release|Any CPU + {C76CA7A5-B719-4A01-865F-BD6FD679ECCC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C76CA7A5-B719-4A01-865F-BD6FD679ECCC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C76CA7A5-B719-4A01-865F-BD6FD679ECCC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C76CA7A5-B719-4A01-865F-BD6FD679ECCC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/AbstractShop/AbstractShopDatabaseImplement/AbstractShopDatabase.cs b/AbstractShop/AbstractShopDatabaseImplement/AbstractShopDatabase.cs new file mode 100644 index 0000000..7a5a0a3 --- /dev/null +++ b/AbstractShop/AbstractShopDatabaseImplement/AbstractShopDatabase.cs @@ -0,0 +1,25 @@ +using AbstractShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace AbstractShopDatabaseImplement +{ + public class AbstractShopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=CHESHIR\SQLEXPRESS;Initial Catalog=AbstractShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + + public virtual DbSet Components { set; get; } + + public virtual DbSet Products { set; get; } + + public virtual DbSet ProductComponents { set; get; } + + public virtual DbSet Orders { set; get; } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopDatabaseImplement/AbstractShopDatabaseImplement.csproj b/AbstractShop/AbstractShopDatabaseImplement/AbstractShopDatabaseImplement.csproj new file mode 100644 index 0000000..7d2f61b --- /dev/null +++ b/AbstractShop/AbstractShopDatabaseImplement/AbstractShopDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/AbstractShop/AbstractShopDatabaseImplement/Implements/ComponentStorage.cs b/AbstractShop/AbstractShopDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..8086fe9 --- /dev/null +++ b/AbstractShop/AbstractShopDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,84 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.SearchModels; +using AbstractShopContracts.StoragesContracts; +using AbstractShopContracts.ViewModels; +using AbstractShopDatabaseImplement.Models; + +namespace AbstractShopDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new AbstractShopDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new AbstractShopDatabase(); + return context.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + using var context = new AbstractShopDatabase(); + 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) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new AbstractShopDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new AbstractShopDatabase(); + 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 AbstractShopDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopDatabaseImplement/Implements/OrderStorage.cs b/AbstractShop/AbstractShopDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..6473730 --- /dev/null +++ b/AbstractShop/AbstractShopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,40 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.SearchModels; +using AbstractShopContracts.StoragesContracts; +using AbstractShopContracts.ViewModels; + +namespace AbstractShopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + throw new NotImplementedException(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFilteredList(OrderSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFullList() + { + throw new NotImplementedException(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + throw new NotImplementedException(); + } + + public OrderViewModel? Update(OrderBindingModel model) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopDatabaseImplement/Implements/ProductStorage.cs b/AbstractShop/AbstractShopDatabaseImplement/Implements/ProductStorage.cs new file mode 100644 index 0000000..982420a --- /dev/null +++ b/AbstractShop/AbstractShopDatabaseImplement/Implements/ProductStorage.cs @@ -0,0 +1,106 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.SearchModels; +using AbstractShopContracts.StoragesContracts; +using AbstractShopContracts.ViewModels; +using AbstractShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace AbstractShopDatabaseImplement.Implements +{ + public class ProductStorage : IProductStorage + { + public List GetFullList() + { + using var context = new AbstractShopDatabase(); + return context.Products + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.ProductName)) + { + return new(); + } + using var context = new AbstractShopDatabase(); + return context.Products + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.ProductName.Contains(model.ProductName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ProductViewModel? GetElement(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.ProductName) && !model.Id.HasValue) + { + return null; + } + using var context = new AbstractShopDatabase(); + return context.Products + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ProductName) && x.ProductName == model.ProductName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ProductViewModel? Insert(ProductBindingModel model) + { + using var context = new AbstractShopDatabase(); + var newProduct = Product.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Products.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + + public ProductViewModel? Update(ProductBindingModel model) + { + using var context = new AbstractShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Products.FirstOrDefault(rec => rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateComponents(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ProductViewModel? Delete(ProductBindingModel model) + { + using var context = new AbstractShopDatabase(); + var element = context.Products + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Products.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopDatabaseImplement/Migrations/20230224083046_InitMigration.Designer.cs b/AbstractShop/AbstractShopDatabaseImplement/Migrations/20230224083046_InitMigration.Designer.cs new file mode 100644 index 0000000..f18cd53 --- /dev/null +++ b/AbstractShop/AbstractShopDatabaseImplement/Migrations/20230224083046_InitMigration.Designer.cs @@ -0,0 +1,156 @@ +// +using System; +using AbstractShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AbstractShopDatabaseImplement.Migrations +{ + [DbContext(typeof(AbstractShopDatabase))] + [Migration("20230224083046_InitMigration")] + partial class InitMigration + { + /// + 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("AbstractShopDatabaseImplement.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("AbstractShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("AbstractShopDatabaseImplement.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("ProductName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("AbstractShopDatabaseImplement.Models.ProductComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ProductId"); + + b.ToTable("ProductComponents"); + }); + + modelBuilder.Entity("AbstractShopDatabaseImplement.Models.ProductComponent", b => + { + b.HasOne("AbstractShopDatabaseImplement.Models.Component", "Component") + .WithMany("ProductComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AbstractShopDatabaseImplement.Models.Product", "Product") + .WithMany("Components") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("AbstractShopDatabaseImplement.Models.Component", b => + { + b.Navigation("ProductComponents"); + }); + + modelBuilder.Entity("AbstractShopDatabaseImplement.Models.Product", b => + { + b.Navigation("Components"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/AbstractShop/AbstractShopDatabaseImplement/Migrations/20230224083046_InitMigration.cs b/AbstractShop/AbstractShopDatabaseImplement/Migrations/20230224083046_InitMigration.cs new file mode 100644 index 0000000..c356c19 --- /dev/null +++ b/AbstractShop/AbstractShopDatabaseImplement/Migrations/20230224083046_InitMigration.cs @@ -0,0 +1,114 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace AbstractShopDatabaseImplement.Migrations +{ + /// + public partial class InitMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + 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"), + ProductId = 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); + }); + + migrationBuilder.CreateTable( + name: "Products", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ProductName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Products", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ProductComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ProductId = 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_ProductComponents", x => x.Id); + table.ForeignKey( + name: "FK_ProductComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ProductComponents_Products_ProductId", + column: x => x.ProductId, + principalTable: "Products", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ProductComponents_ComponentId", + table: "ProductComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_ProductComponents_ProductId", + table: "ProductComponents", + column: "ProductId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "ProductComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Products"); + } + } +} diff --git a/AbstractShop/AbstractShopDatabaseImplement/Migrations/AbstractShopDatabaseModelSnapshot.cs b/AbstractShop/AbstractShopDatabaseImplement/Migrations/AbstractShopDatabaseModelSnapshot.cs new file mode 100644 index 0000000..91b8616 --- /dev/null +++ b/AbstractShop/AbstractShopDatabaseImplement/Migrations/AbstractShopDatabaseModelSnapshot.cs @@ -0,0 +1,153 @@ +// +using System; +using AbstractShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AbstractShopDatabaseImplement.Migrations +{ + [DbContext(typeof(AbstractShopDatabase))] + partial class AbstractShopDatabaseModelSnapshot : 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("AbstractShopDatabaseImplement.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("AbstractShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("AbstractShopDatabaseImplement.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("ProductName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Products"); + }); + + modelBuilder.Entity("AbstractShopDatabaseImplement.Models.ProductComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ProductId"); + + b.ToTable("ProductComponents"); + }); + + modelBuilder.Entity("AbstractShopDatabaseImplement.Models.ProductComponent", b => + { + b.HasOne("AbstractShopDatabaseImplement.Models.Component", "Component") + .WithMany("ProductComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AbstractShopDatabaseImplement.Models.Product", "Product") + .WithMany("Components") + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Product"); + }); + + modelBuilder.Entity("AbstractShopDatabaseImplement.Models.Component", b => + { + b.Navigation("ProductComponents"); + }); + + modelBuilder.Entity("AbstractShopDatabaseImplement.Models.Product", b => + { + b.Navigation("Components"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/AbstractShop/AbstractShopDatabaseImplement/Models/Component.cs b/AbstractShop/AbstractShopDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..0f53138 --- /dev/null +++ b/AbstractShop/AbstractShopDatabaseImplement/Models/Component.cs @@ -0,0 +1,63 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.ViewModels; +using AbstractShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace AbstractShopDatabaseImplement.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 ProductComponents { 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 + }; + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopDatabaseImplement/Models/Order.cs b/AbstractShop/AbstractShopDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..a423a45 --- /dev/null +++ b/AbstractShop/AbstractShopDatabaseImplement/Models/Order.cs @@ -0,0 +1,38 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.ViewModels; +using AbstractShopDataModels.Enums; +using AbstractShopDataModels.Models; + +namespace AbstractShopDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int ProductId { get; set; } + + public int Count { get; set; } + + public double Sum { get; set; } + + public OrderStatus Status { get; set; } + + public DateTime DateCreate { get; set; } + + public DateTime? DateImplement { get; set; } + + public int Id { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + return new Order(); + } + + public void Update(OrderBindingModel? model) + { + + } + + public OrderViewModel GetViewModel => new() + { + }; + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopDatabaseImplement/Models/Product.cs b/AbstractShop/AbstractShopDatabaseImplement/Models/Product.cs new file mode 100644 index 0000000..f4efed3 --- /dev/null +++ b/AbstractShop/AbstractShopDatabaseImplement/Models/Product.cs @@ -0,0 +1,96 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.ViewModels; +using AbstractShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace AbstractShopDatabaseImplement.Models +{ + public class Product : IProductModel + { + public int Id { get; set; } + + [Required] + public string ProductName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + private Dictionary? _productComponents = null; + + [NotMapped] + public Dictionary ProductComponents + { + get + { + if (_productComponents == null) + { + _productComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _productComponents; + } + } + + [ForeignKey("ProductId")] + public virtual List Components { get; set; } = new(); + + public static Product Create(AbstractShopDatabase context, ProductBindingModel model) + { + return new Product() + { + Id = model.Id, + ProductName = model.ProductName, + Price = model.Price, + Components = model.ProductComponents.Select(x => new ProductComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(ProductBindingModel model) + { + ProductName = model.ProductName; + Price = model.Price; + } + + public ProductViewModel GetViewModel => new() + { + Id = Id, + ProductName = ProductName, + Price = Price, + ProductComponents = ProductComponents + }; + + public void UpdateComponents(AbstractShopDatabase context, ProductBindingModel model) + { + var productComponents = context.ProductComponents.Where(rec => rec.ProductId == model.Id).ToList(); + if (productComponents != null && productComponents.Count > 0) + { // удалили те, которых нет в модели + context.ProductComponents.RemoveRange(productComponents.Where(rec => !model.ProductComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in productComponents) + { + updateComponent.Count = model.ProductComponents[updateComponent.ComponentId].Item2; + model.ProductComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var product = context.Products.First(x => x.Id == Id); + foreach (var pc in model.ProductComponents) + { + context.ProductComponents.Add(new ProductComponent + { + Product = product, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _productComponents = null; + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopDatabaseImplement/Models/ProductComponent.cs b/AbstractShop/AbstractShopDatabaseImplement/Models/ProductComponent.cs new file mode 100644 index 0000000..20db5c6 --- /dev/null +++ b/AbstractShop/AbstractShopDatabaseImplement/Models/ProductComponent.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace AbstractShopDatabaseImplement.Models +{ + public class ProductComponent + { + public int Id { get; set; } + + [Required] + public int ProductId { get; set; } + + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Component Component { get; set; } = new(); + + public virtual Product Product { get; set; } = new(); + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopView/AbstractShopView.csproj b/AbstractShop/AbstractShopView/AbstractShopView.csproj index aaf2d90..33e343c 100644 --- a/AbstractShop/AbstractShopView/AbstractShopView.csproj +++ b/AbstractShop/AbstractShopView/AbstractShopView.csproj @@ -19,6 +19,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -28,6 +32,7 @@ + diff --git a/AbstractShop/AbstractShopView/Program.cs b/AbstractShop/AbstractShopView/Program.cs index 812c020..5fa80bf 100644 --- a/AbstractShop/AbstractShopView/Program.cs +++ b/AbstractShop/AbstractShopView/Program.cs @@ -1,7 +1,7 @@ using AbstractShopBusinessLogic.BusinessLogics; using AbstractShopContracts.BusinessLogicsContracts; using AbstractShopContracts.StoragesContracts; -using AbstractShopFileImplement.Implements; +using AbstractShopDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -- 2.25.1