From 33a1c735e589c67c56bedf544d669a31991c3971 Mon Sep 17 00:00:00 2001 From: Yourdax Date: Fri, 3 May 2024 20:55:50 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B5=D1=89=D0=B5=20=D1=84=D0=B8=D0=BA=D1=81?= =?UTF-8?q?=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/CardLogic.cs | 112 +++++++++++++ ...ReportGuarantorLogic.cs => ReportLogic.cs} | 6 +- .../BindingModels/CardBindingModel.cs | 5 +- .../BusinessLogicContracts/ICardLogic.cs | 2 +- ...eportGuarantorLogic.cs => IReportLogic.cs} | 4 +- .../SearchModels/CardSearchModel.cs | 2 + .../StorageContracts/ICardStorage.cs | 3 +- .../ViewModels/CardViewModel.cs | 2 + .../ReportComponentOrderViewModel.cs | 2 +- .../Implements/ComponentStorage.cs | 2 +- ... 20240503165534_InitialCreate.Designer.cs} | 94 ++++++++++- ...ate.cs => 20240503165534_InitialCreate.cs} | 149 +++++++++++++----- .../DiningRoomDatabaseModelSnapshot.cs | 92 ++++++++++- .../Models/Product.cs | 4 +- 14 files changed, 411 insertions(+), 68 deletions(-) create mode 100644 DiningRoom/DiningRoomBusinessLogic/BusinessLogics/CardLogic.cs rename DiningRoom/DiningRoomBusinessLogic/BusinessLogics/{ReportGuarantorLogic.cs => ReportLogic.cs} (85%) rename DiningRoom/DiningRoomContracts/BusinessLogicContracts/{IReportGuarantorLogic.cs => IReportLogic.cs} (78%) rename DiningRoom/DiningRoomDatabaseImplement/Migrations/{20240430170642_InitialCreate.Designer.cs => 20240503165534_InitialCreate.Designer.cs} (77%) rename DiningRoom/DiningRoomDatabaseImplement/Migrations/{20240430170642_InitialCreate.cs => 20240503165534_InitialCreate.cs} (77%) diff --git a/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/CardLogic.cs b/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/CardLogic.cs new file mode 100644 index 0000000..2be5688 --- /dev/null +++ b/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/CardLogic.cs @@ -0,0 +1,112 @@ +using DiningRoomContracts.BindingModels; +using DiningRoomContracts.BusinessLogicContracts; +using DiningRoomContracts.SearchModels; +using DiningRoomContracts.StorageContracts; +using DiningRoomContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace DiningRoomBusinessLogic.BusinessLogics +{ + public class CardLogic : ICardLogic + { + private readonly ILogger _logger; + private readonly ICardStorage _CardStorage; + public CardLogic(ILogger logger, ICardStorage CardStorage) + { + _logger = logger; + _CardStorage = CardStorage; + } + + public List? ReadList(CardSearchModel? model) + { + //model.UserId = -1 для swagger, чтобы можно было считать все заявки всех пользователей + var list = (model == null || model.UserId == -1) ? _CardStorage.GetFullList() : _CardStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList Cards return null list"); + return null; + } + _logger.LogInformation("ReadList Cards.Count:{Count}", list.Count); + return list; + } + + public CardViewModel? ReadElement(CardSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + var element = _CardStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement Card not found"); + return null; + } + _logger.LogInformation("ReadElement card found Id:{Id}", element.Id); + return element; + } + + public bool Create(CardBindingModel model) + { + CheckModel(model); + if (_CardStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(CardBindingModel model) + { + CheckModel(model); + if (_CardStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool ConnectCardDrink(CardBindingModel model) + { + _logger.LogInformation("Connect Card {rId} with drink {aId}", model.Id, model.DrinkId); + return _CardStorage.ConnectCardDrink(model); + } + + public bool Delete(CardBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_CardStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(CardBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.DateCardCreate > DateTime.Now) + { + throw new ArgumentException($"Дата создания карты {model.DateCardCreate} не может быть в будущем"); + } + } + } +} diff --git a/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/ReportGuarantorLogic.cs b/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/ReportLogic.cs similarity index 85% rename from DiningRoom/DiningRoomBusinessLogic/BusinessLogics/ReportGuarantorLogic.cs rename to DiningRoom/DiningRoomBusinessLogic/BusinessLogics/ReportLogic.cs index 8431f3b..9c8d34f 100644 --- a/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/ReportGuarantorLogic.cs +++ b/DiningRoom/DiningRoomBusinessLogic/BusinessLogics/ReportLogic.cs @@ -6,11 +6,11 @@ using DiningRoomContracts.ViewModels; namespace DiningRoomBusinessLogic.BusinessLogics { - public class ReportGuarantorLogic : IReportGuarantorLogic + public class ReportLogic : IReportLogic { private readonly IComponentStorage _componentStorage; - public ReportGuarantorLogic(IComponentStorage ComponentStorage) + public ReportLogic(IComponentStorage ComponentStorage) { _componentStorage = ComponentStorage; } @@ -26,7 +26,7 @@ namespace DiningRoomBusinessLogic.BusinessLogics /// /// Получение отчёта для отправки на почту /// - public List GetReportComponentsByRequestDate(UserSearchModel CurrentUser, ReportBindingModel Report) + public List GetReportComponentsByCardDate(UserSearchModel CurrentUser, ReportBindingModel Report) { return _componentStorage.GetComponentsByDate(Report, CurrentUser); } diff --git a/DiningRoom/DiningRoomContracts/BindingModels/CardBindingModel.cs b/DiningRoom/DiningRoomContracts/BindingModels/CardBindingModel.cs index 07bf7ac..f19de81 100644 --- a/DiningRoom/DiningRoomContracts/BindingModels/CardBindingModel.cs +++ b/DiningRoom/DiningRoomContracts/BindingModels/CardBindingModel.cs @@ -13,7 +13,8 @@ namespace DiningRoomContracts.BindingModels public int Id { get; set; } public int UserId { get; set; } - + public int? DrinkId { get; set; } public string CardName { get; set; } = string.Empty; - } + public DateTime DateCardCreate { get; set; } = DateTime.Now; + } } diff --git a/DiningRoom/DiningRoomContracts/BusinessLogicContracts/ICardLogic.cs b/DiningRoom/DiningRoomContracts/BusinessLogicContracts/ICardLogic.cs index c44d410..a79f2fa 100644 --- a/DiningRoom/DiningRoomContracts/BusinessLogicContracts/ICardLogic.cs +++ b/DiningRoom/DiningRoomContracts/BusinessLogicContracts/ICardLogic.cs @@ -16,6 +16,6 @@ namespace DiningRoomContracts.BusinessLogicContracts bool Create(CardBindingModel model); bool Update(CardBindingModel model); bool Delete(CardBindingModel model); - bool ConnectCardDrink(int cardId, int drinkId); + bool ConnectCardDrink(CardBindingModel model); } } diff --git a/DiningRoom/DiningRoomContracts/BusinessLogicContracts/IReportGuarantorLogic.cs b/DiningRoom/DiningRoomContracts/BusinessLogicContracts/IReportLogic.cs similarity index 78% rename from DiningRoom/DiningRoomContracts/BusinessLogicContracts/IReportGuarantorLogic.cs rename to DiningRoom/DiningRoomContracts/BusinessLogicContracts/IReportLogic.cs index a831dae..a169cde 100644 --- a/DiningRoom/DiningRoomContracts/BusinessLogicContracts/IReportGuarantorLogic.cs +++ b/DiningRoom/DiningRoomContracts/BusinessLogicContracts/IReportLogic.cs @@ -4,7 +4,7 @@ using DiningRoomContracts.ViewModels; namespace DiningRoomContracts.BusinessLogicContracts { - public interface IReportGuarantorLogic + public interface IReportLogic { /// /// Получение отчёта для Word или Excel @@ -14,7 +14,7 @@ namespace DiningRoomContracts.BusinessLogicContracts /// /// Получение отчёта для отправки на почту /// - List GetReportComponentsByRequestDate(UserSearchModel CurrentUser, ReportBindingModel Report); + List GetReportComponentsByCardDate(UserSearchModel CurrentUser, ReportBindingModel Report); void SaveReportToWordFile(ReportBindingModel Model); diff --git a/DiningRoom/DiningRoomContracts/SearchModels/CardSearchModel.cs b/DiningRoom/DiningRoomContracts/SearchModels/CardSearchModel.cs index c1d6a4c..c71ad3b 100644 --- a/DiningRoom/DiningRoomContracts/SearchModels/CardSearchModel.cs +++ b/DiningRoom/DiningRoomContracts/SearchModels/CardSearchModel.cs @@ -5,6 +5,8 @@ public int? Id { get; set; } public int? UserId { get; set; } + public DateTime? DateCardCreate { get; set; } + public int? DrinkId { get; set; } public string? Cardname { get; set; } } diff --git a/DiningRoom/DiningRoomContracts/StorageContracts/ICardStorage.cs b/DiningRoom/DiningRoomContracts/StorageContracts/ICardStorage.cs index be6658d..ac75c37 100644 --- a/DiningRoom/DiningRoomContracts/StorageContracts/ICardStorage.cs +++ b/DiningRoom/DiningRoomContracts/StorageContracts/ICardStorage.cs @@ -17,5 +17,6 @@ namespace DiningRoomContracts.StorageContracts CardViewModel? Update(CardBindingModel Model); CardViewModel? Delete(CardBindingModel Model); - } + bool ConnectCardDrink(CardBindingModel model); + } } diff --git a/DiningRoom/DiningRoomContracts/ViewModels/CardViewModel.cs b/DiningRoom/DiningRoomContracts/ViewModels/CardViewModel.cs index 3803991..fc09b87 100644 --- a/DiningRoom/DiningRoomContracts/ViewModels/CardViewModel.cs +++ b/DiningRoom/DiningRoomContracts/ViewModels/CardViewModel.cs @@ -13,5 +13,7 @@ namespace DiningRoomContracts.ViewModels [DisplayName("Название карты")] public string CardName { get; set; } = string.Empty; public Dictionary DrinkCard { get; set; } = new(); + [DisplayName("Дата создания")] + public DateTime DateCardCreate { get; set; } = DateTime.Now; } } diff --git a/DiningRoom/DiningRoomContracts/ViewModels/ReportComponentOrderViewModel.cs b/DiningRoom/DiningRoomContracts/ViewModels/ReportComponentOrderViewModel.cs index 9e7b829..c66b906 100644 --- a/DiningRoom/DiningRoomContracts/ViewModels/ReportComponentOrderViewModel.cs +++ b/DiningRoom/DiningRoomContracts/ViewModels/ReportComponentOrderViewModel.cs @@ -8,6 +8,6 @@ public double ComponentCost { get; set; } - public List<(int Count, string ProductName, double ProductPrice, DateTime OrderDate)> Orders { get; set; } = new(); + public List<(int Count, string ProductName, double ProductPrice)> Orders { get; set; } = new(); } } diff --git a/DiningRoom/DiningRoomDatabaseImplement/Implements/ComponentStorage.cs b/DiningRoom/DiningRoomDatabaseImplement/Implements/ComponentStorage.cs index 94bf19a..9972521 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Implements/ComponentStorage.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Implements/ComponentStorage.cs @@ -108,7 +108,7 @@ namespace DiningRoomDatabaseImplement.Implements ComponentName = x.ComponentName, ComponentCost = x.Cost, Orders = x.ProductComponents - .Select(y => (y.Count, y.Product.ProductName, y.Product.Cost, y.Product.Order.DateCreate)) + .Select(y => (y.Count, y.Product.ProductName, y.Product.Cost)) .ToList(), }) .ToList(); diff --git a/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240430170642_InitialCreate.Designer.cs b/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503165534_InitialCreate.Designer.cs similarity index 77% rename from DiningRoom/DiningRoomDatabaseImplement/Migrations/20240430170642_InitialCreate.Designer.cs rename to DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503165534_InitialCreate.Designer.cs index c685cb5..9bec036 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240430170642_InitialCreate.Designer.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503165534_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace DiningRoomDatabaseImplement.Migrations { [DbContext(typeof(DiningRoomDatabase))] - [Migration("20240430170642_InitialCreate")] + [Migration("20240503165534_InitialCreate")] partial class InitialCreate { /// @@ -37,11 +37,21 @@ namespace DiningRoomDatabaseImplement.Migrations .IsRequired() .HasColumnType("text"); + b.Property("DateCardCreate") + .HasColumnType("timestamp without time zone"); + + b.Property("DrinkId") + .HasColumnType("integer"); + b.Property("UserId") .HasColumnType("integer"); b.HasKey("Id"); + b.HasIndex("DrinkId"); + + b.HasIndex("UserId"); + b.ToTable("Cards"); }); @@ -69,6 +79,8 @@ namespace DiningRoomDatabaseImplement.Migrations b.HasKey("Id"); + b.HasIndex("UserId"); + b.ToTable("Components"); }); @@ -99,6 +111,8 @@ namespace DiningRoomDatabaseImplement.Migrations b.HasKey("Id"); + b.HasIndex("UserId"); + b.ToTable("Drinks"); }); @@ -143,6 +157,10 @@ namespace DiningRoomDatabaseImplement.Migrations b.Property("ProductId") .HasColumnType("integer"); + b.Property("ProductName") + .IsRequired() + .HasColumnType("text"); + b.Property("Status") .HasColumnType("integer"); @@ -172,6 +190,9 @@ namespace DiningRoomDatabaseImplement.Migrations b.Property("Cost") .HasColumnType("double precision"); + b.Property("OrderId") + .HasColumnType("integer"); + b.Property("ProductName") .IsRequired() .HasColumnType("text"); @@ -181,6 +202,8 @@ namespace DiningRoomDatabaseImplement.Migrations b.HasKey("Id"); + b.HasIndex("UserId"); + b.ToTable("Products"); }); @@ -205,6 +228,8 @@ namespace DiningRoomDatabaseImplement.Migrations b.HasIndex("ComponentId"); + b.HasIndex("ProductId"); + b.ToTable("ProductComponents"); }); @@ -233,6 +258,39 @@ namespace DiningRoomDatabaseImplement.Migrations b.ToTable("Users"); }); + modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Card", b => + { + b.HasOne("DiningRoomDatabaseImplement.Models.Drink", "Drink") + .WithMany("Cards") + .HasForeignKey("DrinkId"); + + b.HasOne("DiningRoomDatabaseImplement.Models.User", null) + .WithMany("Cards") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Drink"); + }); + + modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Component", b => + { + b.HasOne("DiningRoomDatabaseImplement.Models.User", null) + .WithMany("Components") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Drink", b => + { + b.HasOne("DiningRoomDatabaseImplement.Models.User", null) + .WithMany("Drinks") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + modelBuilder.Entity("DiningRoomDatabaseImplement.Models.DrinkComponent", b => { b.HasOne("DiningRoomDatabaseImplement.Models.Component", "Component") @@ -255,20 +313,27 @@ namespace DiningRoomDatabaseImplement.Migrations modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Order", b => { b.HasOne("DiningRoomDatabaseImplement.Models.Product", "Product") - .WithMany() + .WithMany("Order") .HasForeignKey("ProductId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("DiningRoomDatabaseImplement.Models.User", "User") - .WithMany() + b.HasOne("DiningRoomDatabaseImplement.Models.User", null) + .WithMany("Orders") .HasForeignKey("UserId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); b.Navigation("Product"); + }); - b.Navigation("User"); + modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Product", b => + { + b.HasOne("DiningRoomDatabaseImplement.Models.User", null) + .WithMany("Products") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); }); modelBuilder.Entity("DiningRoomDatabaseImplement.Models.ProductComponent", b => @@ -281,7 +346,7 @@ namespace DiningRoomDatabaseImplement.Migrations b.HasOne("DiningRoomDatabaseImplement.Models.Product", "Product") .WithMany("Components") - .HasForeignKey("ComponentId") + .HasForeignKey("ProductId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -299,12 +364,29 @@ namespace DiningRoomDatabaseImplement.Migrations modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Drink", b => { + b.Navigation("Cards"); + b.Navigation("Components"); }); modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Product", b => { b.Navigation("Components"); + + b.Navigation("Order"); + }); + + modelBuilder.Entity("DiningRoomDatabaseImplement.Models.User", b => + { + b.Navigation("Cards"); + + b.Navigation("Components"); + + b.Navigation("Drinks"); + + b.Navigation("Orders"); + + b.Navigation("Products"); }); #pragma warning restore 612, 618 } diff --git a/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240430170642_InitialCreate.cs b/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503165534_InitialCreate.cs similarity index 77% rename from DiningRoom/DiningRoomDatabaseImplement/Migrations/20240430170642_InitialCreate.cs rename to DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503165534_InitialCreate.cs index cc6ffa2..57424f2 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240430170642_InitialCreate.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503165534_InitialCreate.cs @@ -13,17 +13,18 @@ namespace DiningRoomDatabaseImplement.Migrations protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( - name: "Cards", + name: "Users", columns: table => new { Id = table.Column(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - UserId = table.Column(type: "integer", nullable: false), - CardName = table.Column(type: "text", nullable: false) + Login = table.Column(type: "text", nullable: false), + Password = table.Column(type: "text", nullable: false), + Email = table.Column(type: "text", nullable: false) }, constraints: table => { - table.PrimaryKey("PK_Cards", x => x.Id); + table.PrimaryKey("PK_Users", x => x.Id); }); migrationBuilder.CreateTable( @@ -40,6 +41,12 @@ namespace DiningRoomDatabaseImplement.Migrations constraints: table => { table.PrimaryKey("PK_Components", x => x.Id); + table.ForeignKey( + name: "FK_Components_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( @@ -57,6 +64,12 @@ namespace DiningRoomDatabaseImplement.Migrations constraints: table => { table.PrimaryKey("PK_Drinks", x => x.Id); + table.ForeignKey( + name: "FK_Drinks_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( @@ -67,26 +80,45 @@ namespace DiningRoomDatabaseImplement.Migrations .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), UserId = table.Column(type: "integer", nullable: false), ProductName = table.Column(type: "text", nullable: false), + OrderId = table.Column(type: "integer", nullable: true), Cost = table.Column(type: "double precision", nullable: false) }, constraints: table => { table.PrimaryKey("PK_Products", x => x.Id); + table.ForeignKey( + name: "FK_Products_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( - name: "Users", + name: "Cards", columns: table => new { Id = table.Column(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - Login = table.Column(type: "text", nullable: false), - Password = table.Column(type: "text", nullable: false), - Email = table.Column(type: "text", nullable: false) + UserId = table.Column(type: "integer", nullable: false), + CardName = table.Column(type: "text", nullable: false), + DrinkId = table.Column(type: "integer", nullable: true), + DateCardCreate = table.Column(type: "timestamp without time zone", nullable: false) }, constraints: table => { - table.PrimaryKey("PK_Users", x => x.Id); + table.PrimaryKey("PK_Cards", x => x.Id); + table.ForeignKey( + name: "FK_Cards_Drinks_DrinkId", + column: x => x.DrinkId, + principalTable: "Drinks", + principalColumn: "Id"); + table.ForeignKey( + name: "FK_Cards_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( @@ -116,6 +148,37 @@ namespace DiningRoomDatabaseImplement.Migrations onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ProductId = table.Column(type: "integer", nullable: false), + ProductName = table.Column(type: "text", 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 without time zone", nullable: false), + UserId = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Products_ProductId", + column: x => x.ProductId, + principalTable: "Products", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_Orders_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateTable( name: "ProductComponents", columns: table => new @@ -136,48 +199,38 @@ namespace DiningRoomDatabaseImplement.Migrations principalColumn: "Id", onDelete: ReferentialAction.Cascade); table.ForeignKey( - name: "FK_ProductComponents_Products_ComponentId", - column: x => x.ComponentId, - principalTable: "Products", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "Orders", - columns: table => new - { - Id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - ProductId = table.Column(type: "integer", nullable: false), - UserId = table.Column(type: "integer", nullable: false), - Sum = table.Column(type: "double precision", nullable: false), - Count = table.Column(type: "integer", nullable: false), - DateCreate = table.Column(type: "timestamp without time zone", nullable: false), - Status = table.Column(type: "integer", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Orders", x => x.Id); - table.ForeignKey( - name: "FK_Orders_Products_ProductId", + name: "FK_ProductComponents_Products_ProductId", column: x => x.ProductId, principalTable: "Products", principalColumn: "Id", onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_Orders_Users_UserId", - column: x => x.UserId, - principalTable: "Users", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateIndex( + name: "IX_Cards_DrinkId", + table: "Cards", + column: "DrinkId"); + + migrationBuilder.CreateIndex( + name: "IX_Cards_UserId", + table: "Cards", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_Components_UserId", + table: "Components", + column: "UserId"); + migrationBuilder.CreateIndex( name: "IX_DrinkComponents_ComponentId", table: "DrinkComponents", column: "ComponentId"); + migrationBuilder.CreateIndex( + name: "IX_Drinks_UserId", + table: "Drinks", + column: "UserId"); + migrationBuilder.CreateIndex( name: "IX_Orders_ProductId", table: "Orders", @@ -192,6 +245,16 @@ namespace DiningRoomDatabaseImplement.Migrations name: "IX_ProductComponents_ComponentId", table: "ProductComponents", column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_ProductComponents_ProductId", + table: "ProductComponents", + column: "ProductId"); + + migrationBuilder.CreateIndex( + name: "IX_Products_UserId", + table: "Products", + column: "UserId"); } /// @@ -212,14 +275,14 @@ namespace DiningRoomDatabaseImplement.Migrations migrationBuilder.DropTable( name: "Drinks"); - migrationBuilder.DropTable( - name: "Users"); - migrationBuilder.DropTable( name: "Components"); migrationBuilder.DropTable( name: "Products"); + + migrationBuilder.DropTable( + name: "Users"); } } } diff --git a/DiningRoom/DiningRoomDatabaseImplement/Migrations/DiningRoomDatabaseModelSnapshot.cs b/DiningRoom/DiningRoomDatabaseImplement/Migrations/DiningRoomDatabaseModelSnapshot.cs index ea7c14b..cea701b 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Migrations/DiningRoomDatabaseModelSnapshot.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Migrations/DiningRoomDatabaseModelSnapshot.cs @@ -34,11 +34,21 @@ namespace DiningRoomDatabaseImplement.Migrations .IsRequired() .HasColumnType("text"); + b.Property("DateCardCreate") + .HasColumnType("timestamp without time zone"); + + b.Property("DrinkId") + .HasColumnType("integer"); + b.Property("UserId") .HasColumnType("integer"); b.HasKey("Id"); + b.HasIndex("DrinkId"); + + b.HasIndex("UserId"); + b.ToTable("Cards"); }); @@ -66,6 +76,8 @@ namespace DiningRoomDatabaseImplement.Migrations b.HasKey("Id"); + b.HasIndex("UserId"); + b.ToTable("Components"); }); @@ -96,6 +108,8 @@ namespace DiningRoomDatabaseImplement.Migrations b.HasKey("Id"); + b.HasIndex("UserId"); + b.ToTable("Drinks"); }); @@ -140,6 +154,10 @@ namespace DiningRoomDatabaseImplement.Migrations b.Property("ProductId") .HasColumnType("integer"); + b.Property("ProductName") + .IsRequired() + .HasColumnType("text"); + b.Property("Status") .HasColumnType("integer"); @@ -169,6 +187,9 @@ namespace DiningRoomDatabaseImplement.Migrations b.Property("Cost") .HasColumnType("double precision"); + b.Property("OrderId") + .HasColumnType("integer"); + b.Property("ProductName") .IsRequired() .HasColumnType("text"); @@ -178,6 +199,8 @@ namespace DiningRoomDatabaseImplement.Migrations b.HasKey("Id"); + b.HasIndex("UserId"); + b.ToTable("Products"); }); @@ -202,6 +225,8 @@ namespace DiningRoomDatabaseImplement.Migrations b.HasIndex("ComponentId"); + b.HasIndex("ProductId"); + b.ToTable("ProductComponents"); }); @@ -230,6 +255,39 @@ namespace DiningRoomDatabaseImplement.Migrations b.ToTable("Users"); }); + modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Card", b => + { + b.HasOne("DiningRoomDatabaseImplement.Models.Drink", "Drink") + .WithMany("Cards") + .HasForeignKey("DrinkId"); + + b.HasOne("DiningRoomDatabaseImplement.Models.User", null) + .WithMany("Cards") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Drink"); + }); + + modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Component", b => + { + b.HasOne("DiningRoomDatabaseImplement.Models.User", null) + .WithMany("Components") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Drink", b => + { + b.HasOne("DiningRoomDatabaseImplement.Models.User", null) + .WithMany("Drinks") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + modelBuilder.Entity("DiningRoomDatabaseImplement.Models.DrinkComponent", b => { b.HasOne("DiningRoomDatabaseImplement.Models.Component", "Component") @@ -252,20 +310,27 @@ namespace DiningRoomDatabaseImplement.Migrations modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Order", b => { b.HasOne("DiningRoomDatabaseImplement.Models.Product", "Product") - .WithMany() + .WithMany("Order") .HasForeignKey("ProductId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("DiningRoomDatabaseImplement.Models.User", "User") - .WithMany() + b.HasOne("DiningRoomDatabaseImplement.Models.User", null) + .WithMany("Orders") .HasForeignKey("UserId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); b.Navigation("Product"); + }); - b.Navigation("User"); + modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Product", b => + { + b.HasOne("DiningRoomDatabaseImplement.Models.User", null) + .WithMany("Products") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); }); modelBuilder.Entity("DiningRoomDatabaseImplement.Models.ProductComponent", b => @@ -278,7 +343,7 @@ namespace DiningRoomDatabaseImplement.Migrations b.HasOne("DiningRoomDatabaseImplement.Models.Product", "Product") .WithMany("Components") - .HasForeignKey("ComponentId") + .HasForeignKey("ProductId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -296,12 +361,29 @@ namespace DiningRoomDatabaseImplement.Migrations modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Drink", b => { + b.Navigation("Cards"); + b.Navigation("Components"); }); modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Product", b => { b.Navigation("Components"); + + b.Navigation("Order"); + }); + + modelBuilder.Entity("DiningRoomDatabaseImplement.Models.User", b => + { + b.Navigation("Cards"); + + b.Navigation("Components"); + + b.Navigation("Drinks"); + + b.Navigation("Orders"); + + b.Navigation("Products"); }); #pragma warning restore 612, 618 } diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs index 0df21d8..61b08fb 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs @@ -17,8 +17,6 @@ namespace DiningRoomDatabaseImplement.Models [Required] public string ProductName { get; set; } = string.Empty; public int? OrderId { get; set; } - - public virtual Order? Order { get; set; } [Required] public double Cost { get; set; } private Dictionary? _productComponents; @@ -42,7 +40,7 @@ namespace DiningRoomDatabaseImplement.Models [ForeignKey("ProductId")] public virtual List Components { get; set; } = new(); [ForeignKey("ProductId")] - public virtual List Orders { get; set; } = new(); + public virtual List Order { get; set; } = new(); public static Product Create(DiningRoomDatabase Context, ProductBindingModel Model) { return new()