From 0c86149377549cb41f289051a6e94c9075887c76 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 4 Jun 2023 03:46:48 +0400 Subject: [PATCH] lab3complete --- GiftShop/GiftShop.sln | 8 +- GiftShop/GiftShop/GiftShopView.csproj | 5 + GiftShop/GiftShop/Program.cs | 3 +- .../GiftShopDatabase.cs | 28 +++ .../GiftShopDatabaseImplement.csproj | 23 +++ .../Implements/ComponentStorage.cs | 89 +++++++++ .../Implements/GiftStorage.cs | 111 +++++++++++ .../Implements/OrderStorage.cs | 81 ++++++++ .../20230307085317_InitialCreate.Designer.cs | 175 ++++++++++++++++++ .../20230307085317_InitialCreate.cs | 126 +++++++++++++ .../GiftShopDatabaseModelSnapshot.cs | 172 +++++++++++++++++ .../Models/Component.cs | 62 +++++++ .../GiftShopDatabaseImplement/Models/Gift.cs | 98 ++++++++++ .../Models/GiftComponent.cs | 22 +++ .../GiftShopDatabaseImplement/Models/Order.cs | 86 +++++++++ 15 files changed, 1086 insertions(+), 3 deletions(-) create mode 100644 GiftShop/GiftShopDatabaseImplement/GiftShopDatabase.cs create mode 100644 GiftShop/GiftShopDatabaseImplement/GiftShopDatabaseImplement.csproj create mode 100644 GiftShop/GiftShopDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 GiftShop/GiftShopDatabaseImplement/Implements/GiftStorage.cs create mode 100644 GiftShop/GiftShopDatabaseImplement/Implements/OrderStorage.cs create mode 100644 GiftShop/GiftShopDatabaseImplement/Migrations/20230307085317_InitialCreate.Designer.cs create mode 100644 GiftShop/GiftShopDatabaseImplement/Migrations/20230307085317_InitialCreate.cs create mode 100644 GiftShop/GiftShopDatabaseImplement/Migrations/GiftShopDatabaseModelSnapshot.cs create mode 100644 GiftShop/GiftShopDatabaseImplement/Models/Component.cs create mode 100644 GiftShop/GiftShopDatabaseImplement/Models/Gift.cs create mode 100644 GiftShop/GiftShopDatabaseImplement/Models/GiftComponent.cs create mode 100644 GiftShop/GiftShopDatabaseImplement/Models/Order.cs diff --git a/GiftShop/GiftShop.sln b/GiftShop/GiftShop.sln index 12e319d..e6c80ac 100644 --- a/GiftShop/GiftShop.sln +++ b/GiftShop/GiftShop.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GiftShopBusinessLogic", "Gi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GiftShopListImplement", "GiftShopListImplement\GiftShopListImplement.csproj", "{D3518048-9FEF-41E6-A2A7-80C9BF4A3D04}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GiftShopFileImplement", "GiftShopFileImplement\GiftShopFileImplement.csproj", "{1F2E45B0-3080-4344-BE76-C2B896866DFA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GiftShopFileImplement", "GiftShopFileImplement\GiftShopFileImplement.csproj", "{1F2E45B0-3080-4344-BE76-C2B896866DFA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GiftShopDatabaseImplement", "GiftShopDatabaseImplement\GiftShopDatabaseImplement.csproj", "{2C31DE8F-E8B8-466E-9D9E-F16CF69245BF}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {1F2E45B0-3080-4344-BE76-C2B896866DFA}.Debug|Any CPU.Build.0 = Debug|Any CPU {1F2E45B0-3080-4344-BE76-C2B896866DFA}.Release|Any CPU.ActiveCfg = Release|Any CPU {1F2E45B0-3080-4344-BE76-C2B896866DFA}.Release|Any CPU.Build.0 = Release|Any CPU + {2C31DE8F-E8B8-466E-9D9E-F16CF69245BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2C31DE8F-E8B8-466E-9D9E-F16CF69245BF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2C31DE8F-E8B8-466E-9D9E-F16CF69245BF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2C31DE8F-E8B8-466E-9D9E-F16CF69245BF}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/GiftShop/GiftShop/GiftShopView.csproj b/GiftShop/GiftShop/GiftShopView.csproj index 63bae46..460c2f2 100644 --- a/GiftShop/GiftShop/GiftShopView.csproj +++ b/GiftShop/GiftShop/GiftShopView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -17,6 +21,7 @@ + diff --git a/GiftShop/GiftShop/Program.cs b/GiftShop/GiftShop/Program.cs index 9a54075..39142c7 100644 --- a/GiftShop/GiftShop/Program.cs +++ b/GiftShop/GiftShop/Program.cs @@ -2,11 +2,10 @@ using Microsoft.Extensions.DependencyInjection; using GiftShopContracts.BusinessLogicsContracts; using GiftShopContracts.StoragesContracts; -using GiftShopFileImplement.Implements; +using GiftShopDatabaseImplement.Implements; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; using GiftShopBusinessLogic.BusinessLogics; -using System.Drawing; namespace GiftShopView { diff --git a/GiftShop/GiftShopDatabaseImplement/GiftShopDatabase.cs b/GiftShop/GiftShopDatabaseImplement/GiftShopDatabase.cs new file mode 100644 index 0000000..d5b272d --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/GiftShopDatabase.cs @@ -0,0 +1,28 @@ +using GiftShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopDatabaseImplement +{ + public class GiftShopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder + optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-0BJHUJC\SQLEXPRESS;Initial Catalog=GiftShopDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Gifts { set; get; } + public virtual DbSet GiftComponents { set; get; } + public virtual DbSet Orders { set; get; } + } + +} diff --git a/GiftShop/GiftShopDatabaseImplement/GiftShopDatabaseImplement.csproj b/GiftShop/GiftShopDatabaseImplement/GiftShopDatabaseImplement.csproj new file mode 100644 index 0000000..ae2647e --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/GiftShopDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/GiftShop/GiftShopDatabaseImplement/Implements/ComponentStorage.cs b/GiftShop/GiftShopDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..3ae524a --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,89 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.SearchModels; +using GiftShopContracts.StoragesContracts; +using GiftShopContracts.ViewModels; +using GiftShopDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new GiftShopDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel + model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new GiftShopDatabase(); + 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 GiftShopDatabase(); + 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 GiftShopDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new GiftShopDatabase(); + 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 GiftShopDatabase(); + 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/GiftShop/GiftShopDatabaseImplement/Implements/GiftStorage.cs b/GiftShop/GiftShopDatabaseImplement/Implements/GiftStorage.cs new file mode 100644 index 0000000..a370774 --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Implements/GiftStorage.cs @@ -0,0 +1,111 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.SearchModels; +using GiftShopContracts.StoragesContracts; +using GiftShopContracts.ViewModels; +using GiftShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopDatabaseImplement.Implements +{ + public class GiftStorage : IGiftStorage + { + public List GetFullList() + { + using var context = new GiftShopDatabase(); + return context.Gifts + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(GiftSearchModel model) + { + if (string.IsNullOrEmpty(model.GiftName)) + { + return new(); + } + using var context = new GiftShopDatabase(); + return context.Gifts + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.GiftName.Contains(model.GiftName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public GiftViewModel? GetElement(GiftSearchModel model) + { + if (string.IsNullOrEmpty(model.GiftName) && + !model.Id.HasValue) + { + return null; + } + using var context = new GiftShopDatabase(); + return context.Gifts + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.GiftName) && + x.GiftName == model.GiftName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public GiftViewModel? Insert(GiftBindingModel model) + { + using var context = new GiftShopDatabase(); + var newGift = Gift.Create(context, model); + if (newGift == null) + { + return null; + } + context.Gifts.Add(newGift); + context.SaveChanges(); + return newGift.GetViewModel; + } + public GiftViewModel? Update(GiftBindingModel model) + { + using var context = new GiftShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var gift = context.Gifts.FirstOrDefault(rec => + rec.Id == model.Id); + if (gift == null) + { + return null; + } + gift.Update(model); + context.SaveChanges(); + gift.UpdateComponents(context, model); + transaction.Commit(); + return gift.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public GiftViewModel? Delete(GiftBindingModel model) + { + using var context = new GiftShopDatabase(); + var element = context.Gifts + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Gifts.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } + +} diff --git a/GiftShop/GiftShopDatabaseImplement/Implements/OrderStorage.cs b/GiftShop/GiftShopDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..da59775 --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,81 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.SearchModels; +using GiftShopContracts.StoragesContracts; +using GiftShopContracts.ViewModels; +using GiftShopDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new GiftShopDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new GiftShopDatabase(); + return context.Orders.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 GiftShopDatabase(); + return context.Orders.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new GiftShopDatabase(); + return context.Orders.Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new GiftShopDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new GiftShopDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + } +} diff --git a/GiftShop/GiftShopDatabaseImplement/Migrations/20230307085317_InitialCreate.Designer.cs b/GiftShop/GiftShopDatabaseImplement/Migrations/20230307085317_InitialCreate.Designer.cs new file mode 100644 index 0000000..d70fbf6 --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Migrations/20230307085317_InitialCreate.Designer.cs @@ -0,0 +1,175 @@ +// +using System; +using GiftShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace GiftShopDatabaseImplement.Migrations +{ + [DbContext(typeof(GiftShopDatabase))] + [Migration("20230307085317_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("GiftShopDatabaseImplement.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("GiftShopDatabaseImplement.Models.Gift", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("GiftName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Gifts"); + }); + + modelBuilder.Entity("GiftShopDatabaseImplement.Models.GiftComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("GiftId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("GiftId"); + + b.ToTable("GiftComponents"); + }); + + modelBuilder.Entity("GiftShopDatabaseImplement.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("GiftId") + .HasColumnType("int"); + + b.Property("GiftName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("GiftId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("GiftShopDatabaseImplement.Models.GiftComponent", b => + { + b.HasOne("GiftShopDatabaseImplement.Models.Component", "Component") + .WithMany("GiftComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("GiftShopDatabaseImplement.Models.Gift", "Gift") + .WithMany("Components") + .HasForeignKey("GiftId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Gift"); + }); + + modelBuilder.Entity("GiftShopDatabaseImplement.Models.Order", b => + { + b.HasOne("GiftShopDatabaseImplement.Models.Gift", "Gift") + .WithMany("Orders") + .HasForeignKey("GiftId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Gift"); + }); + + modelBuilder.Entity("GiftShopDatabaseImplement.Models.Component", b => + { + b.Navigation("GiftComponents"); + }); + + modelBuilder.Entity("GiftShopDatabaseImplement.Models.Gift", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/GiftShop/GiftShopDatabaseImplement/Migrations/20230307085317_InitialCreate.cs b/GiftShop/GiftShopDatabaseImplement/Migrations/20230307085317_InitialCreate.cs new file mode 100644 index 0000000..9d7a396 --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Migrations/20230307085317_InitialCreate.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace GiftShopDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : 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: "Gifts", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + GiftName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Gifts", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "GiftComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + GiftId = 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_GiftComponents", x => x.Id); + table.ForeignKey( + name: "FK_GiftComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_GiftComponents_Gifts_GiftId", + column: x => x.GiftId, + principalTable: "Gifts", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + GiftId = table.Column(type: "int", nullable: false), + GiftName = table.Column(type: "nvarchar(max)", 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_Gifts_GiftId", + column: x => x.GiftId, + principalTable: "Gifts", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_GiftComponents_ComponentId", + table: "GiftComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_GiftComponents_GiftId", + table: "GiftComponents", + column: "GiftId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_GiftId", + table: "Orders", + column: "GiftId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "GiftComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Gifts"); + } + } +} diff --git a/GiftShop/GiftShopDatabaseImplement/Migrations/GiftShopDatabaseModelSnapshot.cs b/GiftShop/GiftShopDatabaseImplement/Migrations/GiftShopDatabaseModelSnapshot.cs new file mode 100644 index 0000000..70c50d1 --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Migrations/GiftShopDatabaseModelSnapshot.cs @@ -0,0 +1,172 @@ +// +using System; +using GiftShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace GiftShopDatabaseImplement.Migrations +{ + [DbContext(typeof(GiftShopDatabase))] + partial class GiftShopDatabaseModelSnapshot : 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("GiftShopDatabaseImplement.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("GiftShopDatabaseImplement.Models.Gift", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("GiftName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Gifts"); + }); + + modelBuilder.Entity("GiftShopDatabaseImplement.Models.GiftComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("GiftId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("GiftId"); + + b.ToTable("GiftComponents"); + }); + + modelBuilder.Entity("GiftShopDatabaseImplement.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("GiftId") + .HasColumnType("int"); + + b.Property("GiftName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("GiftId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("GiftShopDatabaseImplement.Models.GiftComponent", b => + { + b.HasOne("GiftShopDatabaseImplement.Models.Component", "Component") + .WithMany("GiftComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("GiftShopDatabaseImplement.Models.Gift", "Gift") + .WithMany("Components") + .HasForeignKey("GiftId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Gift"); + }); + + modelBuilder.Entity("GiftShopDatabaseImplement.Models.Order", b => + { + b.HasOne("GiftShopDatabaseImplement.Models.Gift", "Gift") + .WithMany("Orders") + .HasForeignKey("GiftId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Gift"); + }); + + modelBuilder.Entity("GiftShopDatabaseImplement.Models.Component", b => + { + b.Navigation("GiftComponents"); + }); + + modelBuilder.Entity("GiftShopDatabaseImplement.Models.Gift", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/GiftShop/GiftShopDatabaseImplement/Models/Component.cs b/GiftShop/GiftShopDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..80c113e --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Models/Component.cs @@ -0,0 +1,62 @@ +using GiftShopContracts.BindingModels; +using GiftShopDataModels.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 GiftShopContracts.ViewModels; + +namespace GiftShopDatabaseImplement.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 GiftComponents { 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/GiftShop/GiftShopDatabaseImplement/Models/Gift.cs b/GiftShop/GiftShopDatabaseImplement/Models/Gift.cs new file mode 100644 index 0000000..03d0ef8 --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Models/Gift.cs @@ -0,0 +1,98 @@ +using GiftShopDataModels.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 GiftShopContracts.BindingModels; +using GiftShopContracts.ViewModels; + +namespace GiftShopDatabaseImplement.Models +{ + public class Gift : IGiftModel + { + public int Id { get; set; } + [Required] + public string GiftName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _giftComponents = null; + [NotMapped] + public Dictionary GiftComponents + { + get + { + if (_giftComponents == null) + { + _giftComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _giftComponents; + } + } + [ForeignKey("GiftId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("GiftId")] + public virtual List Orders { get; set; } = new(); + public static Gift Create(GiftShopDatabase context, GiftBindingModel model) + { + return new Gift() + { + Id = model.Id, + GiftName = model.GiftName, + Price = model.Price, + Components = model.GiftComponents.Select(x => new + GiftComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(GiftBindingModel model) + { + GiftName = model.GiftName; + Price = model.Price; + } + public GiftViewModel GetViewModel => new() + { + Id = Id, + GiftName = GiftName, + Price = Price, + GiftComponents = GiftComponents + }; + public void UpdateComponents(GiftShopDatabase context, GiftBindingModel model) + { + var giftComponents = context.GiftComponents.Where(rec => + rec.GiftId == model.Id).ToList(); + if (giftComponents != null && giftComponents.Count > 0) + { // удалили те, которых нет в модели + context.GiftComponents.RemoveRange(giftComponents.Where(rec + => !model.GiftComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in giftComponents) + { + updateComponent.Count = model.GiftComponents[updateComponent.ComponentId].Item2; + model.GiftComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var gift = context.Gifts.First(x => x.Id == Id); + foreach (var pc in model.GiftComponents) + { + context.GiftComponents.Add(new GiftComponent + { + Gift = gift, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _giftComponents = null; + } + } +} diff --git a/GiftShop/GiftShopDatabaseImplement/Models/GiftComponent.cs b/GiftShop/GiftShopDatabaseImplement/Models/GiftComponent.cs new file mode 100644 index 0000000..6c02679 --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Models/GiftComponent.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopDatabaseImplement.Models +{ + public class GiftComponent + { + public int Id { get; set; } + [Required] + public int GiftId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Gift Gift { get; set; } = new(); + } +} diff --git a/GiftShop/GiftShopDatabaseImplement/Models/Order.cs b/GiftShop/GiftShopDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..d011f6d --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Models/Order.cs @@ -0,0 +1,86 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.ViewModels; +using GiftShopDataModels.Enums; +using GiftShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + public int GiftId { get; private set; } + + public string GiftName { get; private set; } = string.Empty; + + [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 Gift Gift { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + + return new Order() + { + Id = model.Id, + GiftId = model.GiftId, + GiftName = model.GiftName, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + + GiftId = model.GiftId; + GiftName = model.GiftName; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + GiftId = GiftId, + GiftName = GiftName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +}