From 3182115804b8e7f828edec9b99c88b3788caa219 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Tue, 21 Mar 2023 18:05:59 +0400 Subject: [PATCH 1/4] LabWork_03 completed --- FishFactory/FishFactory.sln | 6 + .../FishFactory/FishFactoryView.csproj | 1 + FishFactory/FishFactory/Program.cs | 2 +- .../FishFactoryBusinessLogic/OrderLogic.cs | 2 +- .../BindingModels/OrderBindingModel.cs | 2 +- .../FishFactoryDatabase.cs | 27 +++ .../FishFactoryDatabaseImplement.csproj | 19 ++ .../Implements/CannedStorage.cs | 111 ++++++++++++ .../Implements/ComponentStorage.cs | 87 +++++++++ .../Implements/OrderStorage.cs | 105 +++++++++++ .../20230227185524_MigrateInit.Designer.cs | 169 ++++++++++++++++++ .../Migrations/20230227185524_MigrateInit.cs | 126 +++++++++++++ .../FishFactoryDatabaseModelSnapshot.cs | 166 +++++++++++++++++ .../Models/Canned.cs | 93 ++++++++++ .../Models/CannedComponent.cs | 22 +++ .../Models/Component.cs | 61 +++++++ .../Models/Order.cs | 90 ++++++++++ 17 files changed, 1086 insertions(+), 3 deletions(-) create mode 100644 FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj create mode 100644 FishFactory/FishFactoryDatabaseImplement/Implements/CannedStorage.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Implements/OrderStorage.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Migrations/20230227185524_MigrateInit.Designer.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Migrations/20230227185524_MigrateInit.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Models/Canned.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Models/CannedComponent.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Models/Component.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Models/Order.cs diff --git a/FishFactory/FishFactory.sln b/FishFactory/FishFactory.sln index 802abb8..1d146e7 100644 --- a/FishFactory/FishFactory.sln +++ b/FishFactory/FishFactory.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FishFactoryListImplement", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FishFactoryFileImplement", "FishFactoryFileImplement\FishFactoryFileImplement.csproj", "{6C503B98-AEB3-4322-B8DF-1C0E3E294041}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FishFactoryDatabaseImplement", "FishFactoryDatabaseImplement\FishFactoryDatabaseImplement.csproj", "{CFFEEB1E-413D-4624-8C30-E6D8860A9819}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {6C503B98-AEB3-4322-B8DF-1C0E3E294041}.Debug|Any CPU.Build.0 = Debug|Any CPU {6C503B98-AEB3-4322-B8DF-1C0E3E294041}.Release|Any CPU.ActiveCfg = Release|Any CPU {6C503B98-AEB3-4322-B8DF-1C0E3E294041}.Release|Any CPU.Build.0 = Release|Any CPU + {CFFEEB1E-413D-4624-8C30-E6D8860A9819}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CFFEEB1E-413D-4624-8C30-E6D8860A9819}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CFFEEB1E-413D-4624-8C30-E6D8860A9819}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CFFEEB1E-413D-4624-8C30-E6D8860A9819}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FishFactory/FishFactory/FishFactoryView.csproj b/FishFactory/FishFactory/FishFactoryView.csproj index 81b3f1e..3125cd1 100644 --- a/FishFactory/FishFactory/FishFactoryView.csproj +++ b/FishFactory/FishFactory/FishFactoryView.csproj @@ -16,6 +16,7 @@ + diff --git a/FishFactory/FishFactory/Program.cs b/FishFactory/FishFactory/Program.cs index 37f9e55..d9edc45 100644 --- a/FishFactory/FishFactory/Program.cs +++ b/FishFactory/FishFactory/Program.cs @@ -4,7 +4,7 @@ using FishFactoryContracts.StoragesContracts; using FishFactoryView; using Microsoft.Extensions.DependencyInjection; using System; -using FishFactoryFileImplement.Implements; +using FishFactoryDatabaseImplement.Implements; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/FishFactory/FishFactoryBusinessLogic/OrderLogic.cs b/FishFactory/FishFactoryBusinessLogic/OrderLogic.cs index e53b151..d2834fa 100644 --- a/FishFactory/FishFactoryBusinessLogic/OrderLogic.cs +++ b/FishFactory/FishFactoryBusinessLogic/OrderLogic.cs @@ -60,7 +60,7 @@ namespace FishFactoryBusinessLogic.BusinessLogics { model.Status = newStatus; if (model.Status == OrderStatus.Выдан) - model.DateImplement = DateTime.Now; + model.DateImplement = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); if (_orderStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); diff --git a/FishFactory/FishFactoryContracts/BindingModels/OrderBindingModel.cs b/FishFactory/FishFactoryContracts/BindingModels/OrderBindingModel.cs index 0fb0eed..58def85 100644 --- a/FishFactory/FishFactoryContracts/BindingModels/OrderBindingModel.cs +++ b/FishFactory/FishFactoryContracts/BindingModels/OrderBindingModel.cs @@ -15,7 +15,7 @@ namespace FishFactoryContracts.BindingModels public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public DateTime? DateImplement { get; set; } } } diff --git a/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs b/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs new file mode 100644 index 0000000..31b8fd9 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs @@ -0,0 +1,27 @@ +using FishFactoryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using Npgsql.EntityFrameworkCore.PostgreSQL; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryDatabaseImplement +{ + public class FishFactoryDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql("Host=localhost;Port=5432;Database=FishFactory;Username=postgres;Password=postgres"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Canneds { set; get; } + public virtual DbSet CannedComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj b/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj new file mode 100644 index 0000000..b53e457 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj @@ -0,0 +1,19 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + diff --git a/FishFactory/FishFactoryDatabaseImplement/Implements/CannedStorage.cs b/FishFactory/FishFactoryDatabaseImplement/Implements/CannedStorage.cs new file mode 100644 index 0000000..911edf1 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Implements/CannedStorage.cs @@ -0,0 +1,111 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryDatabaseImplement.Implements +{ + public class CannedStorage : ICannedStorage + { + public List GetFullList() + { + using var context = new FishFactoryDatabase(); + return context.Canneds + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(CannedSearchModel model) + { + if (string.IsNullOrEmpty(model.CannedName)) + { + return new(); + } + using var context = new FishFactoryDatabase(); + return context.Canneds + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.CannedName.Contains(model.CannedName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public CannedViewModel? GetElement(CannedSearchModel model) + { + if (string.IsNullOrEmpty(model.CannedName) && + !model.Id.HasValue) + { + return null; + } + using var context = new FishFactoryDatabase(); + return context.Canneds + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.CannedName) && + x.CannedName == model.CannedName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public CannedViewModel? Insert(CannedBindingModel model) + { + using var context = new FishFactoryDatabase(); + var newCanned = Canned.Create(context, model); + if (newCanned == null) + { + return null; + } + context.Canneds.Add(newCanned); + context.SaveChanges(); + return newCanned.GetViewModel; + } + public CannedViewModel? Update(CannedBindingModel model) + { + using var context = new FishFactoryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Canneds.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateComponents(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public CannedViewModel? Delete(CannedBindingModel model) + { + using var context = new FishFactoryDatabase(); + var element = context.Canneds + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Canneds.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Implements/ComponentStorage.cs b/FishFactory/FishFactoryDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..6676df5 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,87 @@ + using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new FishFactoryDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ComponentSearchModel + model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new FishFactoryDatabase(); + 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 FishFactoryDatabase(); + 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 FishFactoryDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new FishFactoryDatabase(); + 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 FishFactoryDatabase(); + 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/FishFactory/FishFactoryDatabaseImplement/Implements/OrderStorage.cs b/FishFactory/FishFactoryDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..97ae408 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,105 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new FishFactoryDatabase(); + + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + + return GetViewModel(element); + } + + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new FishFactoryDatabase(); + + return GetViewModel(context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new FishFactoryDatabase(); + + return context.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); + } + + public List GetFullList() + { + using var context = new FishFactoryDatabase(); + + return context.Orders.Select(x => GetViewModel(x)).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + + using var context = new FishFactoryDatabase(); + + context.Orders.Add(newOrder); + context.SaveChanges(); + return GetViewModel(newOrder); + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new FishFactoryDatabase(); + + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + + if (order == null) + { + return null; + } + + order.Update(model); + context.SaveChanges(); + + return GetViewModel(order); + } + + private static OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + using var context = new FishFactoryDatabase(); + var element = context.Canneds.FirstOrDefault(x => x.Id == order.CannedId); + viewModel.CannedName = element.CannedName; + return viewModel; + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Migrations/20230227185524_MigrateInit.Designer.cs b/FishFactory/FishFactoryDatabaseImplement/Migrations/20230227185524_MigrateInit.Designer.cs new file mode 100644 index 0000000..0be7f32 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Migrations/20230227185524_MigrateInit.Designer.cs @@ -0,0 +1,169 @@ +// +using System; +using FishFactoryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace FishFactoryDatabaseImplement.Migrations +{ + [DbContext(typeof(FishFactoryDatabase))] + [Migration("20230227185524_MigrateInit")] + partial class MigrateInit + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Canneds"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("integer"); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.HasIndex("ComponentId"); + + b.ToTable("CannedComponents"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany("Components") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FishFactoryDatabaseImplement.Models.Component", "Component") + .WithMany("CannedComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + + b.Navigation("Component"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", null) + .WithMany("Orders") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Component", b => + { + b.Navigation("CannedComponents"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Migrations/20230227185524_MigrateInit.cs b/FishFactory/FishFactoryDatabaseImplement/Migrations/20230227185524_MigrateInit.cs new file mode 100644 index 0000000..f2e96df --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Migrations/20230227185524_MigrateInit.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace FishFactoryDatabaseImplement.Migrations +{ + /// + public partial class MigrateInit : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Canneds", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + CannedName = table.Column(type: "text", nullable: false), + Price = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Canneds", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ComponentName = table.Column(type: "text", nullable: false), + Cost = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + CannedId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false), + Sum = table.Column(type: "double precision", nullable: false), + Status = table.Column(type: "integer", nullable: false), + DateCreate = table.Column(type: "timestamp with time zone", nullable: false), + DateImplement = table.Column(type: "timestamp with time zone", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Canneds_CannedId", + column: x => x.CannedId, + principalTable: "Canneds", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "CannedComponents", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + CannedId = table.Column(type: "integer", nullable: false), + ComponentId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_CannedComponents", x => x.Id); + table.ForeignKey( + name: "FK_CannedComponents_Canneds_CannedId", + column: x => x.CannedId, + principalTable: "Canneds", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_CannedComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_CannedComponents_CannedId", + table: "CannedComponents", + column: "CannedId"); + + migrationBuilder.CreateIndex( + name: "IX_CannedComponents_ComponentId", + table: "CannedComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_CannedId", + table: "Orders", + column: "CannedId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "CannedComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Canneds"); + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs b/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs new file mode 100644 index 0000000..69adfec --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs @@ -0,0 +1,166 @@ +// +using System; +using FishFactoryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace FishFactoryDatabaseImplement.Migrations +{ + [DbContext(typeof(FishFactoryDatabase))] + partial class FishFactoryDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Canneds"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("integer"); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.HasIndex("ComponentId"); + + b.ToTable("CannedComponents"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany("Components") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FishFactoryDatabaseImplement.Models.Component", "Component") + .WithMany("CannedComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + + b.Navigation("Component"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", null) + .WithMany("Orders") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Component", b => + { + b.Navigation("CannedComponents"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Models/Canned.cs b/FishFactory/FishFactoryDatabaseImplement/Models/Canned.cs new file mode 100644 index 0000000..5cf70bf --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Models/Canned.cs @@ -0,0 +1,93 @@ +using FishFactoryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; + +namespace FishFactoryDatabaseImplement.Models +{ + public class Canned : ICannedModel + { + public int Id { get; set; } + [Required] + public string CannedName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _cannedComponents = null; + [NotMapped] + public Dictionary CannedComponents + { + get + { + if (_cannedComponents == null) + { + _cannedComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _cannedComponents; + } + } + [ForeignKey("CannedId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("CannedId")] + public virtual List Orders { get; set; } = new(); + public static Canned Create(FishFactoryDatabase context, CannedBindingModel model) + { + return new Canned() + { + Id = model.Id, + CannedName = model.CannedName, + Price = model.Price, + Components = model.CannedComponents.Select(x => new CannedComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(CannedBindingModel model) + { + CannedName = model.CannedName; + Price = model.Price; + } + public CannedViewModel GetViewModel => new() + { + Id = Id, + CannedName = CannedName, + Price = Price, + CannedComponents = CannedComponents + }; + public void UpdateComponents(FishFactoryDatabase context, CannedBindingModel model) + { + var cannedComponents = context.CannedComponents.Where(rec => rec.CannedId == model.Id).ToList(); + if (cannedComponents != null && cannedComponents.Count > 0) + { // удалили те, которых нет в модели + context.CannedComponents.RemoveRange(cannedComponents.Where(rec => !model.CannedComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in cannedComponents) + { + updateComponent.Count = model.CannedComponents[updateComponent.ComponentId].Item2; + model.CannedComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var canned = context.Canneds.First(x => x.Id == Id); + foreach (var pc in model.CannedComponents) + { + context.CannedComponents.Add(new CannedComponent + { + Canned = canned, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _cannedComponents = null; + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Models/CannedComponent.cs b/FishFactory/FishFactoryDatabaseImplement/Models/CannedComponent.cs new file mode 100644 index 0000000..904054f --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Models/CannedComponent.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryDatabaseImplement.Models +{ + public class CannedComponent + { + public int Id { get; set; } + [Required] + public int CannedId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Canned Canned { get; set; } = new(); + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Models/Component.cs b/FishFactory/FishFactoryDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..2b46641 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Models/Component.cs @@ -0,0 +1,61 @@ +using FishFactoryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; + +namespace FishFactoryDatabaseImplement.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 CannedComponents { 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/FishFactory/FishFactoryDatabaseImplement/Models/Order.cs b/FishFactory/FishFactoryDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..57b8c01 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Models/Order.cs @@ -0,0 +1,90 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModels.Enums; +using FishFactoryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; set; } + [Required] + public int CannedId { get; set; } + + [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 static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + CannedId = model.CannedId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + public static Order Create(OrderViewModel model) + { + return new Order + { + Id = model.Id, + CannedId = model.CannedId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Id = model.Id; + CannedId = model.CannedId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + CannedId = CannedId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} -- 2.25.1 From f946e461074e50dfe1984e412887c78f0a45c820 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Wed, 3 May 2023 21:25:13 +0400 Subject: [PATCH 2/4] =?UTF-8?q?=D0=9F=D0=BE=D1=84=D0=B8=D0=BA=D1=88=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BA=D0=BE=D0=BD=D1=84=D0=BB=D0=B8=D0=BA=D1=82?= =?UTF-8?q?=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FishFactory/FishFactoryBusinessLogic/OrderLogic.cs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/FishFactory/FishFactoryBusinessLogic/OrderLogic.cs b/FishFactory/FishFactoryBusinessLogic/OrderLogic.cs index 0e5df1c..cf52fd2 100644 --- a/FishFactory/FishFactoryBusinessLogic/OrderLogic.cs +++ b/FishFactory/FishFactoryBusinessLogic/OrderLogic.cs @@ -81,19 +81,6 @@ namespace FishFactoryBusinessLogic.BusinessLogics return false; } return true; - } - model.Status = newStatus; - if (model.Status == OrderStatus.Выдан) - model.DateImplement = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); - if (_orderStorage.Update(model) == null) - { - _logger.LogWarning("Update operation failed"); - return false; - } - return true; - } - _logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, newStatus); - throw new ArgumentException($"Невозможно приствоить статус {newStatus} заказу с текущим статусом {model.Status}"); } public bool DeliveryOrder(OrderBindingModel model) { -- 2.25.1 From adc5aed3d8cc1057b3ae0b0d938a9cde1d1e7c27 Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Thu, 4 May 2023 21:33:10 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=203=20=D0=BB=D0=B0=D0=B1=D0=B0=20=D1=85=D0=B0?= =?UTF-8?q?=D1=80=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .../FishFactory/FishFactoryView.csproj | 4 + FishFactory/FishFactory/FormMain.Designer.cs | 1 + FishFactory/FishFactory/FormMain.cs | 18 +- FishFactory/FishFactory/FormShop.cs | 2 +- .../FishFactoryBusinessLogic/OrderLogic.cs | 2 +- .../BindingModels/ShopBindingModel.cs | 2 +- .../FishFactoryDatabase.cs | 3 + .../FishFactoryDatabaseImplement.csproj | 8 +- .../Implements/OrderStorage.cs | 25 +- .../Implements/ShopStorage.cs | 155 +++++++++++ .../20230504165216_Init3.Designer.cs | 246 +++++++++++++++++ .../Migrations/20230504165216_Init3.cs | 79 ++++++ .../20230504172335_Init4.Designer.cs | 248 ++++++++++++++++++ .../Migrations/20230504172335_Init4.cs | 22 ++ .../FishFactoryDatabaseModelSnapshot.cs | 83 +++++- .../Models/Order.cs | 8 +- .../Models/Shop.cs | 109 ++++++++ .../Models/ShopCanned.cs | 27 ++ 19 files changed, 1011 insertions(+), 32 deletions(-) create mode 100644 FishFactory/FishFactoryDatabaseImplement/Implements/ShopStorage.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Migrations/20230504165216_Init3.Designer.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Migrations/20230504165216_Init3.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Migrations/20230504172335_Init4.Designer.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Migrations/20230504172335_Init4.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Models/Shop.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Models/ShopCanned.cs diff --git a/.gitignore b/.gitignore index ca1c7a3..de3e3ff 100644 --- a/.gitignore +++ b/.gitignore @@ -398,3 +398,4 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml +/FishFactory/ImplementationExtensions diff --git a/FishFactory/FishFactory/FishFactoryView.csproj b/FishFactory/FishFactory/FishFactoryView.csproj index 3125cd1..c850c79 100644 --- a/FishFactory/FishFactory/FishFactoryView.csproj +++ b/FishFactory/FishFactory/FishFactoryView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/FishFactory/FishFactory/FormMain.Designer.cs b/FishFactory/FishFactory/FormMain.Designer.cs index 9695d2e..18181c9 100644 --- a/FishFactory/FishFactory/FormMain.Designer.cs +++ b/FishFactory/FishFactory/FormMain.Designer.cs @@ -186,6 +186,7 @@ this.MainMenuStrip = this.menuStrip1; this.Name = "FormMain"; this.Text = "Рыбный завод"; + this.Load += new System.EventHandler(this.FormMain_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.menuStrip1.ResumeLayout(false); this.menuStrip1.PerformLayout(); diff --git a/FishFactory/FishFactory/FormMain.cs b/FishFactory/FishFactory/FormMain.cs index 4c7f6f6..66ae6ef 100644 --- a/FishFactory/FishFactory/FormMain.cs +++ b/FishFactory/FishFactory/FormMain.cs @@ -82,16 +82,10 @@ namespace FishFactoryView int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); _logger.LogInformation("Заказ №{id}. Меняется статус на 'В работе'", id); - try{ + try{ var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel - { Id = id - /*CannedId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["CannedId"].Value), - *//*CannedName = dataGridView.SelectedRows[0].Cells["CannedName"].Value.ToString,*//* - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()),*/ - + { + Id = id }); if (!operationResult) { @@ -118,12 +112,6 @@ namespace FishFactoryView var operationResult = _orderLogic.FinishOrder(new OrderBindingModel { Id = id, - CannedId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["CannedId"].Value), - /*CannedName = dataGridView.SelectedRows[0].Cells["CannedName"].Value.ToString,*/ - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), }); if (!operationResult) { diff --git a/FishFactory/FishFactory/FormShop.cs b/FishFactory/FishFactory/FormShop.cs index 4549243..32228bf 100644 --- a/FishFactory/FishFactory/FormShop.cs +++ b/FishFactory/FishFactory/FormShop.cs @@ -101,7 +101,7 @@ namespace FishFactoryView Id = _id ?? 0, ShopName = textBoxShop.Text, Address = textBoxAddress.Text, - DateOpening = dateTimePicker.Value.Date, + DateOpening = DateTime.SpecifyKind(DateTime.Parse(dateTimePicker.Text), DateTimeKind.Utc), ListCanneds = _shopListCanneds, MaxCountCanneds = (int)numericUpDownCount.Value }; diff --git a/FishFactory/FishFactoryBusinessLogic/OrderLogic.cs b/FishFactory/FishFactoryBusinessLogic/OrderLogic.cs index cf52fd2..13402b2 100644 --- a/FishFactory/FishFactoryBusinessLogic/OrderLogic.cs +++ b/FishFactory/FishFactoryBusinessLogic/OrderLogic.cs @@ -58,7 +58,7 @@ namespace FishFactoryBusinessLogic.BusinessLogics model.Status = newStatus; if (model.Status == OrderStatus.Готов) { - model.DateImplement = DateTime.Now; + model.DateImplement = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); var canned = _cannedStorage.GetElement(new() { Id = viewModel.CannedId }); if (canned == null) { diff --git a/FishFactory/FishFactoryContracts/BindingModels/ShopBindingModel.cs b/FishFactory/FishFactoryContracts/BindingModels/ShopBindingModel.cs index b829394..ba3bc7f 100644 --- a/FishFactory/FishFactoryContracts/BindingModels/ShopBindingModel.cs +++ b/FishFactory/FishFactoryContracts/BindingModels/ShopBindingModel.cs @@ -15,7 +15,7 @@ namespace FishFactoryContracts.BindingModels public string Address { get; set; } = string.Empty; - public DateTime DateOpening { get; set; } = DateTime.Now; + public DateTime DateOpening { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public Dictionary ListCanneds { diff --git a/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs b/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs index 31b8fd9..537633c 100644 --- a/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs +++ b/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs @@ -4,6 +4,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL; using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.Intrinsics.Arm; using System.Text; using System.Threading.Tasks; @@ -23,5 +24,7 @@ namespace FishFactoryDatabaseImplement public virtual DbSet Canneds { set; get; } public virtual DbSet CannedComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopCanned { set; get; } } } diff --git a/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj b/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj index b53e457..802d2cc 100644 --- a/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj +++ b/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj @@ -7,8 +7,12 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/FishFactory/FishFactoryDatabaseImplement/Implements/OrderStorage.cs b/FishFactory/FishFactoryDatabaseImplement/Implements/OrderStorage.cs index 97ae408..34d5ea0 100644 --- a/FishFactory/FishFactoryDatabaseImplement/Implements/OrderStorage.cs +++ b/FishFactory/FishFactoryDatabaseImplement/Implements/OrderStorage.cs @@ -3,6 +3,7 @@ using FishFactoryContracts.SearchModels; using FishFactoryContracts.StoragesContracts; using FishFactoryContracts.ViewModels; using FishFactoryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; @@ -39,7 +40,9 @@ namespace FishFactoryDatabaseImplement.Implements using var context = new FishFactoryDatabase(); - return GetViewModel(context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); + return GetViewModel(context.Orders + .Include(x => x.Canned) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); } public List GetFilteredList(OrderSearchModel model) @@ -51,14 +54,21 @@ namespace FishFactoryDatabaseImplement.Implements using var context = new FishFactoryDatabase(); - return context.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); + return context.Orders + .Include(x => x.Canned) + .Where(x => x.Id == model.Id) + .Select(x => GetViewModel(x)) + .ToList(); } public List GetFullList() { using var context = new FishFactoryDatabase(); - return context.Orders.Select(x => GetViewModel(x)).ToList(); + return context.Orders + .Include(x => x.Canned) + .Select(x => GetViewModel(x)) + .ToList(); } public OrderViewModel? Insert(OrderBindingModel model) @@ -73,14 +83,19 @@ namespace FishFactoryDatabaseImplement.Implements context.Orders.Add(newOrder); context.SaveChanges(); - return GetViewModel(newOrder); + return context.Orders + .Include(x => x.Canned) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) { using var context = new FishFactoryDatabase(); - var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + var order = context.Orders + .Include(x => x.Canned) + .FirstOrDefault(x => x.Id == model.Id); if (order == null) { diff --git a/FishFactory/FishFactoryDatabaseImplement/Implements/ShopStorage.cs b/FishFactory/FishFactoryDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..4becaaf --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,155 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDatabaseImplement.Models; +using FishFactoryDataModels.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new FishFactoryDatabase(); + return context.Shops + .Include(x => x.ListCannedFk) + .ThenInclude(x => x.Canned) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new FishFactoryDatabase(); + return context.Shops + .Include(x => x.ListCannedFk) + .ThenInclude(x => x.Canned) + .Where(x => x.ShopName.Contains(model.ShopName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + using var context = new FishFactoryDatabase(); + return context.Shops + .Include(x => x.ListCannedFk) + .ThenInclude(x => x.Canned) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new FishFactoryDatabase(); + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new FishFactoryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + context.SaveChanges(); + shop.UpdateSushi(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new FishFactoryDatabase(); + var element = context.Shops + .Include(x => x.ListCannedFk) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + public bool SellCanneds(ICannedModel model, int count) + { + using var context = new FishFactoryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + foreach (var shop in context.Shops + .Include(x => x.ListCannedFk) + .ThenInclude(x => x.Canned) + .ToList() + .Where(x => x.ListCanneds.ContainsKey(model.Id))) + { + int countInCurrentShop = shop.ListCanneds[model.Id].Item2; + if (countInCurrentShop <= count) + { + var elem = context.ShopCanned + .Where(x => x.CannedId == model.Id) + .FirstOrDefault(x => x.ShopId == shop.Id); + context.ShopCanned.Remove(elem); + shop.ListCanneds.Remove(model.Id); + count -= countInCurrentShop; + } + else + { + shop.ListCanneds[model.Id] = (shop.ListCanneds[model.Id].Item1, countInCurrentShop - count); + count = 0; + shop.UpdateSushi(context, new() + { + Id = shop.Id, + ListCanneds = shop.ListCanneds, + }); + } + if (count == 0) + { + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Migrations/20230504165216_Init3.Designer.cs b/FishFactory/FishFactoryDatabaseImplement/Migrations/20230504165216_Init3.Designer.cs new file mode 100644 index 0000000..ec3cf39 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Migrations/20230504165216_Init3.Designer.cs @@ -0,0 +1,246 @@ +// +using System; +using FishFactoryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace FishFactoryDatabaseImplement.Migrations +{ + [DbContext(typeof(FishFactoryDatabase))] + [Migration("20230504165216_Init3")] + partial class Init3 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Canneds"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("integer"); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.HasIndex("ComponentId"); + + b.ToTable("CannedComponents"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("text"); + + b.Property("DateOpening") + .HasColumnType("timestamp with time zone"); + + b.Property("MaxCountCanneds") + .HasColumnType("integer"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.ShopCanned", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("ShopId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopCanned"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany("Components") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FishFactoryDatabaseImplement.Models.Component", "Component") + .WithMany("CannedComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + + b.Navigation("Component"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", null) + .WithMany("Orders") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.ShopCanned", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany() + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FishFactoryDatabaseImplement.Models.Shop", "Shop") + .WithMany("ListCannedFk") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Component", b => + { + b.Navigation("CannedComponents"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Shop", b => + { + b.Navigation("ListCannedFk"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Migrations/20230504165216_Init3.cs b/FishFactory/FishFactoryDatabaseImplement/Migrations/20230504165216_Init3.cs new file mode 100644 index 0000000..88020ca --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Migrations/20230504165216_Init3.cs @@ -0,0 +1,79 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace FishFactoryDatabaseImplement.Migrations +{ + /// + public partial class Init3 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ShopName = table.Column(type: "text", nullable: false), + Address = table.Column(type: "text", nullable: false), + DateOpening = table.Column(type: "timestamp with time zone", nullable: false), + MaxCountCanneds = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ShopCanned", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + CannedId = table.Column(type: "integer", nullable: false), + ShopId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopCanned", x => x.Id); + table.ForeignKey( + name: "FK_ShopCanned_Canneds_CannedId", + column: x => x.CannedId, + principalTable: "Canneds", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopCanned_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShopCanned_CannedId", + table: "ShopCanned", + column: "CannedId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopCanned_ShopId", + table: "ShopCanned", + column: "ShopId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShopCanned"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Migrations/20230504172335_Init4.Designer.cs b/FishFactory/FishFactoryDatabaseImplement/Migrations/20230504172335_Init4.Designer.cs new file mode 100644 index 0000000..e323349 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Migrations/20230504172335_Init4.Designer.cs @@ -0,0 +1,248 @@ +// +using System; +using FishFactoryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace FishFactoryDatabaseImplement.Migrations +{ + [DbContext(typeof(FishFactoryDatabase))] + [Migration("20230504172335_Init4")] + partial class Init4 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Canneds"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("integer"); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.HasIndex("ComponentId"); + + b.ToTable("CannedComponents"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("text"); + + b.Property("DateOpening") + .HasColumnType("timestamp with time zone"); + + b.Property("MaxCountCanneds") + .HasColumnType("integer"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.ShopCanned", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("ShopId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopCanned"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany("Components") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FishFactoryDatabaseImplement.Models.Component", "Component") + .WithMany("CannedComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + + b.Navigation("Component"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany("Orders") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.ShopCanned", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany() + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FishFactoryDatabaseImplement.Models.Shop", "Shop") + .WithMany("ListCannedFk") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Component", b => + { + b.Navigation("CannedComponents"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Shop", b => + { + b.Navigation("ListCannedFk"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Migrations/20230504172335_Init4.cs b/FishFactory/FishFactoryDatabaseImplement/Migrations/20230504172335_Init4.cs new file mode 100644 index 0000000..57a463c --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Migrations/20230504172335_Init4.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace FishFactoryDatabaseImplement.Migrations +{ + /// + public partial class Init4 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs b/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs index 69adfec..cb0cdae 100644 --- a/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs +++ b/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs @@ -17,7 +17,7 @@ namespace FishFactoryDatabaseImplement.Migrations { #pragma warning disable 612, 618 modelBuilder - .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("ProductVersion", "7.0.5") .HasAnnotation("Relational:MaxIdentifierLength", 63); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); @@ -121,6 +121,59 @@ namespace FishFactoryDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("text"); + + b.Property("DateOpening") + .HasColumnType("timestamp with time zone"); + + b.Property("MaxCountCanneds") + .HasColumnType("integer"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.ShopCanned", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("ShopId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopCanned"); + }); + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b => { b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") @@ -142,11 +195,32 @@ namespace FishFactoryDatabaseImplement.Migrations modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => { - b.HasOne("FishFactoryDatabaseImplement.Models.Canned", null) + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") .WithMany("Orders") .HasForeignKey("CannedId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.Navigation("Canned"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.ShopCanned", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany() + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FishFactoryDatabaseImplement.Models.Shop", "Shop") + .WithMany("ListCannedFk") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + + b.Navigation("Shop"); }); modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => @@ -160,6 +234,11 @@ namespace FishFactoryDatabaseImplement.Migrations { b.Navigation("CannedComponents"); }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Shop", b => + { + b.Navigation("ListCannedFk"); + }); #pragma warning restore 612, 618 } } diff --git a/FishFactory/FishFactoryDatabaseImplement/Models/Order.cs b/FishFactory/FishFactoryDatabaseImplement/Models/Order.cs index 57b8c01..d87eaf9 100644 --- a/FishFactory/FishFactoryDatabaseImplement/Models/Order.cs +++ b/FishFactory/FishFactoryDatabaseImplement/Models/Order.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; +using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; @@ -31,6 +32,8 @@ namespace FishFactoryDatabaseImplement.Models public DateTime? DateImplement { get; set; } + public virtual Canned Canned { get; set; } + public static Order? Create(OrderBindingModel model) { if (model == null) @@ -68,12 +71,7 @@ namespace FishFactoryDatabaseImplement.Models { return; } - Id = model.Id; - CannedId = model.CannedId; - Count = model.Count; - Sum = model.Sum; Status = model.Status; - DateCreate = model.DateCreate; DateImplement = model.DateImplement; } public OrderViewModel GetViewModel => new() diff --git a/FishFactory/FishFactoryDatabaseImplement/Models/Shop.cs b/FishFactory/FishFactoryDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..573667d --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Models/Shop.cs @@ -0,0 +1,109 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FishFactoryDataModels.Models; + +namespace FishFactoryDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + [Required] + public string ShopName { get; private set; } = string.Empty; + [Required] + public string Address { get; private set; } = string.Empty; + [Required] + public DateTime DateOpening { get; private set; } + [Required] + public int MaxCountCanneds { get; private set; } + + private Dictionary? _shopCanned = null; + + [NotMapped] + public Dictionary ListCanneds + { + get + { + if (_shopCanned == null) + { + _shopCanned = ListCannedFk.ToDictionary(recPC => recPC.CannedId, recPC => (recPC.Canned as ICannedModel, recPC.Count)); + } + return _shopCanned; + } + } + + [ForeignKey("ShopId")] + public virtual List ListCannedFk { get; set; } = new(); + + public static Shop Create(FishFactoryDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + MaxCountCanneds = model.MaxCountCanneds, + ListCannedFk = model.ListCanneds.Select(x => new ShopCanned + { + Canned = context.Canneds.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Address = model.Address; + DateOpening = model.DateOpening; + MaxCountCanneds = model.MaxCountCanneds; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + MaxCountCanneds = MaxCountCanneds, + ListCanneds = ListCanneds + }; + + public void UpdateSushi(FishFactoryDatabase context, ShopBindingModel model) + { + var shopCanned = context.ShopCanned.Where(rec => rec.ShopId == model.Id).ToList(); + if (shopCanned != null && shopCanned.Count > 0) + { // удалили те, которых нет в модели + context.ShopCanned.RemoveRange(shopCanned.Where(rec => !model.ListCanneds.ContainsKey(rec.CannedId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateCanned in shopCanned) + { + updateCanned.Count = model.ListCanneds[updateCanned.CannedId].Item2; + model.ListCanneds.Remove(updateCanned.CannedId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var ss in model.ListCanneds) + { + context.ShopCanned.Add(new ShopCanned + { + Shop = shop, + Canned = context.Canneds.First(x => x.Id == ss.Key), + Count = ss.Value.Item2 + }); + context.SaveChanges(); + } + _shopCanned = null; + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Models/ShopCanned.cs b/FishFactory/FishFactoryDatabaseImplement/Models/ShopCanned.cs new file mode 100644 index 0000000..980e85f --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Models/ShopCanned.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryDatabaseImplement.Models +{ + public class ShopCanned + { + public int Id { get; set; } + + [Required] + public int CannedId { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + + public virtual Canned Canned { get; set; } = new(); + } +} -- 2.25.1 From dd808b4e5fc19e696822ba22b4362082e62221eb Mon Sep 17 00:00:00 2001 From: ArtemEmelyanov Date: Fri, 5 May 2023 14:56:04 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D1=81=D1=83=D0=BF=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FishFactoryDatabaseImplement/Implements/ShopStorage.cs | 4 ++-- FishFactory/FishFactoryDatabaseImplement/Models/Shop.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/FishFactory/FishFactoryDatabaseImplement/Implements/ShopStorage.cs b/FishFactory/FishFactoryDatabaseImplement/Implements/ShopStorage.cs index 4becaaf..f177ba4 100644 --- a/FishFactory/FishFactoryDatabaseImplement/Implements/ShopStorage.cs +++ b/FishFactory/FishFactoryDatabaseImplement/Implements/ShopStorage.cs @@ -79,7 +79,7 @@ namespace FishFactoryDatabaseImplement.Implements } shop.Update(model); context.SaveChanges(); - shop.UpdateSushi(context, model); + shop.UpdateCanneds(context, model); transaction.Commit(); return shop.GetViewModel; } @@ -129,7 +129,7 @@ namespace FishFactoryDatabaseImplement.Implements { shop.ListCanneds[model.Id] = (shop.ListCanneds[model.Id].Item1, countInCurrentShop - count); count = 0; - shop.UpdateSushi(context, new() + shop.UpdateCanneds(context, new() { Id = shop.Id, ListCanneds = shop.ListCanneds, diff --git a/FishFactory/FishFactoryDatabaseImplement/Models/Shop.cs b/FishFactory/FishFactoryDatabaseImplement/Models/Shop.cs index 573667d..509a72e 100644 --- a/FishFactory/FishFactoryDatabaseImplement/Models/Shop.cs +++ b/FishFactory/FishFactoryDatabaseImplement/Models/Shop.cs @@ -77,7 +77,7 @@ namespace FishFactoryDatabaseImplement.Models ListCanneds = ListCanneds }; - public void UpdateSushi(FishFactoryDatabase context, ShopBindingModel model) + public void UpdateCanneds(FishFactoryDatabase context, ShopBindingModel model) { var shopCanned = context.ShopCanned.Where(rec => rec.ShopId == model.Id).ToList(); if (shopCanned != null && shopCanned.Count > 0) -- 2.25.1