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..556bb99 --- /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=LAPTOP-43DJMKMK\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..e3e5174 --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,76 @@ +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(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/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/GiftShopDatabaseModelSnapshot.cs b/GiftShop/GiftShopDatabaseImplement/Migrations/GiftShopDatabaseModelSnapshot.cs new file mode 100644 index 0000000..b0f77ae --- /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.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/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..041e83d --- /dev/null +++ b/GiftShop/GiftShopDatabaseImplement/Models/Order.cs @@ -0,0 +1,81 @@ +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; } + + 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 + }; + } +} 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;