diff --git a/DiningRoom/DiningRoomDataModels/Models/IComponentModel.cs b/DiningRoom/DiningRoomDataModels/Models/IComponentModel.cs index 3bd7608..2ea858c 100644 --- a/DiningRoom/DiningRoomDataModels/Models/IComponentModel.cs +++ b/DiningRoom/DiningRoomDataModels/Models/IComponentModel.cs @@ -1,7 +1,7 @@ namespace DiningRoomDataModels.Models { /// - /// Комплектующая + /// Продукт /// public interface IComponentModel : IId { diff --git a/DiningRoom/DiningRoomDataModels/Models/IDrinkModel.cs b/DiningRoom/DiningRoomDataModels/Models/IDrinkModel.cs index 0c01525..e54ceb8 100644 --- a/DiningRoom/DiningRoomDataModels/Models/IDrinkModel.cs +++ b/DiningRoom/DiningRoomDataModels/Models/IDrinkModel.cs @@ -22,7 +22,7 @@ int CardId { get; } /// - /// Список комплектующих + /// Список продуктов /// Dictionary DrinkComponents { get; } } diff --git a/DiningRoom/DiningRoomDataModels/Models/IProductModel.cs b/DiningRoom/DiningRoomDataModels/Models/IProductModel.cs index 65cd61b..7d30e25 100644 --- a/DiningRoom/DiningRoomDataModels/Models/IProductModel.cs +++ b/DiningRoom/DiningRoomDataModels/Models/IProductModel.cs @@ -27,6 +27,5 @@ /// /// Привязка блюда к заказу /// - int? OrderId { get; } } } diff --git a/DiningRoom/DiningRoomDatabaseImplement/Implements/ComponentStorage.cs b/DiningRoom/DiningRoomDatabaseImplement/Implements/ComponentStorage.cs index 9972521..68db08e 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Implements/ComponentStorage.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Implements/ComponentStorage.cs @@ -118,32 +118,27 @@ namespace DiningRoomDatabaseImplement.Implements { using var Context = new DiningRoomDatabase(); - return Context.Components - .Where(c => c.UserId == UserModel.Id) - .Include(x => x.ProductComponents) - .ThenInclude(pc => pc.Product) - .Include(c => c.DrinkComponents) - .ThenInclude(ac => ac.Drink) - .ThenInclude(a => a.Cards.Where(r => r.DateCardCreate >= ReportModel.DateFrom && r.DateCardCreate <= ReportModel.DateTo)) + return Context.Cards + .Where(card => card.UserId == UserModel.Id && card.DateCardCreate >= ReportModel.DateFrom && card.DateCardCreate <= ReportModel.DateTo) + .Include(card => card.Drinks) + .ThenInclude(d => d.Components) + .ThenInclude(dc => dc.Component) + .ThenInclude(c => c.ProductComponents) + .ThenInclude(pc => pc.Product) .ToList() - .SelectMany(c => c.DrinkComponents - .SelectMany(ac => ac.Drink.Cards, (ac, r) => new { ac, r }) - .SelectMany(temp => c.ProductComponents, (temp, pc) => new { temp.ac, temp.r, pc }) - .Select(temp => new ReportComponentByDateViewModel - { - ComponentId = c.Id, - ComponentName = c.ComponentName, - ComponentCost = c.Cost, - ProductId = temp.pc.Product.Id, - ProductName = temp.pc.Product.ProductName, - ProductPrice = temp.pc.Product.Cost, - CardId = temp.r.Id, - CardName = temp.r.CardName, - })) - .ToList(); + .SelectMany(card => card.Drinks + .SelectMany(d => d.Components.SelectMany(dc => dc.Component.ProductComponents.Select(pc => new ReportComponentByDateViewModel + { + ComponentId = pc.ComponentId, + ComponentName = dc.Component.ComponentName, + ComponentCost = dc.Component.Cost, + ProductId = pc.ProductId, + ProductName = pc.Product.ProductName, + ProductPrice = pc.Product.Cost, + CardId = card.Id, + CardName = card.CardName, + })))) + .ToList(); } - - - } } \ No newline at end of file diff --git a/DiningRoom/DiningRoomDatabaseImplement/Implements/ProductStorage.cs b/DiningRoom/DiningRoomDatabaseImplement/Implements/ProductStorage.cs index 928cb6d..1f17ed1 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Implements/ProductStorage.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Implements/ProductStorage.cs @@ -32,7 +32,7 @@ namespace DiningRoomDatabaseImplement.Implements .Include(x => x.Order) .Include(x => x.Components) .ThenInclude(x => x.Component) - .Where(x => x.UserId == model.UserId && x.OrderId == model.OrderId) + .Where(x => x.UserId == model.UserId) .Select(x => x.GetViewModel) .ToList(); } diff --git a/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503165534_InitialCreate.Designer.cs b/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503210711_InitialCreate.Designer.cs similarity index 95% rename from DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503165534_InitialCreate.Designer.cs rename to DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503210711_InitialCreate.Designer.cs index 9bec036..f2dae02 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503165534_InitialCreate.Designer.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503210711_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace DiningRoomDatabaseImplement.Migrations { [DbContext(typeof(DiningRoomDatabase))] - [Migration("20240503165534_InitialCreate")] + [Migration("20240503210711_InitialCreate")] partial class InitialCreate { /// @@ -40,16 +40,11 @@ namespace DiningRoomDatabaseImplement.Migrations 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"); @@ -95,10 +90,6 @@ namespace DiningRoomDatabaseImplement.Migrations b.Property("CardId") .HasColumnType("integer"); - b.Property("Category") - .IsRequired() - .HasColumnType("text"); - b.Property("Cost") .HasColumnType("double precision"); @@ -111,6 +102,8 @@ namespace DiningRoomDatabaseImplement.Migrations b.HasKey("Id"); + b.HasIndex("CardId"); + b.HasIndex("UserId"); b.ToTable("Drinks"); @@ -190,9 +183,6 @@ namespace DiningRoomDatabaseImplement.Migrations b.Property("Cost") .HasColumnType("double precision"); - b.Property("OrderId") - .HasColumnType("integer"); - b.Property("ProductName") .IsRequired() .HasColumnType("text"); @@ -260,17 +250,11 @@ namespace DiningRoomDatabaseImplement.Migrations 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 => @@ -284,11 +268,19 @@ namespace DiningRoomDatabaseImplement.Migrations modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Drink", b => { + b.HasOne("DiningRoomDatabaseImplement.Models.Card", "Card") + .WithMany("Drinks") + .HasForeignKey("CardId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("DiningRoomDatabaseImplement.Models.User", null) .WithMany("Drinks") .HasForeignKey("UserId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.Navigation("Card"); }); modelBuilder.Entity("DiningRoomDatabaseImplement.Models.DrinkComponent", b => @@ -355,6 +347,11 @@ namespace DiningRoomDatabaseImplement.Migrations b.Navigation("Product"); }); + modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Card", b => + { + b.Navigation("Drinks"); + }); + modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Component", b => { b.Navigation("DrinkComponents"); @@ -364,8 +361,6 @@ namespace DiningRoomDatabaseImplement.Migrations modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Drink", b => { - b.Navigation("Cards"); - b.Navigation("Components"); }); diff --git a/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503165534_InitialCreate.cs b/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503210711_InitialCreate.cs similarity index 95% rename from DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503165534_InitialCreate.cs rename to DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503210711_InitialCreate.cs index 57424f2..2dc1303 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503165534_InitialCreate.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Migrations/20240503210711_InitialCreate.cs @@ -27,6 +27,27 @@ namespace DiningRoomDatabaseImplement.Migrations table.PrimaryKey("PK_Users", x => x.Id); }); + migrationBuilder.CreateTable( + name: "Cards", + 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), + DateCardCreate = table.Column(type: "timestamp without time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Cards", x => x.Id); + table.ForeignKey( + name: "FK_Cards_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateTable( name: "Components", columns: table => new @@ -49,29 +70,6 @@ namespace DiningRoomDatabaseImplement.Migrations onDelete: ReferentialAction.Cascade); }); - migrationBuilder.CreateTable( - name: "Drinks", - columns: table => new - { - Id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - UserId = table.Column(type: "integer", nullable: false), - DrinkName = table.Column(type: "text", nullable: false), - Cost = table.Column(type: "double precision", nullable: false), - CardId = table.Column(type: "integer", nullable: false), - Category = table.Column(type: "text", nullable: false) - }, - 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( name: "Products", columns: table => new @@ -80,7 +78,6 @@ 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 => @@ -95,59 +92,33 @@ namespace DiningRoomDatabaseImplement.Migrations }); migrationBuilder.CreateTable( - name: "Cards", + name: "Drinks", 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), - DrinkId = table.Column(type: "integer", nullable: true), - DateCardCreate = table.Column(type: "timestamp without time zone", nullable: false) + DrinkName = table.Column(type: "text", nullable: false), + Cost = table.Column(type: "double precision", nullable: false), + CardId = table.Column(type: "integer", nullable: false) }, constraints: table => { - table.PrimaryKey("PK_Cards", x => x.Id); + table.PrimaryKey("PK_Drinks", x => x.Id); table.ForeignKey( - name: "FK_Cards_Drinks_DrinkId", - column: x => x.DrinkId, - principalTable: "Drinks", - principalColumn: "Id"); + name: "FK_Drinks_Cards_CardId", + column: x => x.CardId, + principalTable: "Cards", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); table.ForeignKey( - name: "FK_Cards_Users_UserId", + name: "FK_Drinks_Users_UserId", column: x => x.UserId, principalTable: "Users", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); - migrationBuilder.CreateTable( - name: "DrinkComponents", - columns: table => new - { - Id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - DrinkId = 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_DrinkComponents", x => x.Id); - table.ForeignKey( - name: "FK_DrinkComponents_Components_ComponentId", - column: x => x.ComponentId, - principalTable: "Components", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_DrinkComponents_Drinks_ComponentId", - column: x => x.ComponentId, - principalTable: "Drinks", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - migrationBuilder.CreateTable( name: "Orders", columns: table => new @@ -206,10 +177,32 @@ namespace DiningRoomDatabaseImplement.Migrations onDelete: ReferentialAction.Cascade); }); - migrationBuilder.CreateIndex( - name: "IX_Cards_DrinkId", - table: "Cards", - column: "DrinkId"); + migrationBuilder.CreateTable( + name: "DrinkComponents", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + DrinkId = 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_DrinkComponents", x => x.Id); + table.ForeignKey( + name: "FK_DrinkComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_DrinkComponents_Drinks_ComponentId", + column: x => x.ComponentId, + principalTable: "Drinks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); migrationBuilder.CreateIndex( name: "IX_Cards_UserId", @@ -226,6 +219,11 @@ namespace DiningRoomDatabaseImplement.Migrations table: "DrinkComponents", column: "ComponentId"); + migrationBuilder.CreateIndex( + name: "IX_Drinks_CardId", + table: "Drinks", + column: "CardId"); + migrationBuilder.CreateIndex( name: "IX_Drinks_UserId", table: "Drinks", @@ -260,9 +258,6 @@ namespace DiningRoomDatabaseImplement.Migrations /// protected override void Down(MigrationBuilder migrationBuilder) { - migrationBuilder.DropTable( - name: "Cards"); - migrationBuilder.DropTable( name: "DrinkComponents"); @@ -281,6 +276,9 @@ namespace DiningRoomDatabaseImplement.Migrations migrationBuilder.DropTable( name: "Products"); + migrationBuilder.DropTable( + name: "Cards"); + migrationBuilder.DropTable( name: "Users"); } diff --git a/DiningRoom/DiningRoomDatabaseImplement/Migrations/DiningRoomDatabaseModelSnapshot.cs b/DiningRoom/DiningRoomDatabaseImplement/Migrations/DiningRoomDatabaseModelSnapshot.cs index cea701b..bbee309 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Migrations/DiningRoomDatabaseModelSnapshot.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Migrations/DiningRoomDatabaseModelSnapshot.cs @@ -37,16 +37,11 @@ namespace DiningRoomDatabaseImplement.Migrations 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"); @@ -92,10 +87,6 @@ namespace DiningRoomDatabaseImplement.Migrations b.Property("CardId") .HasColumnType("integer"); - b.Property("Category") - .IsRequired() - .HasColumnType("text"); - b.Property("Cost") .HasColumnType("double precision"); @@ -108,6 +99,8 @@ namespace DiningRoomDatabaseImplement.Migrations b.HasKey("Id"); + b.HasIndex("CardId"); + b.HasIndex("UserId"); b.ToTable("Drinks"); @@ -187,9 +180,6 @@ namespace DiningRoomDatabaseImplement.Migrations b.Property("Cost") .HasColumnType("double precision"); - b.Property("OrderId") - .HasColumnType("integer"); - b.Property("ProductName") .IsRequired() .HasColumnType("text"); @@ -257,17 +247,11 @@ namespace DiningRoomDatabaseImplement.Migrations 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 => @@ -281,11 +265,19 @@ namespace DiningRoomDatabaseImplement.Migrations modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Drink", b => { + b.HasOne("DiningRoomDatabaseImplement.Models.Card", "Card") + .WithMany("Drinks") + .HasForeignKey("CardId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("DiningRoomDatabaseImplement.Models.User", null) .WithMany("Drinks") .HasForeignKey("UserId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.Navigation("Card"); }); modelBuilder.Entity("DiningRoomDatabaseImplement.Models.DrinkComponent", b => @@ -352,6 +344,11 @@ namespace DiningRoomDatabaseImplement.Migrations b.Navigation("Product"); }); + modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Card", b => + { + b.Navigation("Drinks"); + }); + modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Component", b => { b.Navigation("DrinkComponents"); @@ -361,8 +358,6 @@ namespace DiningRoomDatabaseImplement.Migrations modelBuilder.Entity("DiningRoomDatabaseImplement.Models.Drink", b => { - b.Navigation("Cards"); - b.Navigation("Components"); }); diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs index 5dfdb1a..adc6b01 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs @@ -17,12 +17,12 @@ namespace DiningRoomDatabaseImplement.Models { public int Id { get; set; } public int UserId { get; set; } + [Required] public string CardName { get; set; } = string.Empty; - public int? DrinkId { get; set; } - - public virtual Drink? Drink { get; set; } = new(); [Required] public DateTime DateCardCreate { get; set; } + [ForeignKey("CardId")] + public virtual List Drinks { get; set; } = new(); public static Card Create(DiningRoomDatabase context, CardBindingModel model) { return new Card() @@ -30,6 +30,7 @@ namespace DiningRoomDatabaseImplement.Models Id = model.Id, CardName = model.CardName, UserId = model.UserId, + DateCardCreate = model.DateCardCreate, }; } public void Update(CardBindingModel model) @@ -41,6 +42,7 @@ namespace DiningRoomDatabaseImplement.Models Id = Id, CardName = CardName, UserId = UserId, + DateCardCreate = DateCardCreate, }; } } diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs index 98f79af..3184de7 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs @@ -4,6 +4,7 @@ using DiningRoomDataModels.Models; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Diagnostics; +using System.Reflection; namespace DiningRoomDatabaseImplement.Models { @@ -21,13 +22,10 @@ namespace DiningRoomDatabaseImplement.Models public double Cost { get; private set; } public int CardId { get; private set; } - [ForeignKey("DrinkId")] - public virtual List Cards { get; set; } = new(); + public virtual Card? Card { get; set; } = new(); - [Required] - public string Category { get; private set; } = string.Empty; - [ForeignKey("ComponentId")] + [ForeignKey("ComponentId")] public virtual List Components { get; set; } = new(); private Dictionary? _drinkComponents; diff --git a/DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs b/DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs index 61b08fb..a69a8b9 100644 --- a/DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs +++ b/DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs @@ -16,7 +16,7 @@ namespace DiningRoomDatabaseImplement.Models [Required] public string ProductName { get; set; } = string.Empty; - public int? OrderId { get; set; } + [Required] public double Cost { get; set; } private Dictionary? _productComponents; @@ -47,7 +47,6 @@ namespace DiningRoomDatabaseImplement.Models { Id = Model.Id, UserId = Model.UserId, - OrderId = Model.OrderId, ProductName = Model.ProductName, Cost = Model.Cost, Components = Model.ProductComponents.Select(x => new ProductComponent @@ -59,7 +58,6 @@ namespace DiningRoomDatabaseImplement.Models } public void Update(ProductBindingModel model) { - OrderId = model.OrderId; ProductName = model.ProductName; Cost = model.Cost; } @@ -67,7 +65,6 @@ namespace DiningRoomDatabaseImplement.Models { Id = Id, UserId = UserId, - OrderId = OrderId, ProductName = ProductName, Cost = Cost, ProductComponents = ProductComponents