diff --git a/GiftShop/GiftShop.sln b/GiftShop/GiftShop.sln index 839dbe5..0452220 100644 --- a/GiftShop/GiftShop.sln +++ b/GiftShop/GiftShop.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GiftShopListImplement", "Gi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GiftShopBusinessLogic", "GiftShopBusinessLogic\GiftShopBusinessLogic.csproj", "{07C14020-5905-4CCC-9DAC-53507C8F28F8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GiftShopFileImplement", "GiftShopFileImplement\GiftShopFileImplement.csproj", "{05AE2E86-5464-4B1F-BE91-60FA43CE69F1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GiftShopFileImplement", "GiftShopFileImplement\GiftShopFileImplement.csproj", "{05AE2E86-5464-4B1F-BE91-60FA43CE69F1}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GiftShopDatabaseImplement", "GiftShopDatabaseImplement\GiftShopDatabaseImplement.csproj", "{50B7E5D7-A58E-4E36-8A5C-F7E78BB526B0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {05AE2E86-5464-4B1F-BE91-60FA43CE69F1}.Debug|Any CPU.Build.0 = Debug|Any CPU {05AE2E86-5464-4B1F-BE91-60FA43CE69F1}.Release|Any CPU.ActiveCfg = Release|Any CPU {05AE2E86-5464-4B1F-BE91-60FA43CE69F1}.Release|Any CPU.Build.0 = Release|Any CPU + {50B7E5D7-A58E-4E36-8A5C-F7E78BB526B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {50B7E5D7-A58E-4E36-8A5C-F7E78BB526B0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {50B7E5D7-A58E-4E36-8A5C-F7E78BB526B0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {50B7E5D7-A58E-4E36-8A5C-F7E78BB526B0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/GiftShop/GiftShopDatabaseImplement/GiftShopDatabase.cs b/GiftShop/GiftShopDatabaseImplement/GiftShopDatabase.cs new file mode 100644 index 0000000..31f57f0 --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/GiftShopDatabase.cs @@ -0,0 +1,25 @@ +using GiftShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace GiftShopDatabaseImplement +{ + public class GiftShopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=.\SQLEXPRESS;Initial Catalog=GiftShopDatabase;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..81ac990 --- /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..dce0012 --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,85 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.SearchModels; +using GiftShopContracts.StoragesContracts; +using GiftShopContracts.ViewModels; +using GiftShopDatabaseImplement.Models; + +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..437bfba --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Implements/GiftStorage.cs @@ -0,0 +1,107 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.SearchModels; +using GiftShopContracts.StoragesContracts; +using GiftShopContracts.ViewModels; +using GiftShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +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..b602b71 --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,99 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.SearchModels; +using GiftShopContracts.StoragesContracts; +using GiftShopContracts.ViewModels; +using GiftShopDatabaseImplement.Models; + +namespace GiftShopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new GiftShopDatabase(); + var element = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + context.Orders.Remove(element); + context.SaveChanges(); + return AcessDressesStorage(element.GetViewModel, context); + } + + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new GiftShopDatabase(); + return AcessDressesStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel, context); + } + + 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 => AcessDressesStorage(x.GetViewModel, context)).ToList(); + } + + public List GetFullList() + { + using var context = new GiftShopDatabase(); + return context.Orders.Select(x => AcessDressesStorage(x.GetViewModel, context)).ToList(); + } + + + public OrderViewModel? Insert(OrderBindingModel model) + { + var order = Order.Create(model); + if (order == null) + { + return null; + } + using var context = new GiftShopDatabase(); + context.Orders.Add(order); + context.SaveChanges(); + return AcessDressesStorage(order.GetViewModel, context); + + } + + + 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 AcessDressesStorage(order.GetViewModel, context); + + } + + public static OrderViewModel? AcessDressesStorage(OrderViewModel? model, GiftShopDatabase context) + { + if (model == null) + { + return null; + } + var dress = context.Gifts.FirstOrDefault(x => x.Id == model.GiftId); + if (dress == null) + { + return null; + } + model.GiftName = dress.GiftName; + return model; + } + + } +} diff --git a/GiftShop/GiftShopDatabaseImplement/Migrations/20230402200549_InitMigration.Designer.cs b/GiftShop/GiftShopDatabaseImplement/Migrations/20230402200549_InitMigration.Designer.cs new file mode 100644 index 0000000..b7161ad --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Migrations/20230402200549_InitMigration.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("20230402200549_InitMigration")] + partial class InitMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .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/20230402200549_InitMigration.cs b/GiftShop/GiftShopDatabaseImplement/Migrations/20230402200549_InitMigration.cs new file mode 100644 index 0000000..e9e2fec --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Migrations/20230402200549_InitMigration.cs @@ -0,0 +1,125 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace GiftShopDatabaseImplement.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: "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/20240502130547_test.Designer.cs b/GiftShop/GiftShopDatabaseImplement/Migrations/20240502130547_test.Designer.cs new file mode 100644 index 0000000..b37ff91 --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Migrations/20240502130547_test.Designer.cs @@ -0,0 +1,171 @@ +// +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("20240502130547_test")] + partial class test + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .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("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/20240502130547_test.cs b/GiftShop/GiftShopDatabaseImplement/Migrations/20240502130547_test.cs new file mode 100644 index 0000000..de7a39c --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Migrations/20240502130547_test.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace GiftShopDatabaseImplement.Migrations +{ + /// + public partial class test : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "GiftName", + table: "Orders"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "GiftName", + table: "Orders", + type: "nvarchar(max)", + nullable: false, + defaultValue: ""); + } + } +} diff --git a/GiftShop/GiftShopDatabaseImplement/Migrations/GiftShopDatabaseModelSnapshot.cs b/GiftShop/GiftShopDatabaseImplement/Migrations/GiftShopDatabaseModelSnapshot.cs new file mode 100644 index 0000000..5b22674 --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Migrations/GiftShopDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +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.4") + .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("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..20022c7 --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Models/Component.cs @@ -0,0 +1,63 @@ +using GiftShopDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using GiftShopContracts.ViewModels; +using GiftShopContracts.BindingModels; + +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..388cf2f --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Models/Gift.cs @@ -0,0 +1,99 @@ +using GiftShopDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +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..95ae286 --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Models/GiftComponent.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +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..153528f --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Models/Order.cs @@ -0,0 +1,71 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.ViewModels; +using GiftShopDataModels.Enums; +using GiftShopDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace GiftShopDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + public int GiftId { 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 Gift Gift { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + + return new Order() + { + Id = model.Id, + GiftId = model.GiftId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + GiftId = GiftId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} diff --git a/GiftShop/GiftShopFileImplement/Implements/ComponentStorage.cs b/GiftShop/GiftShopFileImplement/Implements/ComponentStorage.cs index f915a4e..6b76fd3 100644 --- a/GiftShop/GiftShopFileImplement/Implements/ComponentStorage.cs +++ b/GiftShop/GiftShopFileImplement/Implements/ComponentStorage.cs @@ -30,7 +30,8 @@ namespace GiftShopFileImplement.Implements } return source.Components .Where(x => x.ComponentName.Contains(model.ComponentName)) - .Select(x => x.GetViewModel).ToList(); + .Select(x => x.GetViewModel) + .ToList(); } public ComponentViewModel? GetElement(ComponentSearchModel model) diff --git a/GiftShop/GiftShopFileImplement/Implements/OrderStorage.cs b/GiftShop/GiftShopFileImplement/Implements/OrderStorage.cs index 3a3e103..784fad4 100644 --- a/GiftShop/GiftShopFileImplement/Implements/OrderStorage.cs +++ b/GiftShop/GiftShopFileImplement/Implements/OrderStorage.cs @@ -40,21 +40,23 @@ namespace GiftShopFileImplement.Implements return source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } + + public List GetFilteredList(OrderSearchModel model) { - if (!model.Id.HasValue) - { - return new(); - } - - return source.Orders.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + return source.Orders.Where(x => x.Id == model.Id) + .Select(x => AcessDressesStorage(x.GetViewModel)) + .ToList(); } + public List GetFullList() { - return source.Orders.Select(x => x.GetViewModel).ToList(); + return source.Orders.Select(x => AcessDressesStorage(x.GetViewModel)).ToList(); } + + public OrderViewModel? Insert(OrderBindingModel model) { model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; @@ -85,5 +87,12 @@ namespace GiftShopFileImplement.Implements return order.GetViewModel; } + + public OrderViewModel AcessDressesStorage(OrderViewModel model) + { + var component = source.Gifts.FirstOrDefault(x => x.Id == model.GiftId); + model.GiftName = component.GiftName; + return model; + } } } diff --git a/GiftShop/GiftShopFileImplement/Models/Component.cs b/GiftShop/GiftShopFileImplement/Models/Component.cs index df89d8e..7197228 100644 --- a/GiftShop/GiftShopFileImplement/Models/Component.cs +++ b/GiftShop/GiftShopFileImplement/Models/Component.cs @@ -7,7 +7,7 @@ namespace GiftShopFileImplement.Models { internal class Component : IComponentModel { - public int Id { get; private set; } // 3 + public int Id { get; private set; } public string ComponentName { get; private set; } = string.Empty; diff --git a/GiftShop/GiftShopFileImplement/Models/Gift.cs b/GiftShop/GiftShopFileImplement/Models/Gift.cs index a77ffa5..34f3373 100644 --- a/GiftShop/GiftShopFileImplement/Models/Gift.cs +++ b/GiftShop/GiftShopFileImplement/Models/Gift.cs @@ -89,6 +89,7 @@ namespace GiftShopFileImplement.Models new XElement("Price", Price.ToString()), new XElement("GiftComponents", Components.Select(x => new XElement("GiftComponent", new XElement("Key", x.Key), - new XElement("Value", x.Value))).ToArray())); + new XElement("Value", x.Value))) + .ToArray())); } } diff --git a/GiftShop/GiftShopFileImplement/Models/Order.cs b/GiftShop/GiftShopFileImplement/Models/Order.cs index d617dda..b44f984 100644 --- a/GiftShop/GiftShopFileImplement/Models/Order.cs +++ b/GiftShop/GiftShopFileImplement/Models/Order.cs @@ -10,8 +10,6 @@ namespace GiftShopFileImplement.Models { public int GiftId { get; private set; } - public string GiftName { get; private set; } - public int Count { get; private set; } public double Sum { get; private set; } @@ -34,7 +32,6 @@ namespace GiftShopFileImplement.Models { Id = model.Id, GiftId = model.GiftId, - GiftName = model.GiftName, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -53,7 +50,6 @@ namespace GiftShopFileImplement.Models { Id = Convert.ToInt32(element.Attribute("Id")!.Value), GiftId = Convert.ToInt32(element.Element("GiftId")!.Value), - GiftName = element.Element("GiftName")!.Value, Count = Convert.ToInt32(element.Element("Count")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), @@ -72,7 +68,11 @@ namespace GiftShopFileImplement.Models { return; } + GiftId = model.GiftId; + Count = model.Count; + Sum = model.Sum; Status = model.Status; + DateCreate = model.DateCreate; DateImplement = model.DateImplement; } @@ -80,7 +80,6 @@ namespace GiftShopFileImplement.Models { Id = Id, GiftId = GiftId, - GiftName = GiftName, Count = Count, Sum = Sum, Status = Status, @@ -90,7 +89,6 @@ namespace GiftShopFileImplement.Models public XElement GetXElement => new("Order", new XAttribute("Id", Id), - new XElement("GiftName", GiftName), new XElement("GiftId", GiftId.ToString()), new XElement("Count", Count.ToString()), new XElement("Sum", Sum.ToString()), diff --git a/GiftShop/GiftShopListImplement/Implements/OrderStorage.cs b/GiftShop/GiftShopListImplement/Implements/OrderStorage.cs index d415fa8..ab6966a 100644 --- a/GiftShop/GiftShopListImplement/Implements/OrderStorage.cs +++ b/GiftShop/GiftShopListImplement/Implements/OrderStorage.cs @@ -50,31 +50,23 @@ namespace GiftShopListImplement.Implements { var result = new List(); - if (!model.Id.HasValue) - { - return result; - } - foreach (var order in _source.Orders) { - if (model.Id.HasValue && order.Id == model.Id) + if (order.Id == model.Id) { - result.Add(order.GetViewModel); + result.Add(AcessGiftsStorage(order.GetViewModel)); } } - return result; } public List GetFullList() { var result = new List(); - foreach (var order in _source.Orders) { - result.Add(order.GetViewModel); + result.Add(AcessGiftsStorage(order.GetViewModel)); } - return result; } @@ -115,5 +107,18 @@ namespace GiftShopListImplement.Implements return null; } + + public OrderViewModel AcessGiftsStorage(OrderViewModel model) + { + foreach (var gifts in _source.Gifts) + { + if (gifts.Id == model.GiftId) + { + model.GiftName = gifts.GiftName; + break; + } + } + return model; + } } } diff --git a/GiftShop/GiftShopView/GiftShopView.csproj b/GiftShop/GiftShopView/GiftShopView.csproj index 853b7e5..4ae91ba 100644 --- a/GiftShop/GiftShopView/GiftShopView.csproj +++ b/GiftShop/GiftShopView/GiftShopView.csproj @@ -19,6 +19,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -28,6 +32,7 @@ + diff --git a/GiftShop/GiftShopView/Program.cs b/GiftShop/GiftShopView/Program.cs index 93b4fdf..22a4ae7 100644 --- a/GiftShop/GiftShopView/Program.cs +++ b/GiftShop/GiftShopView/Program.cs @@ -1,7 +1,7 @@ using GiftShopBusinessLogic.BusinessLogics; using GiftShopContracts.BusinessLogicsContracts; using GiftShopContracts.StoragesContracts; -using GiftShopFileImplement.Implements; +using GiftShopDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging;