Закончил базу данных (наверное)

This commit is contained in:
Алексей Тихоненков 2024-05-04 01:16:00 +04:00
parent 33a1c735e5
commit 23c8471c77
11 changed files with 129 additions and 150 deletions

View File

@ -1,7 +1,7 @@
namespace DiningRoomDataModels.Models
{
/// <summary>
/// Комплектующая
/// Продукт
/// </summary>
public interface IComponentModel : IId
{

View File

@ -22,7 +22,7 @@
int CardId { get; }
/// <summary>
/// Список комплектующих
/// Список продуктов
/// </summary>
Dictionary<int, (IComponentModel, int)> DrinkComponents { get; }
}

View File

@ -27,6 +27,5 @@
/// <summary>
/// Привязка блюда к заказу
/// </summary>
int? OrderId { get; }
}
}

View File

@ -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();
}
}
}

View File

@ -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();
}

View File

@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace DiningRoomDatabaseImplement.Migrations
{
[DbContext(typeof(DiningRoomDatabase))]
[Migration("20240503165534_InitialCreate")]
[Migration("20240503210711_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
@ -40,16 +40,11 @@ namespace DiningRoomDatabaseImplement.Migrations
b.Property<DateTime>("DateCardCreate")
.HasColumnType("timestamp without time zone");
b.Property<int?>("DrinkId")
.HasColumnType("integer");
b.Property<int>("UserId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("DrinkId");
b.HasIndex("UserId");
b.ToTable("Cards");
@ -95,10 +90,6 @@ namespace DiningRoomDatabaseImplement.Migrations
b.Property<int>("CardId")
.HasColumnType("integer");
b.Property<string>("Category")
.IsRequired()
.HasColumnType("text");
b.Property<double>("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<double>("Cost")
.HasColumnType("double precision");
b.Property<int?>("OrderId")
.HasColumnType("integer");
b.Property<string>("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");
});

View File

@ -27,6 +27,27 @@ namespace DiningRoomDatabaseImplement.Migrations
table.PrimaryKey("PK_Users", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Cards",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
UserId = table.Column<int>(type: "integer", nullable: false),
CardName = table.Column<string>(type: "text", nullable: false),
DateCardCreate = table.Column<DateTime>(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<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
UserId = table.Column<int>(type: "integer", nullable: false),
DrinkName = table.Column<string>(type: "text", nullable: false),
Cost = table.Column<double>(type: "double precision", nullable: false),
CardId = table.Column<int>(type: "integer", nullable: false),
Category = table.Column<string>(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<int>(type: "integer", nullable: false),
ProductName = table.Column<string>(type: "text", nullable: false),
OrderId = table.Column<int>(type: "integer", nullable: true),
Cost = table.Column<double>(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<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
UserId = table.Column<int>(type: "integer", nullable: false),
CardName = table.Column<string>(type: "text", nullable: false),
DrinkId = table.Column<int>(type: "integer", nullable: true),
DateCardCreate = table.Column<DateTime>(type: "timestamp without time zone", nullable: false)
DrinkName = table.Column<string>(type: "text", nullable: false),
Cost = table.Column<double>(type: "double precision", nullable: false),
CardId = table.Column<int>(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<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
DrinkId = table.Column<int>(type: "integer", nullable: false),
ComponentId = table.Column<int>(type: "integer", nullable: false),
Count = table.Column<int>(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<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
DrinkId = table.Column<int>(type: "integer", nullable: false),
ComponentId = table.Column<int>(type: "integer", nullable: false),
Count = table.Column<int>(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
/// <inheritdoc />
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");
}

View File

@ -37,16 +37,11 @@ namespace DiningRoomDatabaseImplement.Migrations
b.Property<DateTime>("DateCardCreate")
.HasColumnType("timestamp without time zone");
b.Property<int?>("DrinkId")
.HasColumnType("integer");
b.Property<int>("UserId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("DrinkId");
b.HasIndex("UserId");
b.ToTable("Cards");
@ -92,10 +87,6 @@ namespace DiningRoomDatabaseImplement.Migrations
b.Property<int>("CardId")
.HasColumnType("integer");
b.Property<string>("Category")
.IsRequired()
.HasColumnType("text");
b.Property<double>("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<double>("Cost")
.HasColumnType("double precision");
b.Property<int?>("OrderId")
.HasColumnType("integer");
b.Property<string>("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");
});

View File

@ -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<Drink> 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,
};
}
}

View File

@ -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<Card> 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<DrinkComponent> Components { get; set; } = new();
private Dictionary<int, (IComponentModel, int)>? _drinkComponents;

View File

@ -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<int, (IComponentModel, int)>? _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