From 207ab09be71d80e38e058c842bfb9c37e8b19c56 Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Sun, 26 Feb 2023 21:00:34 +0400 Subject: [PATCH 1/5] =?UTF-8?q?3=20=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=BD=D0=B0=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IceCreamShop/IceCreamShop.sln | 8 +- .../IceCreamShop/IceCreamShopView.csproj | 5 + IceCreamShop/IceCreamShop/Program.cs | 2 +- .../IceCreamShopDatabase.cs | 25 +++ .../IceCreamShopDatabaseImplement.csproj | 23 +++ .../Implements/ComponentStorage.cs | 78 +++++++++ .../Implements/IceCreamStorage.cs | 105 ++++++++++++ .../Implements/OrderStorage.cs | 93 ++++++++++ .../20230226160632_InitMigration.Designer.cs | 160 +++++++++++++++++ .../20230226160632_InitMigration.cs | 115 +++++++++++++ .../IceCreamShopDatabaseModelSnapshot.cs | 161 ++++++++++++++++++ .../Models/Component.cs | 61 +++++++ .../Models/IceCream.cs | 99 +++++++++++ .../Models/IceCreamComponent.cs | 22 +++ .../Models/Order.cs | 74 ++++++++ 15 files changed, 1029 insertions(+), 2 deletions(-) create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20230226160632_InitMigration.Designer.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20230226160632_InitMigration.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs diff --git a/IceCreamShop/IceCreamShop.sln b/IceCreamShop/IceCreamShop.sln index d53647f..e581e77 100644 --- a/IceCreamShop/IceCreamShop.sln +++ b/IceCreamShop/IceCreamShop.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamBusinessLogic", "Ic EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopListImplement", "IceCreamShopListImplement\IceCreamShopListImplement.csproj", "{EBB0BAF6-8DEE-42EF-8AF5-571B4E0DE95E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopFileImplement", "IceCreamShopFileImplement\IceCreamShopFileImplement.csproj", "{382E618C-D833-4E98-9786-7A831953ABD0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopFileImplement", "IceCreamShopFileImplement\IceCreamShopFileImplement.csproj", "{382E618C-D833-4E98-9786-7A831953ABD0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopDatabaseImplement", "IceCreamShopDatabaseImplement\IceCreamShopDatabaseImplement.csproj", "{9C8466E4-0AA1-4B4F-A733-5130C29513BB}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {382E618C-D833-4E98-9786-7A831953ABD0}.Debug|Any CPU.Build.0 = Debug|Any CPU {382E618C-D833-4E98-9786-7A831953ABD0}.Release|Any CPU.ActiveCfg = Release|Any CPU {382E618C-D833-4E98-9786-7A831953ABD0}.Release|Any CPU.Build.0 = Release|Any CPU + {9C8466E4-0AA1-4B4F-A733-5130C29513BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9C8466E4-0AA1-4B4F-A733-5130C29513BB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9C8466E4-0AA1-4B4F-A733-5130C29513BB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9C8466E4-0AA1-4B4F-A733-5130C29513BB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/IceCreamShop/IceCreamShop/IceCreamShopView.csproj b/IceCreamShop/IceCreamShop/IceCreamShopView.csproj index 21ad05e..1e192a7 100644 --- a/IceCreamShop/IceCreamShop/IceCreamShopView.csproj +++ b/IceCreamShop/IceCreamShop/IceCreamShopView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -16,6 +20,7 @@ + diff --git a/IceCreamShop/IceCreamShop/Program.cs b/IceCreamShop/IceCreamShop/Program.cs index 9e9dcb1..8e85327 100644 --- a/IceCreamShop/IceCreamShop/Program.cs +++ b/IceCreamShop/IceCreamShop/Program.cs @@ -5,7 +5,7 @@ using IceCreamShopView; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -using IceCreamShopFileImplement.Implements; +using IceCreamShopDatabaseImplement.Implements; namespace IceCreamShop { diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs new file mode 100644 index 0000000..7056c7a --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs @@ -0,0 +1,25 @@ +using IceCreamShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace IceCreamShopDatabaseImplement +{ + public class IceCreamShopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=CHESHIR\SQLEXPRESS;Initial Catalog=IceCreamShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + + public virtual DbSet Components { set; get; } + + public virtual DbSet IceCreams { set; get; } + + public virtual DbSet IceCreamComponents { set; get; } + + public virtual DbSet Orders { set; get; } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj new file mode 100644 index 0000000..616d02d --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..8898898 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,78 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDatabaseImplement.Models; + +namespace IceCreamShopDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new IceCreamShopDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new IceCreamShopDatabase(); + 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 IceCreamShopDatabase(); + 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 IceCreamShopDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new IceCreamShopDatabase(); + 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 IceCreamShopDatabase(); + 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/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs new file mode 100644 index 0000000..a34bfe5 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs @@ -0,0 +1,105 @@ +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopContracts.BindingModels; +using Microsoft.EntityFrameworkCore; +using IceCreamShopDatabaseImplement.Models; + +namespace IceCreamShopDatabaseImplement.Implements +{ + public class IceCreamStorage : IIceCreamStorage + { + public List GetFullList() + { + using var context = new IceCreamShopDatabase(); + return context.IceCreams + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(IceCreamSearchModel model) + { + if (string.IsNullOrEmpty(model.IceCreamName)) + { + return new(); + } + using var context = new IceCreamShopDatabase(); + return context.IceCreams + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.IceCreamName.Contains(model.IceCreamName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public IceCreamViewModel? GetElement(IceCreamSearchModel model) + { + if (string.IsNullOrEmpty(model.IceCreamName) && !model.Id.HasValue) + { + return null; + } + using var context = new IceCreamShopDatabase(); + return context.IceCreams + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.IceCreamName) && x.IceCreamName == model.IceCreamName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public IceCreamViewModel? Insert(IceCreamBindingModel model) + { + using var context = new IceCreamShopDatabase(); + var newIceCream = IceCream.Create(context, model); + if (newIceCream == null) + { + return null; + } + context.IceCreams.Add(newIceCream); + context.SaveChanges(); + return newIceCream.GetViewModel; + } + + public IceCreamViewModel? Update(IceCreamBindingModel model) + { + using var context = new IceCreamShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var iceCream = context.IceCreams.FirstOrDefault(rec => rec.Id == model.Id); + if (iceCream == null) + { + return null; + } + iceCream.Update(model); + context.SaveChanges(); + iceCream.UpdateComponents(context, model); + transaction.Commit(); + return iceCream.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public IceCreamViewModel? Delete(IceCreamBindingModel model) + { + using var context = new IceCreamShopDatabase(); + var element = context.IceCreams + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.IceCreams.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..d0f72db --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,93 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDatabaseImplement.Models; + +namespace IceCreamShopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new IceCreamShopDatabase(); + + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new IceCreamShopDatabase(); + + return context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new IceCreamShopDatabase(); + + return context.Orders.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new IceCreamShopDatabase(); + + return context.Orders.Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + + if (newOrder == null) + { + return null; + } + + using var context = new IceCreamShopDatabase(); + + context.Orders.Add(newOrder); + context.SaveChanges(); + + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new IceCreamShopDatabase(); + + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + + if (order == null) + { + return null; + } + + order.Update(model); + context.SaveChanges(); + + return order.GetViewModel; + } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20230226160632_InitMigration.Designer.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20230226160632_InitMigration.Designer.cs new file mode 100644 index 0000000..f3b6c8e --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20230226160632_InitMigration.Designer.cs @@ -0,0 +1,160 @@ +// +using System; +using IceCreamShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace IceCreamShopDatabaseImplement.Migrations +{ + [DbContext(typeof(IceCreamShopDatabase))] + [Migration("20230226160632_InitMigration")] + partial class InitMigration + { + /// + 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("IceCreamShopDatabaseImplement.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("IceCreamShopDatabaseImplement.Models.IceCream", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("IceCreamName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("IceCreams"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("IceCreamId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("IceCreamId"); + + b.ToTable("IceCreamComponents"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("IceCreamId") + .HasColumnType("int"); + + b.Property("IceCreamName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => + { + b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component") + .WithMany("IceCreamComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") + .WithMany("Components") + .HasForeignKey("IceCreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("IceCream"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => + { + b.Navigation("IceCreamComponents"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => + { + b.Navigation("Components"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20230226160632_InitMigration.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20230226160632_InitMigration.cs new file mode 100644 index 0000000..acc77f9 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20230226160632_InitMigration.cs @@ -0,0 +1,115 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace IceCreamShopDatabaseImplement.Migrations +{ + /// + public partial class InitMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "IceCreams", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + IceCreamName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_IceCreams", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + IceCreamId = table.Column(type: "int", nullable: false), + IceCreamName = table.Column(type: "nvarchar(max)", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "IceCreamComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + IceCreamId = 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_IceCreamComponents", x => x.Id); + table.ForeignKey( + name: "FK_IceCreamComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_IceCreamComponents_IceCreams_IceCreamId", + column: x => x.IceCreamId, + principalTable: "IceCreams", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_IceCreamComponents_ComponentId", + table: "IceCreamComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_IceCreamComponents_IceCreamId", + table: "IceCreamComponents", + column: "IceCreamId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "IceCreamComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "IceCreams"); + } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs new file mode 100644 index 0000000..df89698 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs @@ -0,0 +1,161 @@ +// +using System; +using IceCreamShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace IceCreamShopDatabaseImplement.Migrations +{ + [DbContext(typeof(IceCreamShopDatabase))] + partial class IceCreamShopDatabaseModelSnapshot : 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("IceCreamShopDatabaseImplement.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("IceCreamShopDatabaseImplement.Models.IceCream", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("IceCreamName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("IceCreams"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("IceCreamId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("IceCreamId"); + + b.ToTable("IceCreamComponents"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("IceCreamId") + .HasColumnType("int"); + + b.Property("IceCreamName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => + { + b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component") + .WithMany("IceCreamComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") + .WithMany("Components") + .HasForeignKey("IceCreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("IceCream"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => + { + b.Navigation("IceCreamComponents"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..ea5c094 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs @@ -0,0 +1,61 @@ +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using AbstractIceCreamShopDataModels.Models; + + +namespace IceCreamShopDatabaseImplement.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 IceCreamComponents { 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/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs new file mode 100644 index 0000000..45b4267 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs @@ -0,0 +1,99 @@ +using AbstractIceCreamShopDataModels.Models; +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace IceCreamShopDatabaseImplement.Models +{ + public class IceCream : IIceCreamModel + { + public int Id { get; set; } + + [Required] + public string IceCreamName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + private Dictionary? _iceCreamComponents = null; + + [NotMapped] + public Dictionary IceCreamComponents + { + get + { + if (_iceCreamComponents == null) + { + _iceCreamComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _iceCreamComponents; + } + } + + [ForeignKey("IceCreamId")] + public virtual List Components { get; set; } = new(); + + [ForeignKey("IceCreamId")] + public virtual List Orders { get; set; } = new(); + + public static IceCream Create(IceCreamShopDatabase context, IceCreamBindingModel model) + { + return new IceCream() + { + Id = model.Id, + IceCreamName = model.IceCreamName, + Price = model.Price, + Components = model.IceCreamComponents.Select(x => new IceCreamComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(IceCreamBindingModel model) + { + IceCreamName = model.IceCreamName; + Price = model.Price; + } + + public IceCreamViewModel GetViewModel => new() + { + Id = Id, + IceCreamName = IceCreamName, + Price = Price, + IceCreamComponents = IceCreamComponents + }; + + public void UpdateComponents(IceCreamShopDatabase context, IceCreamBindingModel model) + { + var iceCreamComponents = context.IceCreamComponents.Where(rec => rec.IceCreamId == model.Id).ToList(); + if (iceCreamComponents != null && iceCreamComponents.Count > 0) + { // удалили те, которых нет в модели + context.IceCreamComponents.RemoveRange(iceCreamComponents.Where(rec => !model.IceCreamComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in iceCreamComponents) + { + updateComponent.Count = model.IceCreamComponents[updateComponent.ComponentId].Item2; + model.IceCreamComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var iceCream = context.IceCreams.First(x => x.Id == Id); + foreach (var pc in model.IceCreamComponents) + { + context.IceCreamComponents.Add(new IceCreamComponent + { + IceCream = iceCream, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _iceCreamComponents = null; + } + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs new file mode 100644 index 0000000..bf849cf --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace IceCreamShopDatabaseImplement.Models +{ + public class IceCreamComponent + { + public int Id { get; set; } + + [Required] + public int IceCreamId { get; set; } + + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Component Component { get; set; } = new(); + + public virtual IceCream IceCream { get; set; } = new(); + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..ad5e6f1 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs @@ -0,0 +1,74 @@ +using AbstractIceCreamShopDataModels.Enums; +using AbstractIceCreamShopDataModels.Models; +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using System.ComponentModel.DataAnnotations; + +namespace IceCreamShopDatabaseImplement.Models +{ + public class Order : IOrderModel + { + [Required] + public int IceCreamId { get; set; } + public string IceCreamName { get; private set; } = string.Empty; + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } + [Required] + public DateTime DateCreate { get; set; } + + public DateTime? DateImplement { get; set; } + + public int Id { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + IceCreamId = model.IceCreamId, + IceCreamName = model.IceCreamName, + 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; + } + IceCreamId = model.IceCreamId; + IceCreamName = model.IceCreamName; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + Id = model.Id; + } + + public OrderViewModel GetViewModel => new() + { + IceCreamId = IceCreamId, + IceCreamName = IceCreamName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + }; + } +} -- 2.25.1 From 934fb70ec99a62086f1a2ac1855cd2bd1786df0d Mon Sep 17 00:00:00 2001 From: Inohara Date: Tue, 28 Feb 2023 09:30:02 +0400 Subject: [PATCH 2/5] =?UTF-8?q?=D1=81=D0=B4=D0=B0=D0=BD=D0=B0=203=20=D0=BB?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs index 7056c7a..cb7355f 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs @@ -9,7 +9,7 @@ namespace IceCreamShopDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=CHESHIR\SQLEXPRESS;Initial Catalog=IceCreamShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=INOSENJU\SQLEXPRESS;Initial Catalog=IceCreamShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } -- 2.25.1 From 925c8af3975c4aa835bda74a4967ba27f06be5b3 Mon Sep 17 00:00:00 2001 From: Inohara Date: Sat, 11 Mar 2023 16:20:53 +0400 Subject: [PATCH 3/5] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs index ad5e6f1..9a28952 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs @@ -49,14 +49,7 @@ namespace IceCreamShopDatabaseImplement.Models { return; } - IceCreamId = model.IceCreamId; - IceCreamName = model.IceCreamName; - Count = model.Count; - Sum = model.Sum; Status = model.Status; - DateCreate = model.DateCreate; - DateImplement = model.DateImplement; - Id = model.Id; } public OrderViewModel GetViewModel => new() -- 2.25.1 From bbc89019e5090154f6b39fb8608df0960e60b136 Mon Sep 17 00:00:00 2001 From: Inohara Date: Sun, 9 Apr 2023 19:24:19 +0400 Subject: [PATCH 4/5] =?UTF-8?q?=D0=B2=D1=80=D0=BE=D0=B4=D0=B5=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/OrderLogic.cs | 8 +- .../IceCreamShopDatabase.cs | 5 +- .../Implements/ShopStorage.cs | 137 +++++++++ .../20230409123459_shops.Designer.cs | 264 ++++++++++++++++++ .../Migrations/20230409123459_shops.cs | 95 +++++++ .../IceCreamShopDatabaseModelSnapshot.cs | 102 ++++++- .../Models/IceCream.cs | 5 +- .../Models/Shop.cs | 116 ++++++++ .../Models/ShopIcecream.cs | 18 ++ 9 files changed, 743 insertions(+), 7 deletions(-) create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Implements/ShopStorage.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20230409123459_shops.Designer.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20230409123459_shops.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/Shop.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/ShopIcecream.cs diff --git a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs index 0653c4f..a5d9ae8 100644 --- a/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/IceCreamShop/IceCreamBusinessLogic/BusinessLogics/OrderLogic.cs @@ -170,9 +170,9 @@ namespace IceCreamBusinessLogic.BusinessLogics foreach (var shop in _shopStorage.GetFullList()) { freeSpace += shop.IceCreamMaxCount; - foreach (var doc in shop.ShopIceCreams) + foreach (var icecream in shop.ShopIceCreams) { - freeSpace -= doc.Value.Item2; + freeSpace -= icecream.Value.Item2; } } @@ -185,9 +185,9 @@ namespace IceCreamBusinessLogic.BusinessLogics foreach (var shop in _shopStorage.GetFullList()) { freeSpace = shop.IceCreamMaxCount; - foreach (var doc in shop.ShopIceCreams) + foreach (var icecream in shop.ShopIceCreams) { - freeSpace -= doc.Value.Item2; + freeSpace -= icecream.Value.Item2; } if (freeSpace == 0) { diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs index cb7355f..ee68564 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabase.cs @@ -9,7 +9,7 @@ namespace IceCreamShopDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=INOSENJU\SQLEXPRESS;Initial Catalog=IceCreamShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=INOSENJU\SQLEXPRESS;Initial Catalog=IceCreamShopDatabaseHard;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } @@ -21,5 +21,8 @@ namespace IceCreamShopDatabaseImplement public virtual DbSet IceCreamComponents { set; get; } public virtual DbSet Orders { set; get; } + + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopIcecreams { set; get; } } } diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ShopStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..d0d2491 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,137 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDatabaseImplement.Models; +using AbstractIceCreamShopDataModels.Models; +using Microsoft.EntityFrameworkCore; + +namespace IceCreamShopDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new IceCreamShopDatabase(); + var element = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new IceCreamShopDatabase(); + return context.Shops + .Include(x => x.IceCreams) + .ThenInclude(x => x.IceCream) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new IceCreamShopDatabase(); + return context.Shops + .Include(x => x.IceCreams) + .ThenInclude(x => x.IceCream) + .Select(x => x.GetViewModel) + .Where(x => x.Name.Contains(model.Name ?? string.Empty)) + .ToList(); + } + + public List GetFullList() + { + using var context = new IceCreamShopDatabase(); + return context.Shops + .Include(x => x.IceCreams) + .ThenInclude(x => x.IceCream) + .Select(shop => shop.GetViewModel) + .ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new IceCreamShopDatabase(); + 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 IceCreamShopDatabase(); + var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + shop.UpdatePastries(context, model); + context.SaveChanges(); + return shop.GetViewModel; + } + + public bool SellIceCreams(IIceCreamModel icecream, int needCount) + { + using var context = new IceCreamShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + foreach (var sp in context.ShopIcecreams.Where(x => x.IceCreamId == icecream.Id)) + { + var res = Math.Min(needCount, sp.Count); + sp.Count -= res; + needCount -= res; + if (sp.Count == 0) // Изделия больше нет в магазине, значит удаляем его + { + context.ShopIcecreams.Remove(sp); + } + if (needCount == 0) // Нельзя коммитить изменения в цикле, что использует контекст, поэтому выходим + { + break; + } + } + if (needCount == 0) + { + context.SaveChanges(); + transaction.Commit(); + } + else + { + transaction.Rollback(); + } + return needCount == 0; + } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20230409123459_shops.Designer.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20230409123459_shops.Designer.cs new file mode 100644 index 0000000..1b69253 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20230409123459_shops.Designer.cs @@ -0,0 +1,264 @@ +// +using System; +using IceCreamShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace IceCreamShopDatabaseImplement.Migrations +{ + [DbContext(typeof(IceCreamShopDatabase))] + [Migration("20230409123459_shops")] + partial class shops + { + /// + 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("IceCreamShopDatabaseImplement.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("IceCreamShopDatabaseImplement.Models.IceCream", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("IceCreamName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("IceCreams"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("IceCreamId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("IceCreamId"); + + b.ToTable("IceCreamComponents"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("IceCreamId") + .HasColumnType("int"); + + b.Property("IceCreamName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("IceCreamId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Adress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("IceCreamId") + .HasColumnType("int"); + + b.Property("IceCreamMaxCount") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.HasIndex("IceCreamId"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.ShopIcecream", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("IceCreamId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("IceCreamId"); + + b.HasIndex("ShopId"); + + b.ToTable("IceCreams"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => + { + b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component") + .WithMany("IceCreamComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") + .WithMany("Components") + .HasForeignKey("IceCreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("IceCream"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => + { + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", null) + .WithMany("Orders") + .HasForeignKey("IceCreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Shop", b => + { + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", null) + .WithMany("Shops") + .HasForeignKey("IceCreamId"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.ShopIcecream", b => + { + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") + .WithMany() + .HasForeignKey("IceCreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("IceCreamShopDatabaseImplement.Models.Shop", "Shop") + .WithMany("IceCreams") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("IceCream"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => + { + b.Navigation("IceCreamComponents"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + + b.Navigation("Shops"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Shop", b => + { + b.Navigation("IceCreams"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20230409123459_shops.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20230409123459_shops.cs new file mode 100644 index 0000000..5855cac --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20230409123459_shops.cs @@ -0,0 +1,95 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace IceCreamShopDatabaseImplement.Migrations +{ + /// + public partial class shops : 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), + Adress = table.Column(type: "nvarchar(max)", nullable: false), + OpeningDate = table.Column(type: "datetime2", nullable: false), + IceCreamMaxCount = table.Column(type: "int", nullable: false), + IceCreamId = table.Column(type: "int", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + table.ForeignKey( + name: "FK_Shops_IceCreams_IceCreamId", + column: x => x.IceCreamId, + principalTable: "IceCreams", + principalColumn: "Id"); + }); + + migrationBuilder.CreateTable( + name: "IceCreams", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + IceCreamId = 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_ShopIcecreams", x => x.Id); + table.ForeignKey( + name: "FK_ShopIcecreams_IceCreams_IceCreamId", + column: x => x.IceCreamId, + principalTable: "IceCreams", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopIcecreams_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShopIcecreams_IceCreamId", + table: "IceCreams", + column: "IceCreamId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopIcecreams_ShopId", + table: "IceCreams", + column: "ShopId"); + + migrationBuilder.CreateIndex( + name: "IX_Shops_IceCreamId", + table: "Shops", + column: "IceCreamId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "IceCreams"); + + migrationBuilder.DropTable( + name: "Shops"); + + migrationBuilder.DropColumn( + name: "IceCreamName", + table: "Orders"); + } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs index df89698..e6ae098 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDatabaseModelSnapshot.cs @@ -120,11 +120,69 @@ namespace IceCreamShopDatabaseImplement.Migrations b.HasKey("Id"); - b.HasIndex("DocumentId"); + b.HasIndex("IceCreamId"); b.ToTable("Orders"); }); + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Adress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("IceCreamId") + .HasColumnType("int"); + + b.Property("IceCreamMaxCount") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.HasIndex("IceCreamId"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.ShopIcecream", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("IceCreamId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("IceCreamId"); + + b.HasIndex("ShopId"); + + b.ToTable("IceCreams"); + }); + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => { b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component") @@ -144,6 +202,41 @@ namespace IceCreamShopDatabaseImplement.Migrations b.Navigation("IceCream"); }); + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => + { + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", null) + .WithMany("Orders") + .HasForeignKey("IceCreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Shop", b => + { + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", null) + .WithMany("Shops") + .HasForeignKey("IceCreamId"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.ShopIcecream", b => + { + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") + .WithMany() + .HasForeignKey("IceCreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("IceCreamShopDatabaseImplement.Models.Shop", "Shop") + .WithMany("IceCreams") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("IceCream"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => { b.Navigation("IceCreamComponents"); @@ -154,6 +247,13 @@ namespace IceCreamShopDatabaseImplement.Migrations b.Navigation("Components"); b.Navigation("Orders"); + + b.Navigation("Shops"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Shop", b => + { + b.Navigation("IceCreams"); }); #pragma warning restore 612, 618 } diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs index 45b4267..360940d 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs @@ -26,7 +26,8 @@ namespace IceCreamShopDatabaseImplement.Models if (_iceCreamComponents == null) { _iceCreamComponents = Components - .ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + .ToDictionary(recPC => recPC.ComponentId, + recPC => (recPC.Component as IComponentModel, recPC.Count)); } return _iceCreamComponents; } @@ -37,6 +38,8 @@ namespace IceCreamShopDatabaseImplement.Models [ForeignKey("IceCreamId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("IceCreamId")] + public virtual List Shops { get; set; } = new(); public static IceCream Create(IceCreamShopDatabase context, IceCreamBindingModel model) { diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Shop.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..371888d --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Shop.cs @@ -0,0 +1,116 @@ +using AbstractIceCreamShopDataModels.Models; +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Net; + +namespace IceCreamShopDatabaseImplement.Models +{ + public class Shop : IShopModel + { + + [Required] + public string Name { get; private set; } = string.Empty; + + [Required] + public string Adress { get; private set; } = string.Empty; + + public DateTime OpeningDate { get; private set; } + + private Dictionary? _cashedIcecreams = null; + + [NotMapped] + public Dictionary ShopIceCreams + { + get + { + if(_cashedIcecreams == null) + { + _cashedIcecreams = IceCreams.ToDictionary(recPC => recPC.IceCreamId, recPC => (recPC.IceCream as IIceCreamModel, recPC.Count)); + } + return _cashedIcecreams; + } + } + + [Required] + public int IceCreamMaxCount { get; private set; } + + public int Id { get; private set; } + + [ForeignKey("ShopId")] + public virtual List IceCreams { get; set; } = new(); + + public static Shop? Create(IceCreamShopDatabase context, ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + Name = model.Name, + Adress = model.Adress, + OpeningDate = model.OpeningDate, + IceCreamMaxCount = model.IceCreamMaxCount, + IceCreams = model.ShopIceCreams.Select(x => new ShopIcecream + { + IceCream = context.IceCreams.First(y => y.Id == x.Key)!, + Count = x.Value.Item2, + }).ToList() + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + Name = model.Name; + Adress = model.Adress; + OpeningDate = model.OpeningDate; + IceCreamMaxCount = model.IceCreamMaxCount; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Adress = Adress, + ShopIceCreams = ShopIceCreams, + OpeningDate = OpeningDate, + IceCreamMaxCount = IceCreamMaxCount, + }; + + public void UpdatePastries(IceCreamShopDatabase context, ShopBindingModel model) + { + var iceCreams = context.ShopIcecreams.Where(rec => rec.ShopId == model.Id).ToList(); + if (iceCreams != null && iceCreams.Count > 0) + { + // удалили те, которых нет в модели + context.ShopIcecreams.RemoveRange(iceCreams.Where(rec => !model.ShopIceCreams.ContainsKey(rec.IceCreamId))); + context.SaveChanges(); + iceCreams = context.ShopIcecreams.Where(rec => rec.ShopId == model.Id).ToList(); + // обновили количество у существующих записей + foreach (var updateIceCream in iceCreams) + { + updateIceCream.Count = model.ShopIceCreams[updateIceCream.IceCreamId].Item2; + model.ShopIceCreams.Remove(updateIceCream.IceCreamId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var elem in model.ShopIceCreams) + { + context.ShopIcecreams.Add(new ShopIcecream + { + Shop = shop, + IceCream = context.IceCreams.First(x => x.Id == elem.Key), + Count = elem.Value.Item2 + }); + context.SaveChanges(); + } + _cashedIcecreams = null; + } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/ShopIcecream.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/ShopIcecream.cs new file mode 100644 index 0000000..b09c7f4 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/ShopIcecream.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace IceCreamShopDatabaseImplement.Models +{ + public class ShopIcecream + { + public int Id { get; set; } + [Required] + public int IceCreamId { get; set; } + [Required] + public int ShopId { get; set; } + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + public virtual IceCream IceCream { get; set; } = new(); + } +} -- 2.25.1 From 91e2b800aafc98e117f37b4303336150dbf425d7 Mon Sep 17 00:00:00 2001 From: Inohara Date: Tue, 11 Apr 2023 09:40:58 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=D1=81=D0=B4=D0=B0=D0=BD=D0=B0=203=20=D1=83?= =?UTF-8?q?=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=B5=D0=BD=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IceCreamShopDatabaseImplement/Implements/ShopStorage.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ShopStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ShopStorage.cs index d0d2491..8aa7a74 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ShopStorage.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ShopStorage.cs @@ -128,9 +128,8 @@ namespace IceCreamShopDatabaseImplement.Implements transaction.Commit(); } else - { transaction.Rollback(); - } + return needCount == 0; } } -- 2.25.1