From 32ae633f5e8a8e61e90bee79432dceb486d733e1 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 14 Apr 2023 02:36:37 +0400 Subject: [PATCH 1/2] lab3 complete --- FlowerShop/FlowerShop.sln | 6 + FlowerShop/FlowerShop/FlowerShop.csproj | 5 + FlowerShop/FlowerShop/Program.cs | 2 +- .../FlowerShopDatabase.cs | 22 +++ .../FlowerShopDatabaseImplement.csproj | 23 +++ .../Implements/BouquetStorage.cs | 122 +++++++++++++ .../Implements/ComponentStorage.cs | 89 +++++++++ .../Implements/OrderStorage.cs | 89 +++++++++ .../20230312011037_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20230312011037_InitialCreate.cs | 125 +++++++++++++ .../FlowerShopDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Bouquet.cs | 100 ++++++++++ .../Models/BouquetComponent.cs | 22 +++ .../Models/Component.cs | 56 ++++++ .../Models/Order.cs | 75 ++++++++ 15 files changed, 1074 insertions(+), 1 deletion(-) create mode 100644 FlowerShop/FlowerShopDatabaseImplement/FlowerShopDatabase.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/FlowerShopDatabaseImplement.csproj create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Implements/BouquetStorage.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Implements/OrderStorage.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Migrations/20230312011037_InitialCreate.Designer.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Migrations/20230312011037_InitialCreate.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Migrations/FlowerShopDatabaseModelSnapshot.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Models/Bouquet.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Models/BouquetComponent.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Models/Component.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Models/Order.cs diff --git a/FlowerShop/FlowerShop.sln b/FlowerShop/FlowerShop.sln index 01a9c8b..7295d3e 100644 --- a/FlowerShop/FlowerShop.sln +++ b/FlowerShop/FlowerShop.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopListImplement", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopFileImplement", "FlowerShopFileImplement\FlowerShopFileImplement.csproj", "{87E9042D-E27B-4185-9FE2-92F15D68F8D4}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopDatabaseImplement", "FlowerShopDatabaseImplement\FlowerShopDatabaseImplement.csproj", "{99C4FA4A-EBAC-49E3-8D7F-C9177F9876B2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {87E9042D-E27B-4185-9FE2-92F15D68F8D4}.Debug|Any CPU.Build.0 = Debug|Any CPU {87E9042D-E27B-4185-9FE2-92F15D68F8D4}.Release|Any CPU.ActiveCfg = Release|Any CPU {87E9042D-E27B-4185-9FE2-92F15D68F8D4}.Release|Any CPU.Build.0 = Release|Any CPU + {99C4FA4A-EBAC-49E3-8D7F-C9177F9876B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {99C4FA4A-EBAC-49E3-8D7F-C9177F9876B2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {99C4FA4A-EBAC-49E3-8D7F-C9177F9876B2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {99C4FA4A-EBAC-49E3-8D7F-C9177F9876B2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FlowerShop/FlowerShop/FlowerShop.csproj b/FlowerShop/FlowerShop/FlowerShop.csproj index 9a6165d..b5fe158 100644 --- a/FlowerShop/FlowerShop/FlowerShop.csproj +++ b/FlowerShop/FlowerShop/FlowerShop.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -16,6 +20,7 @@ + diff --git a/FlowerShop/FlowerShop/Program.cs b/FlowerShop/FlowerShop/Program.cs index ffa0793..ce78939 100644 --- a/FlowerShop/FlowerShop/Program.cs +++ b/FlowerShop/FlowerShop/Program.cs @@ -1,6 +1,6 @@ using FlowerShopContracts.StoragesContracts; using Microsoft.Extensions.DependencyInjection; -using FlowerShopFileImplement.Implements; +using FlowerShopDatabaseImplement.Implements; using FlowerShopBusinessLogic.BusinessLogics; using FlowerShopContracts.BusinessLogicsContracts; using NLog.Extensions.Logging; diff --git a/FlowerShop/FlowerShopDatabaseImplement/FlowerShopDatabase.cs b/FlowerShop/FlowerShopDatabaseImplement/FlowerShopDatabase.cs new file mode 100644 index 0000000..5b95666 --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/FlowerShopDatabase.cs @@ -0,0 +1,22 @@ +using FlowerShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.Identity.Client; + +namespace FlowerShopDatabaseImplement +{ + public class FlowerShopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Server=DESKTOP-A68O3K0;Initial Catalog=FlowerShopDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Bouquets { set; get; } + public virtual DbSet BouquetComponents { 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..aef7629 --- /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/BouquetStorage.cs b/FlowerShop/FlowerShopDatabaseImplement/Implements/BouquetStorage.cs new file mode 100644 index 0000000..1558faa --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Implements/BouquetStorage.cs @@ -0,0 +1,122 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace FlowerShopDatabaseImplement.Implements +{ + public class BouquetStorage : IBouquetStorage + { + public BouquetViewModel? GetElement(BouquetSearchModel model) + { + if (string.IsNullOrEmpty(model.BouquetName) && !model.Id.HasValue) + { + return null; + } + + using var context = new FlowerShopDatabase(); + + return context.Bouquets.Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault( + x => (!string.IsNullOrEmpty(model.BouquetName) && x.BouquetName == model.BouquetName) || (model.Id.HasValue && x.Id == model.Id) + )?.GetViewModel; + } + + public List GetFilteredList(BouquetSearchModel model) + { + if (string.IsNullOrEmpty(model.BouquetName)) + { + return new(); + } + + using var context = new FlowerShopDatabase(); + + return context.Bouquets.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.BouquetName.Contains(model.BouquetName)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new FlowerShopDatabase(); + return context.Bouquets.Include(x => x.Components).ThenInclude(x => x.Component).Select(x => x.GetViewModel).ToList(); + } + + public BouquetViewModel? Insert(BouquetBindingModel model) + { + using var context = new FlowerShopDatabase(); + + using var transaction = context.Database.BeginTransaction(); + { + try + { + var newBouquet = Bouquet.Create(context, model); + if (newBouquet == null) + { + transaction.Rollback(); + return null; + } + + context.Bouquets.Add(newBouquet); + + context.SaveChanges(); + context.Database.CommitTransaction(); + + return newBouquet.GetViewModel; + } + catch (Exception) + { + transaction.Rollback(); + return null; + } + } + } + + public BouquetViewModel? Update(BouquetBindingModel model) + { + using var context = new FlowerShopDatabase(); + + using var transaction = context.Database.BeginTransaction(); + { + try + { + var bouquet = context.Bouquets.FirstOrDefault(x => x.Id == model.Id); + if (bouquet == null) + { + transaction.Rollback(); + return null; + } + + bouquet.Update(model); + + context.SaveChanges(); + context.Database.CommitTransaction(); + + return bouquet.GetViewModel; + } + catch (Exception) + { + transaction.Rollback(); + return null; + } + } + } + + public BouquetViewModel? Delete(BouquetBindingModel model) + { + using var context = new FlowerShopDatabase(); + + var element = context.Bouquets.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Bouquets.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Implements/ComponentStorage.cs b/FlowerShop/FlowerShopDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..017ada1 --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,89 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopDatabaseImplement.Models; + +namespace FlowerShopDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + 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 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 List GetFullList() + { + using var context = new FlowerShopDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + 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(record => record.Id == model.Id); + if (element != null) + { + context.Components.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..a26d0ba --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,89 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace FlowerShopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new FlowerShopDatabase(); + return context.Orders.Include(x => x.Bouquet).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 FlowerShopDatabase(); + + return context.Orders.Where(x => x.Id == model.Id).Include(x => x.Bouquet).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new FlowerShopDatabase(); + return context.Orders.Include(x => x.Bouquet).Select(x => x.GetViewModel).ToList(); + } + + 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 context.Orders.Include(x => x.Bouquet).FirstOrDefault(x => x.Id == newOrder.Id)?.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 context.Orders.Include(x => x.Bouquet).FirstOrDefault(x => x.Id == model.Id)?.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 element.GetViewModel; + } + + return null; + } + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Migrations/20230312011037_InitialCreate.Designer.cs b/FlowerShop/FlowerShopDatabaseImplement/Migrations/20230312011037_InitialCreate.Designer.cs new file mode 100644 index 0000000..d612ac0 --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Migrations/20230312011037_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +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("20230312011037_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Bouquet", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BouquetName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Bouquets"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.BouquetComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BouquetId") + .HasColumnType("int"); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("BouquetId"); + + b.HasIndex("ComponentId"); + + b.ToTable("BouquetComponents"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.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("FlowerShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BouquetId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("BouquetId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.BouquetComponent", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Bouquet", "Bouquet") + .WithMany("Components") + .HasForeignKey("BouquetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component") + .WithMany("BouquetComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Bouquet"); + + b.Navigation("Component"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Bouquet", "Bouquet") + .WithMany("Orders") + .HasForeignKey("BouquetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Bouquet"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Bouquet", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b => + { + b.Navigation("BouquetComponents"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Migrations/20230312011037_InitialCreate.cs b/FlowerShop/FlowerShopDatabaseImplement/Migrations/20230312011037_InitialCreate.cs new file mode 100644 index 0000000..c0a9a0a --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Migrations/20230312011037_InitialCreate.cs @@ -0,0 +1,125 @@ +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: "Bouquets", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + BouquetName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Bouquets", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + BouquetId = 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_Bouquets_BouquetId", + column: x => x.BouquetId, + principalTable: "Bouquets", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "BouquetComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + BouquetId = 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_BouquetComponents", x => x.Id); + table.ForeignKey( + name: "FK_BouquetComponents_Bouquets_BouquetId", + column: x => x.BouquetId, + principalTable: "Bouquets", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_BouquetComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_BouquetComponents_BouquetId", + table: "BouquetComponents", + column: "BouquetId"); + + migrationBuilder.CreateIndex( + name: "IX_BouquetComponents_ComponentId", + table: "BouquetComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_BouquetId", + table: "Orders", + column: "BouquetId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "BouquetComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Bouquets"); + } + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Migrations/FlowerShopDatabaseModelSnapshot.cs b/FlowerShop/FlowerShopDatabaseImplement/Migrations/FlowerShopDatabaseModelSnapshot.cs new file mode 100644 index 0000000..7b573da --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Migrations/FlowerShopDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +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", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Bouquet", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BouquetName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Bouquets"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.BouquetComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BouquetId") + .HasColumnType("int"); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("BouquetId"); + + b.HasIndex("ComponentId"); + + b.ToTable("BouquetComponents"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.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("FlowerShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BouquetId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("BouquetId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.BouquetComponent", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Bouquet", "Bouquet") + .WithMany("Components") + .HasForeignKey("BouquetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component") + .WithMany("BouquetComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Bouquet"); + + b.Navigation("Component"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Bouquet", "Bouquet") + .WithMany("Orders") + .HasForeignKey("BouquetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Bouquet"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Bouquet", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b => + { + b.Navigation("BouquetComponents"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Models/Bouquet.cs b/FlowerShop/FlowerShopDatabaseImplement/Models/Bouquet.cs new file mode 100644 index 0000000..0cb5b5e --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Models/Bouquet.cs @@ -0,0 +1,100 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace FlowerShopDatabaseImplement.Models +{ + public class Bouquet : IBouquetModel + { + public int Id { get; private set; } + + [Required] + public string BouquetName { get; private set; } = string.Empty; + + [Required] + public double Price { get; private set; } + + private Dictionary? _bouquetComponents = null; + + [NotMapped] + public Dictionary BouquetComponents + { + get + { + if (_bouquetComponents == null) + { + _bouquetComponents = Components.ToDictionary(record => record.ComponentId, record => (record.Component as IComponentModel, record.Count)); + } + return _bouquetComponents; + } + } + + [ForeignKey("BouquetId")] + public virtual List Components { get; set; } = new(); + + [ForeignKey("BouquetId")] + public virtual List Orders { get; set; } = new(); + + public static Bouquet? Create(FlowerShopDatabase context, BouquetBindingModel model) + { + return new Bouquet() + { + Id = model.Id, + BouquetName = model.BouquetName, + Price = model.Price, + Components = model.BouquetComponents.Select(x => new BouquetComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(BouquetBindingModel model) + { + BouquetName = model.BouquetName; + Price = model.Price; + } + + public BouquetViewModel GetViewModel => new() + { + Id = Id, + BouquetName = BouquetName, + Price = Price, + BouquetComponents = BouquetComponents + }; + + public void UpdateComponents(FlowerShopDatabase context, BouquetBindingModel model) + { + var bouquetComponents = context.BouquetComponents.Where(record => record.BouquetId == model.Id).ToList(); + if (bouquetComponents != null && bouquetComponents.Count > 0) + { + context.BouquetComponents.RemoveRange(bouquetComponents.Where(record => !model.BouquetComponents.ContainsKey(record.ComponentId))); + context.SaveChanges(); + + foreach (var updateComponent in bouquetComponents) + { + updateComponent.Count = model.BouquetComponents[updateComponent.ComponentId].Item2; + model.BouquetComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + + var bouquet = context.Bouquets.First(x => x.Id == Id); + foreach (var pc in model.BouquetComponents) + { + context.BouquetComponents.Add(new BouquetComponent + { + Bouquet = bouquet, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + + _bouquetComponents = null; + } + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Models/BouquetComponent.cs b/FlowerShop/FlowerShopDatabaseImplement/Models/BouquetComponent.cs new file mode 100644 index 0000000..525234f --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Models/BouquetComponent.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace FlowerShopDatabaseImplement.Models +{ + public class BouquetComponent + { + public int Id { get; set; } + + [Required] + public int BouquetId { get; set; } + + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Component Component { get; set; } = new(); + + public virtual Bouquet Bouquet { get; set; } = new(); + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Models/Component.cs b/FlowerShop/FlowerShopDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..117df09 --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Models/Component.cs @@ -0,0 +1,56 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + + +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 BouquetComponents { 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 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/Order.cs b/FlowerShop/FlowerShopDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..6f727b7 --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Models/Order.cs @@ -0,0 +1,75 @@ +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; } + + [Required] + public int BouquetId { 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 Bouquet Bouquet { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + + return new Order + { + BouquetId = model.BouquetId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Id = model.Id + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + BouquetId = BouquetId, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status, + BouquetName = Bouquet.BouquetName + }; + } +} -- 2.25.1 From 76f57dd5c7c5b6b28ef2e9b1f70f2306ab3bd7c0 Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 14 Jun 2023 00:01:27 +0400 Subject: [PATCH 2/2] 3 hard complete --- FlowerShop/FlowerShop.sln | 20 +- FlowerShop/FlowerShop/FlowerShop.csproj | 2 +- .../BusinessLogics/OrderLogic.cs | 69 ++++- .../BusinessLogics/ShopLogic.cs | 66 ----- .../BusinessLogicsContracts/IShopLogic.cs | 1 - .../FlowerShopDatabase.cs | 5 +- .../Implements/BouquetStorage.cs | 1 + .../Implements/ShopStorage.cs | 162 ++++++++++++ .../20230420224142_ShopsAdded.Designer.cs | 248 ++++++++++++++++++ .../Migrations/20230420224142_ShopsAdded.cs | 78 ++++++ .../FlowerShopDatabaseModelSnapshot.cs | 77 ++++++ .../Models/Shop.cs | 114 ++++++++ .../Models/ShopBouquet.cs | 22 ++ 13 files changed, 784 insertions(+), 81 deletions(-) create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Implements/ShopStorage.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Migrations/20230420224142_ShopsAdded.Designer.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Migrations/20230420224142_ShopsAdded.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Models/Shop.cs create mode 100644 FlowerShop/FlowerShopDatabaseImplement/Models/ShopBouquet.cs diff --git a/FlowerShop/FlowerShop.sln b/FlowerShop/FlowerShop.sln index 7295d3e..f0b4bf1 100644 --- a/FlowerShop/FlowerShop.sln +++ b/FlowerShop/FlowerShop.sln @@ -13,9 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopBusinessLogic", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopListImplement", "FlowerShopListImplement\FlowerShopListImplement.csproj", "{C58840E6-D68D-476E-AB3A-EB13F1740FC8}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopFileImplement", "FlowerShopFileImplement\FlowerShopFileImplement.csproj", "{87E9042D-E27B-4185-9FE2-92F15D68F8D4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopFileImplement", "FlowerShopFileImplement\FlowerShopFileImplement.csproj", "{87156689-2FDA-4065-90CC-749B2FE3C0D6}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopDatabaseImplement", "FlowerShopDatabaseImplement\FlowerShopDatabaseImplement.csproj", "{99C4FA4A-EBAC-49E3-8D7F-C9177F9876B2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopDatabaseImplement", "FlowerShopDatabaseImplement\FlowerShopDatabaseImplement.csproj", "{77B5E3FA-7A48-41AD-BB77-6EBF273550AC}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -43,14 +43,14 @@ Global {C58840E6-D68D-476E-AB3A-EB13F1740FC8}.Debug|Any CPU.Build.0 = Debug|Any CPU {C58840E6-D68D-476E-AB3A-EB13F1740FC8}.Release|Any CPU.ActiveCfg = Release|Any CPU {C58840E6-D68D-476E-AB3A-EB13F1740FC8}.Release|Any CPU.Build.0 = Release|Any CPU - {87E9042D-E27B-4185-9FE2-92F15D68F8D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {87E9042D-E27B-4185-9FE2-92F15D68F8D4}.Debug|Any CPU.Build.0 = Debug|Any CPU - {87E9042D-E27B-4185-9FE2-92F15D68F8D4}.Release|Any CPU.ActiveCfg = Release|Any CPU - {87E9042D-E27B-4185-9FE2-92F15D68F8D4}.Release|Any CPU.Build.0 = Release|Any CPU - {99C4FA4A-EBAC-49E3-8D7F-C9177F9876B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {99C4FA4A-EBAC-49E3-8D7F-C9177F9876B2}.Debug|Any CPU.Build.0 = Debug|Any CPU - {99C4FA4A-EBAC-49E3-8D7F-C9177F9876B2}.Release|Any CPU.ActiveCfg = Release|Any CPU - {99C4FA4A-EBAC-49E3-8D7F-C9177F9876B2}.Release|Any CPU.Build.0 = Release|Any CPU + {87156689-2FDA-4065-90CC-749B2FE3C0D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {87156689-2FDA-4065-90CC-749B2FE3C0D6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {87156689-2FDA-4065-90CC-749B2FE3C0D6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {87156689-2FDA-4065-90CC-749B2FE3C0D6}.Release|Any CPU.Build.0 = Release|Any CPU + {77B5E3FA-7A48-41AD-BB77-6EBF273550AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {77B5E3FA-7A48-41AD-BB77-6EBF273550AC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {77B5E3FA-7A48-41AD-BB77-6EBF273550AC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {77B5E3FA-7A48-41AD-BB77-6EBF273550AC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FlowerShop/FlowerShop/FlowerShop.csproj b/FlowerShop/FlowerShop/FlowerShop.csproj index b5fe158..ad47413 100644 --- a/FlowerShop/FlowerShop/FlowerShop.csproj +++ b/FlowerShop/FlowerShop/FlowerShop.csproj @@ -9,7 +9,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/FlowerShop/FlowerShopBusinessLogic/BusinessLogics/OrderLogic.cs b/FlowerShop/FlowerShopBusinessLogic/BusinessLogics/OrderLogic.cs index 1addcc9..967e959 100644 --- a/FlowerShop/FlowerShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/FlowerShop/FlowerShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -100,7 +100,7 @@ namespace FlowerShopBusinessLogic.BusinessLogics return false; } - if (!_shopLogic.CheckThenSupplyMany(bouquet, order.Count)) + if (!CheckThenSupplyMany(bouquet, order.Count)) { _logger.LogWarning("Status update to Готов operation failed. Shop supply error."); throw new Exception("В магазинах не хватает места!"); @@ -165,5 +165,72 @@ namespace FlowerShopBusinessLogic.BusinessLogics throw new InvalidOperationException("Заказ с таким идентификатором уже есть"); } } + + public bool CheckThenSupplyMany(IBouquetModel bouquet, int count) + { + if (count <= 0) + { + _logger.LogWarning("Check then supply operation error. Bouquet count < 0."); + return false; + } + + int freeSpace = 0; + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace += shop.MaxCountBouquets; + foreach (var bouq in shop.ShopBouquets) + { + freeSpace -= bouq.Value.Item2; + } + } + + if (freeSpace - count < 0) + { + _logger.LogWarning("Check then supply operation error. There's no place for new bouquets in shops."); + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace = shop.MaxCountBouquets; + + foreach (var bouq in shop.ShopBouquets) + { + freeSpace -= bouq.Value.Item2; + } + + if (freeSpace == 0) + { + continue; + } + + if (freeSpace - count >= 0) + { + if (_shopLogic.SupplyBouquets(new() { Id = shop.Id }, bouquet, count)) count = 0; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + + if (freeSpace - count < 0) + { + if (_shopLogic.SupplyBouquets(new() { Id = shop.Id }, bouquet, freeSpace)) count -= freeSpace; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + + if (count <= 0) + { + return true; + } + } + + return false; + } } } diff --git a/FlowerShop/FlowerShopBusinessLogic/BusinessLogics/ShopLogic.cs b/FlowerShop/FlowerShopBusinessLogic/BusinessLogics/ShopLogic.cs index e2e2452..918a104 100644 --- a/FlowerShop/FlowerShopBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/FlowerShop/FlowerShopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -199,71 +199,5 @@ namespace FlowerShopBusinessLogic.BusinessLogics throw new InvalidOperationException("Магазин с таким названием уже есть"); } } - public bool CheckThenSupplyMany(IBouquetModel bouquet, int count) - { - if (count <= 0) - { - _logger.LogWarning("Check then supply operation error. Bouquet count < 0."); - return false; - } - - int freeSpace = 0; - foreach (var shop in _shopStorage.GetFullList()) - { - freeSpace += shop.MaxCountBouquets; - foreach (var bouq in shop.ShopBouquets) - { - freeSpace -= bouq.Value.Item2; - } - } - - if (freeSpace - count < 0) - { - _logger.LogWarning("Check then supply operation error. There's no place for new bouquets in shops."); - return false; - } - - foreach (var shop in _shopStorage.GetFullList()) - { - freeSpace = shop.MaxCountBouquets; - - foreach (var bouq in shop.ShopBouquets) - { - freeSpace -= bouq.Value.Item2; - } - - if (freeSpace == 0) - { - continue; - } - - if (freeSpace - count >= 0) - { - if (SupplyBouquets(new() { Id = shop.Id }, bouquet, count)) count = 0; - else - { - _logger.LogWarning("Supply error"); - return false; - } - } - - if (freeSpace - count < 0) - { - if (SupplyBouquets(new() { Id = shop.Id }, bouquet, freeSpace)) count -= freeSpace; - else - { - _logger.LogWarning("Supply error"); - return false; - } - } - - if (count <= 0) - { - return true; - } - } - - return false; - } } } diff --git a/FlowerShop/FlowerShopContracts/BusinessLogicsContracts/IShopLogic.cs b/FlowerShop/FlowerShopContracts/BusinessLogicsContracts/IShopLogic.cs index be32c68..d21f9cb 100644 --- a/FlowerShop/FlowerShopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/FlowerShop/FlowerShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -14,6 +14,5 @@ namespace FlowerShopContracts.BusinessLogicsContracts bool Delete(ShopBindingModel model); bool SupplyBouquets(ShopSearchModel model, IBouquetModel bouquet, int count); bool SellBouquet(IBouquetModel bouquet, int count); - bool CheckThenSupplyMany(IBouquetModel bouquet, int count); } } diff --git a/FlowerShop/FlowerShopDatabaseImplement/FlowerShopDatabase.cs b/FlowerShop/FlowerShopDatabaseImplement/FlowerShopDatabase.cs index 5b95666..17fa4e1 100644 --- a/FlowerShop/FlowerShopDatabaseImplement/FlowerShopDatabase.cs +++ b/FlowerShop/FlowerShopDatabaseImplement/FlowerShopDatabase.cs @@ -1,6 +1,5 @@ using FlowerShopDatabaseImplement.Models; using Microsoft.EntityFrameworkCore; -using Microsoft.Identity.Client; namespace FlowerShopDatabaseImplement { @@ -10,7 +9,7 @@ namespace FlowerShopDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Server=DESKTOP-A68O3K0;Initial Catalog=FlowerShopDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Initial Catalog=FlowerShopDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } @@ -18,5 +17,7 @@ namespace FlowerShopDatabaseImplement public virtual DbSet Bouquets { set; get; } public virtual DbSet BouquetComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopBouquets { set; get; } } } diff --git a/FlowerShop/FlowerShopDatabaseImplement/Implements/BouquetStorage.cs b/FlowerShop/FlowerShopDatabaseImplement/Implements/BouquetStorage.cs index 1558faa..42ee3da 100644 --- a/FlowerShop/FlowerShopDatabaseImplement/Implements/BouquetStorage.cs +++ b/FlowerShop/FlowerShopDatabaseImplement/Implements/BouquetStorage.cs @@ -89,6 +89,7 @@ namespace FlowerShopDatabaseImplement.Implements } bouquet.Update(model); + bouquet.UpdateComponents(context, model); context.SaveChanges(); context.Database.CommitTransaction(); diff --git a/FlowerShop/FlowerShopDatabaseImplement/Implements/ShopStorage.cs b/FlowerShop/FlowerShopDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..8761059 --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,162 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopDatabaseImplement.Models; +using FlowerShopDataModels.Models; +using Microsoft.EntityFrameworkCore; + +namespace FlowerShopDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return new(); + } + + using var context = new FlowerShopDatabase(); + + return context.Shops.Include(x => x.Bouquets) + .ThenInclude(x => x.Bouquet) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + + using var context = new FlowerShopDatabase(); + + return context.Shops.Include(x => x.Bouquets).ThenInclude(x => x.Bouquet).Where(x => x.Name.Contains(model.Name)).ToList().Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new FlowerShopDatabase(); + + return context.Shops.Include(x => x.Bouquets).ThenInclude(x => x.Bouquet).ToList().Select(x => x.GetViewModel).ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new FlowerShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + + try + { + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + + if (context.Shops.Any(x => x.Name == newShop.Name)) + { + throw new Exception("Название магазина уже занято"); + } + + context.Shops.Add(newShop); + context.SaveChanges(); + transaction.Commit(); + + return newShop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new FlowerShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + + try + { + var shop = context.Shops.Include(x => x.Bouquets).FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + + shop.Update(model); + context.SaveChanges(); + + if (model.ShopBouquets.Count > 0) + { + shop.UpdateBouquets(context, model); + } + + transaction.Commit(); + + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new FlowerShopDatabase(); + var shop = context.Shops.Include(x => x.Bouquets).FirstOrDefault(x => x.Id == model.Id); + + if (shop != null) + { + context.Shops.Remove(shop); + context.SaveChanges(); + + return shop.GetViewModel; + } + + return null; + } + + public bool SellBouquet(IBouquetModel model, int count) + { + using var context = new FlowerShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + + foreach (var shopBouquets in context.ShopBouquets.Where(x => x.BouquetId == model.Id)) + { + var min = Math.Min(count, shopBouquets.Count); + shopBouquets.Count -= min; + count -= min; + + if (count <= 0) + { + break; + } + } + + if (count == 0) + { + context.SaveChanges(); + transaction.Commit(); + } + + else + { + transaction.Rollback(); + } + + if (count > 0) + { + return false; + } + + return true; + } + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Migrations/20230420224142_ShopsAdded.Designer.cs b/FlowerShop/FlowerShopDatabaseImplement/Migrations/20230420224142_ShopsAdded.Designer.cs new file mode 100644 index 0000000..904bb87 --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Migrations/20230420224142_ShopsAdded.Designer.cs @@ -0,0 +1,248 @@ +// +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("20230420224142_ShopsAdded")] + partial class ShopsAdded + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Bouquet", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BouquetName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Bouquets"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.BouquetComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BouquetId") + .HasColumnType("int"); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("BouquetId"); + + b.HasIndex("ComponentId"); + + b.ToTable("BouquetComponents"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.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("FlowerShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BouquetId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("BouquetId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("MaxCountBouquets") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.ShopBouquet", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BouquetId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("BouquetId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopBouquets"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.BouquetComponent", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Bouquet", "Bouquet") + .WithMany("Components") + .HasForeignKey("BouquetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component") + .WithMany("BouquetComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Bouquet"); + + b.Navigation("Component"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Bouquet", "Bouquet") + .WithMany("Orders") + .HasForeignKey("BouquetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Bouquet"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.ShopBouquet", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Bouquet", "Bouquet") + .WithMany() + .HasForeignKey("BouquetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FlowerShopDatabaseImplement.Models.Shop", "Shop") + .WithMany("Bouquets") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Bouquet"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Bouquet", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b => + { + b.Navigation("BouquetComponents"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Shop", b => + { + b.Navigation("Bouquets"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Migrations/20230420224142_ShopsAdded.cs b/FlowerShop/FlowerShopDatabaseImplement/Migrations/20230420224142_ShopsAdded.cs new file mode 100644 index 0000000..7eb9b52 --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Migrations/20230420224142_ShopsAdded.cs @@ -0,0 +1,78 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace FlowerShopDatabaseImplement.Migrations +{ + /// + public partial class ShopsAdded : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + OpeningDate = table.Column(type: "datetime2", nullable: false), + MaxCountBouquets = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ShopBouquets", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + BouquetId = table.Column(type: "int", nullable: false), + ShopId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopBouquets", x => x.Id); + table.ForeignKey( + name: "FK_ShopBouquets_Bouquets_BouquetId", + column: x => x.BouquetId, + principalTable: "Bouquets", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopBouquets_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShopBouquets_BouquetId", + table: "ShopBouquets", + column: "BouquetId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopBouquets_ShopId", + table: "ShopBouquets", + column: "ShopId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShopBouquets"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Migrations/FlowerShopDatabaseModelSnapshot.cs b/FlowerShop/FlowerShopDatabaseImplement/Migrations/FlowerShopDatabaseModelSnapshot.cs index 7b573da..317598c 100644 --- a/FlowerShop/FlowerShopDatabaseImplement/Migrations/FlowerShopDatabaseModelSnapshot.cs +++ b/FlowerShop/FlowerShopDatabaseImplement/Migrations/FlowerShopDatabaseModelSnapshot.cs @@ -121,6 +121,59 @@ namespace FlowerShopDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("MaxCountBouquets") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.ShopBouquet", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BouquetId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("BouquetId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopBouquets"); + }); + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.BouquetComponent", b => { b.HasOne("FlowerShopDatabaseImplement.Models.Bouquet", "Bouquet") @@ -151,6 +204,25 @@ namespace FlowerShopDatabaseImplement.Migrations b.Navigation("Bouquet"); }); + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.ShopBouquet", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Bouquet", "Bouquet") + .WithMany() + .HasForeignKey("BouquetId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FlowerShopDatabaseImplement.Models.Shop", "Shop") + .WithMany("Bouquets") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Bouquet"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Bouquet", b => { b.Navigation("Components"); @@ -162,6 +234,11 @@ namespace FlowerShopDatabaseImplement.Migrations { b.Navigation("BouquetComponents"); }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Shop", b => + { + b.Navigation("Bouquets"); + }); #pragma warning restore 612, 618 } } diff --git a/FlowerShop/FlowerShopDatabaseImplement/Models/Shop.cs b/FlowerShop/FlowerShopDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..780556d --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Models/Shop.cs @@ -0,0 +1,114 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace FlowerShopDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + + [Required] + public string Address { get; set; } = string.Empty; + + [Required] + public DateTime OpeningDate { get; set; } + + [ForeignKey("ShopId")] + public List Bouquets { get; set; } = new(); + + private Dictionary? _shopBouquets = null; + + [NotMapped] + public Dictionary ShopBouquets + { + get + { + if (_shopBouquets == null) + { + _shopBouquets = Bouquets.ToDictionary(recPC => recPC.BouquetId, recPC => (recPC.Bouquet as IBouquetModel, recPC.Count)); + } + + return _shopBouquets; + } + } + + [Required] + public int MaxCountBouquets { get; set; } + + public static Shop Create(FlowerShopDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + Name = model.Name, + Address = model.Address, + OpeningDate = model.OpeningDate, + Bouquets = model.ShopBouquets.Select(x => new ShopBouquet + { + Bouquet = context.Bouquets.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + MaxCountBouquets = model.MaxCountBouquets + }; + } + + public void Update(ShopBindingModel model) + { + Name = model.Name; + Address = model.Address; + OpeningDate = model.OpeningDate; + MaxCountBouquets = model.MaxCountBouquets; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Address = Address, + OpeningDate = OpeningDate, + ShopBouquets = ShopBouquets, + MaxCountBouquets = MaxCountBouquets + }; + + public void UpdateBouquets(FlowerShopDatabase context, ShopBindingModel model) + { + var ShopBouquets = context.ShopBouquets.Where(rec => rec.ShopId == model.Id).ToList(); + + if (ShopBouquets != null && this.ShopBouquets.Count > 0) + { + context.ShopBouquets.RemoveRange(ShopBouquets.Where((ShopBouquet rec) => !model.ShopBouquets.ContainsKey(rec.BouquetId))); + context.SaveChanges(); + + ShopBouquets = context.ShopBouquets.Where((ShopBouquet rec) => rec.ShopId == model.Id).ToList(); + + foreach (var updateBouquet in ShopBouquets) + { + updateBouquet.Count = model.ShopBouquets[updateBouquet.BouquetId].Item2; + model.ShopBouquets.Remove(updateBouquet.BouquetId); + } + + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + + foreach (var elem in model.ShopBouquets) + { + context.ShopBouquets.Add(new ShopBouquet + { + Shop = shop, + Bouquet = context.Bouquets.First(x => x.Id == elem.Key), + Count = elem.Value.Item2 + }); + + context.SaveChanges(); + } + + _shopBouquets = null; + } + } +} diff --git a/FlowerShop/FlowerShopDatabaseImplement/Models/ShopBouquet.cs b/FlowerShop/FlowerShopDatabaseImplement/Models/ShopBouquet.cs new file mode 100644 index 0000000..6a36aeb --- /dev/null +++ b/FlowerShop/FlowerShopDatabaseImplement/Models/ShopBouquet.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace FlowerShopDatabaseImplement.Models +{ + public class ShopBouquet + { + public int Id { get; set; } + + [Required] + public int BouquetId { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + + public virtual Bouquet Bouquet { get; set; } = new(); + } +} -- 2.25.1