From 6b22bbd0ac5757af80bc3ca04bcac5c5be78b4a0 Mon Sep 17 00:00:00 2001 From: DavidMakarov Date: Sat, 9 Mar 2024 17:32:17 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FlowerShop/FlowerShop.sln | 8 +- FlowerShop/FlowerShop/FlowerShopView.csproj | 5 + FlowerShop/FlowerShop/Program.cs | 2 +- .../FlowerShopDatabase.cs | 21 +++ .../FlowerShopDatabaseImplement.csproj | 23 +++ .../Implements/ComponentStorage.cs | 80 +++++++++ .../Implements/FlowerStorage.cs | 105 +++++++++++ .../Implements/OrderStorage.cs | 93 ++++++++++ .../20240308215929_InitialCreate.Designer.cs | 169 ++++++++++++++++++ .../20240308215929_InitialCreate.cs | 122 +++++++++++++ .../FlowerShopDatabaseModelSnapshot.cs | 167 +++++++++++++++++ .../Models/Component.cs | 57 ++++++ .../Models/Flower.cs | 91 ++++++++++ .../Models/FlowerComponent.cs | 18 ++ .../Models/Order.cs | 59 ++++++ 15 files changed, 1018 insertions(+), 2 deletions(-) create mode 100644 FlowerShop/FlowerShopDatabaseImplement/FlowerShopDatabase.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/FlowerShopDatabaseImplement.csproj create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Implements/FlowerStorage.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Implements/OrderStorage.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Migrations/20240308215929_InitialCreate.Designer.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Migrations/20240308215929_InitialCreate.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Migrations/FlowerShopDatabaseModelSnapshot.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Models/Component.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Models/Flower.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Models/FlowerComponent.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Models/Order.cs diff --git a/FlowerShop/FlowerShop.sln b/FlowerShop/FlowerShop.sln index cf737f3..f83a975 100644 --- a/FlowerShop/FlowerShop.sln +++ b/FlowerShop/FlowerShop.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopBusinessLogic", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopListImplement", "FlowerShopListImplement\FlowerShopListImplement.csproj", "{8B6B6E6D-442B-46F0-8F60-E3D91E8D0AB5}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlowerShopFileImplement", "FlowerShopFileImplement\FlowerShopFileImplement.csproj", "{239BA98B-C7E4-4434-9A18-6EE4F40071E6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopFileImplement", "FlowerShopFileImplement\FlowerShopFileImplement.csproj", "{239BA98B-C7E4-4434-9A18-6EE4F40071E6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlowerShopDatabaseImplement", "FlowerShopDatabaseImplement\FlowerShopDatabaseImplement.csproj", "{07E0542C-0ED4-4F69-B72D-B51EFF80C599}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {239BA98B-C7E4-4434-9A18-6EE4F40071E6}.Debug|Any CPU.Build.0 = Debug|Any CPU {239BA98B-C7E4-4434-9A18-6EE4F40071E6}.Release|Any CPU.ActiveCfg = Release|Any CPU {239BA98B-C7E4-4434-9A18-6EE4F40071E6}.Release|Any CPU.Build.0 = Release|Any CPU + {07E0542C-0ED4-4F69-B72D-B51EFF80C599}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {07E0542C-0ED4-4F69-B72D-B51EFF80C599}.Debug|Any CPU.Build.0 = Debug|Any CPU + {07E0542C-0ED4-4F69-B72D-B51EFF80C599}.Release|Any CPU.ActiveCfg = Release|Any CPU + {07E0542C-0ED4-4F69-B72D-B51EFF80C599}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FlowerShop/FlowerShop/FlowerShopView.csproj b/FlowerShop/FlowerShop/FlowerShopView.csproj index 9361e1a..83f9c52 100644 --- a/FlowerShop/FlowerShop/FlowerShopView.csproj +++ b/FlowerShop/FlowerShop/FlowerShopView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -17,6 +21,7 @@ + diff --git a/FlowerShop/FlowerShop/Program.cs b/FlowerShop/FlowerShop/Program.cs index f2d2595..82130e5 100644 --- a/FlowerShop/FlowerShop/Program.cs +++ b/FlowerShop/FlowerShop/Program.cs @@ -1,7 +1,7 @@ using FlowerShopBusinessLogic.BusinessLogics; using FlowerShopContracts.BusinessLogicsContracts; using FlowerShopContracts.StoragesContracts; -using FlowerShopFileImplement.Implements; +using FlowerShopDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/FlowerShop/FlowerShopDatabaseImplement/FlowerShopDatabase.cs b/FlowerShop/FlowerShopDatabaseImplement/FlowerShopDatabase.cs new file mode 100644 index 0000000..c92b773 --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/FlowerShopDatabase.cs @@ -0,0 +1,21 @@ +using FlowerShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace FlowerShopDatabaseImplement +{ + public class FlowerShopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=WHITEBEAR\SQLEXPRESS;Initial Catalog=FlowerShopDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Flowers { set; get; } + public virtual DbSet FlowerComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/FlowerShopDatabaseImplement.csproj b/FlowerShop/FlowerShopDatabaseImplement/FlowerShopDatabaseImplement.csproj new file mode 100644 index 0000000..7ecbc47 --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/FlowerShopDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/FlowerShop/FlowerShopDatabaseImplement/Implements/ComponentStorage.cs b/FlowerShop/FlowerShopDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..ede8b89 --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,80 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopDatabaseImplement.Models; + +namespace FlowerShopDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new FlowerShopDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel + model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new FlowerShopDatabase(); + 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 FlowerShopDatabase(); + 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 FlowerShopDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new FlowerShopDatabase(); + 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 FlowerShopDatabase(); + 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/FlowerShop/FlowerShopDatabaseImplement/Implements/FlowerStorage.cs b/FlowerShop/FlowerShopDatabaseImplement/Implements/FlowerStorage.cs new file mode 100644 index 0000000..668e5f5 --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Implements/FlowerStorage.cs @@ -0,0 +1,105 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace FlowerShopDatabaseImplement.Implements +{ + public class FlowerStorage : IFlowerStorage + { + public List GetFullList() + { + using var context = new FlowerShopDatabase(); + return context.Flowers + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(FlowerSearchModel model) + { + if (string.IsNullOrEmpty(model.FlowerName)) + { + return new(); + } + using var context = new FlowerShopDatabase(); + return context.Flowers + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.FlowerName.Contains(model.FlowerName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public FlowerViewModel? GetElement(FlowerSearchModel model) + { + if (string.IsNullOrEmpty(model.FlowerName) && + !model.Id.HasValue) + { + return null; + } + using var context = new FlowerShopDatabase(); + return context.Flowers + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.FlowerName) && + x.FlowerName == model.FlowerName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public FlowerViewModel? Insert(FlowerBindingModel model) + { + using var context = new FlowerShopDatabase(); + var newFlower = Flower.Create(context, model); + if (newFlower == null) + { + return null; + } + context.Flowers.Add(newFlower); + context.SaveChanges(); + return newFlower.GetViewModel; + } + public FlowerViewModel? Update(FlowerBindingModel model) + { + using var context = new FlowerShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Flowers.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateComponents(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public FlowerViewModel? Delete(FlowerBindingModel model) + { + using var context = new FlowerShopDatabase(); + var element = context.Flowers + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Flowers.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Implements/OrderStorage.cs b/FlowerShop/FlowerShopDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..9a1353d --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,93 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopDatabaseImplement.Models; + +namespace FlowerShopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new FlowerShopDatabase(); + return context.Orders + .Select(x => AccessFlowerStorage(x.GetViewModel)) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new FlowerShopDatabase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Select(x => AccessFlowerStorage(x.GetViewModel)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new FlowerShopDatabase(); + return AccessFlowerStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); + } + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new FlowerShopDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return AccessFlowerStorage(newOrder.GetViewModel); + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new FlowerShopDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == + model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return AccessFlowerStorage(order.GetViewModel); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new FlowerShopDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return AccessFlowerStorage(element.GetViewModel); + } + return null; + } + + public static OrderViewModel AccessFlowerStorage(OrderViewModel model) + { + if (model == null) + return null; + using var context = new FlowerShopDatabase(); + foreach (var Flower in context.Flowers) + { + if (Flower.Id == model.FlowerId) + { + model.FlowerName = Flower.FlowerName; + break; + } + } + return model; + } + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Migrations/20240308215929_InitialCreate.Designer.cs b/FlowerShop/FlowerShopDatabaseImplement/Migrations/20240308215929_InitialCreate.Designer.cs new file mode 100644 index 0000000..72f544b --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Migrations/20240308215929_InitialCreate.Designer.cs @@ -0,0 +1,169 @@ +// +using System; +using FlowerShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace FlowerShopDatabaseImplement.Migrations +{ + [DbContext(typeof(FlowerShopDatabase))] + [Migration("20240308215929_InitialCreate")] + partial class InitialCreate + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.27") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Flower", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("FlowerName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Flowers"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("FlowerId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("FlowerId"); + + b.ToTable("FlowerComponents"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .IsRequired() + .HasColumnType("datetime2"); + + b.Property("FlowerId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("FlowerId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component") + .WithMany("FlowerComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FlowerShopDatabaseImplement.Models.Flower", "Flower") + .WithMany("Components") + .HasForeignKey("FlowerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Flower"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Flower", null) + .WithMany("Orders") + .HasForeignKey("FlowerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b => + { + b.Navigation("FlowerComponents"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Flower", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Migrations/20240308215929_InitialCreate.cs b/FlowerShop/FlowerShopDatabaseImplement/Migrations/20240308215929_InitialCreate.cs new file mode 100644 index 0000000..5559573 --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Migrations/20240308215929_InitialCreate.cs @@ -0,0 +1,122 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace FlowerShopDatabaseImplement.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: "Flowers", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + FlowerName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Flowers", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "FlowerComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + FlowerId = 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_FlowerComponents", x => x.Id); + table.ForeignKey( + name: "FK_FlowerComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_FlowerComponents_Flowers_FlowerId", + column: x => x.FlowerId, + principalTable: "Flowers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + FlowerId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Flowers_FlowerId", + column: x => x.FlowerId, + principalTable: "Flowers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_FlowerComponents_ComponentId", + table: "FlowerComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_FlowerComponents_FlowerId", + table: "FlowerComponents", + column: "FlowerId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_FlowerId", + table: "Orders", + column: "FlowerId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "FlowerComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Flowers"); + } + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Migrations/FlowerShopDatabaseModelSnapshot.cs b/FlowerShop/FlowerShopDatabaseImplement/Migrations/FlowerShopDatabaseModelSnapshot.cs new file mode 100644 index 0000000..d93d19a --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Migrations/FlowerShopDatabaseModelSnapshot.cs @@ -0,0 +1,167 @@ +// +using System; +using FlowerShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace FlowerShopDatabaseImplement.Migrations +{ + [DbContext(typeof(FlowerShopDatabase))] + partial class FlowerShopDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.27") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Flower", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("FlowerName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Flowers"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("FlowerId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("FlowerId"); + + b.ToTable("FlowerComponents"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .IsRequired() + .HasColumnType("datetime2"); + + b.Property("FlowerId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("FlowerId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component") + .WithMany("FlowerComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FlowerShopDatabaseImplement.Models.Flower", "Flower") + .WithMany("Components") + .HasForeignKey("FlowerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Flower"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Flower", null) + .WithMany("Orders") + .HasForeignKey("FlowerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b => + { + b.Navigation("FlowerComponents"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Flower", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Models/Component.cs b/FlowerShop/FlowerShopDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..79e9af3 --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Models/Component.cs @@ -0,0 +1,57 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace FlowerShopDatabaseImplement.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 FlowerComponents { 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/FlowerShop/FlowerShopDatabaseImplement/Models/Flower.cs b/FlowerShop/FlowerShopDatabaseImplement/Models/Flower.cs new file mode 100644 index 0000000..eea7e07 --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Models/Flower.cs @@ -0,0 +1,91 @@ +using FlowerShopDataModels.Models; +using FlowerShopContracts.ViewModels; +using FlowerShopContracts.BindingModels; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace FlowerShopDatabaseImplement.Models +{ + public class Flower : IFlowerModel + { + public int Id { get; set; } + [Required] + public string FlowerName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _FlowerComponents = null; + [NotMapped] + public Dictionary FlowerComponents + { + get + { + if (_FlowerComponents == null) + { + _FlowerComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _FlowerComponents; + } + } + [ForeignKey("FlowerId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("FlowerId")] + public virtual List Orders { get; set; } = new(); + public static Flower Create(FlowerShopDatabase context, FlowerBindingModel model) + { + return new Flower() + { + Id = model.Id, + FlowerName = model.FlowerName, + Price = model.Price, + Components = model.FlowerComponents.Select(x => new FlowerComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(FlowerBindingModel model) + { + FlowerName = model.FlowerName; + Price = model.Price; + } + public FlowerViewModel GetViewModel => new() + { + Id = Id, + FlowerName = FlowerName, + Price = Price, + FlowerComponents = FlowerComponents + }; + public void UpdateComponents(FlowerShopDatabase context, FlowerBindingModel model) + { + var FlowerComponents = context.FlowerComponents.Where(rec => rec.FlowerId == model.Id).ToList(); + if (FlowerComponents != null && FlowerComponents.Count > 0) + { + context.FlowerComponents.RemoveRange(FlowerComponents.Where(rec + => !model.FlowerComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + foreach (var updateComponent in FlowerComponents) + { + updateComponent.Count = model.FlowerComponents[updateComponent.ComponentId].Item2; + model.FlowerComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var Flower = context.Flowers.First(x => x.Id == Id); + foreach (var pc in model.FlowerComponents) + { + context.FlowerComponents.Add(new FlowerComponent + { + Flower = Flower, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _FlowerComponents = null; + } + + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Models/FlowerComponent.cs b/FlowerShop/FlowerShopDatabaseImplement/Models/FlowerComponent.cs new file mode 100644 index 0000000..eec1b60 --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Models/FlowerComponent.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace FlowerShopDatabaseImplement.Models +{ + public class FlowerComponent + { + public int Id { get; set; } + [Required] + public int FlowerId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Flower Flower { get; set; } = new(); + } + +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Models/Order.cs b/FlowerShop/FlowerShopDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..cbc0752 --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Models/Order.cs @@ -0,0 +1,59 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Enums; +using FlowerShopDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace FlowerShopDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int FlowerId { get; private set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; private set; } + [Required] + public OrderStatus Status { get; private set; } + [Required] + public DateTime DateCreate { get; private set; } + public DateTime? DateImplement { get; private set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + FlowerId = model.FlowerId, + 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, + FlowerId = FlowerId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +}