Что-то работает, но не все

This commit is contained in:
dasha 2023-03-07 19:27:57 +04:00
parent 522bff8245
commit 9399a68fc9
5 changed files with 510 additions and 11 deletions

View File

@ -3,17 +3,20 @@ using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDatabaseImplement.Models;
using SushiBarDataModels.Models;
using System.Collections.Generic;
using System.Xml.Linq;
namespace SushiBarDatabaseImplement.Implements
{
internal class ShopStorage : IShopStorage
public class ShopStorage : IShopStorage
{
public List<ShopViewModel> GetFullList()
{
using var context = new SushiBarDatabase();
return context.Shops
.Include(x => x.ListSushi)
.Include(x => x.ListSushiFk)
.ThenInclude(x => x.Sushi)
.ToList()
.Select(x => x.GetViewModel)
@ -27,7 +30,7 @@ namespace SushiBarDatabaseImplement.Implements
}
using var context = new SushiBarDatabase();
return context.Shops
.Include(x => x.ListSushi)
.Include(x => x.ListSushiFk)
.ThenInclude(x => x.Sushi)
.Where(x => x.ShopName.Contains(model.ShopName))
.ToList()
@ -42,7 +45,7 @@ namespace SushiBarDatabaseImplement.Implements
}
using var context = new SushiBarDatabase();
return context.Shops
.Include(x => x.ListSushi)
.Include(x => x.ListSushiFk)
.ThenInclude(x => x.Sushi)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) ||
(model.Id.HasValue && x.Id == model.Id))
@ -51,7 +54,7 @@ namespace SushiBarDatabaseImplement.Implements
public ShopViewModel? Insert(ShopBindingModel model)
{
using var context = new SushiBarDatabase();
var newShop = Sushi.Create(context, model);
var newShop = Shop.Create(context, model);
if (newShop == null)
{
return null;
@ -107,19 +110,34 @@ namespace SushiBarDatabaseImplement.Implements
using var transaction = context.Database.BeginTransaction();
try
{
foreach (var shop in context.Shops.Where(x => x.ListSushi.ContainsKey(model.Id)))
foreach (var shop in context.Shops
.Include(x => x.ListSushiFk)
.ThenInclude(x => x.Sushi)
.ToList()
.Where(x => x.ListSushi.ContainsKey(model.Id)))
{
int countInCurrentShop = shop.ListSushi[model.Id].Item2;
if (countInCurrentShop <= count)
{
shop.ListSushi[model.Id] = (shop.ListSushi[model.Id].Item1, 0);
var elem = context.ShopSushi
.Where(x => x.SushiId == model.Id)
.FirstOrDefault(x => x.ShopId == shop.Id);
context.ShopSushi.Remove(elem);
shop.ListSushi.Remove(model.Id);
count -= countInCurrentShop;
}
else
{
shop.ListSushi[model.Id] = (shop.ListSushi[model.Id].Item1, countInCurrentShop - count);
count = 0;
shop.UpdateSushi(context, new()
{
Id = shop.Id,
ListSushi = shop.ListSushi,
});
}
if (count == 0)
{
context.SaveChanges();
transaction.Commit();
return true;

View File

@ -0,0 +1,248 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using SushiBarDatabaseImplement;
#nullable disable
namespace SushiBarDatabaseImplement.Migrations
{
[DbContext(typeof(SushiBarDatabase))]
[Migration("20230307133039_ShopMigration")]
partial class ShopMigration
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Ingredient", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<double>("Cost")
.HasColumnType("float");
b.Property<string>("IngredientName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Ingredients");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.Property<int>("SushiId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("SushiId");
b.ToTable("Orders");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("DateOpening")
.HasColumnType("datetime2");
b.Property<int>("MaxCountSushi")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushi", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.Property<int>("SushiId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ShopId");
b.HasIndex("SushiId");
b.ToTable("ShopSushi");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<double>("Price")
.HasColumnType("float");
b.Property<string>("SushiName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("ListSushi");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiIngredient", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("IngredientId")
.HasColumnType("int");
b.Property<int>("SushiId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("IngredientId");
b.HasIndex("SushiId");
b.ToTable("SushiIngredients");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Order", b =>
{
b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi")
.WithMany("Orders")
.HasForeignKey("SushiId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Sushi");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushi", b =>
{
b.HasOne("SushiBarDatabaseImplement.Models.Shop", "Shop")
.WithMany("ListSushiFk")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi")
.WithMany()
.HasForeignKey("SushiId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Shop");
b.Navigation("Sushi");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiIngredient", b =>
{
b.HasOne("SushiBarDatabaseImplement.Models.Ingredient", "Ingredient")
.WithMany("SushiIngredients")
.HasForeignKey("IngredientId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi")
.WithMany("Ingredients")
.HasForeignKey("SushiId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Ingredient");
b.Navigation("Sushi");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Ingredient", b =>
{
b.Navigation("SushiIngredients");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b =>
{
b.Navigation("ListSushiFk");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b =>
{
b.Navigation("Ingredients");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,152 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace SushiBarDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class ShopMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Orders_SushiList_SushiId",
table: "Orders");
migrationBuilder.DropForeignKey(
name: "FK_SushiIngredients_SushiList_SushiId",
table: "SushiIngredients");
migrationBuilder.DropPrimaryKey(
name: "PK_SushiList",
table: "SushiList");
migrationBuilder.RenameTable(
name: "SushiList",
newName: "ListSushi");
migrationBuilder.AddPrimaryKey(
name: "PK_ListSushi",
table: "ListSushi",
column: "Id");
migrationBuilder.CreateTable(
name: "Shops",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ShopName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Address = table.Column<string>(type: "nvarchar(max)", nullable: false),
DateOpening = table.Column<DateTime>(type: "datetime2", nullable: false),
MaxCountSushi = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable(
name: "ShopSushi",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
SushiId = table.Column<int>(type: "int", nullable: false),
ShopId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ShopSushi", x => x.Id);
table.ForeignKey(
name: "FK_ShopSushi_ListSushi_SushiId",
column: x => x.SushiId,
principalTable: "ListSushi",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopSushi_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_ShopSushi_ShopId",
table: "ShopSushi",
column: "ShopId");
migrationBuilder.CreateIndex(
name: "IX_ShopSushi_SushiId",
table: "ShopSushi",
column: "SushiId");
migrationBuilder.AddForeignKey(
name: "FK_Orders_ListSushi_SushiId",
table: "Orders",
column: "SushiId",
principalTable: "ListSushi",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "FK_SushiIngredients_ListSushi_SushiId",
table: "SushiIngredients",
column: "SushiId",
principalTable: "ListSushi",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Orders_ListSushi_SushiId",
table: "Orders");
migrationBuilder.DropForeignKey(
name: "FK_SushiIngredients_ListSushi_SushiId",
table: "SushiIngredients");
migrationBuilder.DropTable(
name: "ShopSushi");
migrationBuilder.DropTable(
name: "Shops");
migrationBuilder.DropPrimaryKey(
name: "PK_ListSushi",
table: "ListSushi");
migrationBuilder.RenameTable(
name: "ListSushi",
newName: "SushiList");
migrationBuilder.AddPrimaryKey(
name: "PK_SushiList",
table: "SushiList",
column: "Id");
migrationBuilder.AddForeignKey(
name: "FK_Orders_SushiList_SushiId",
table: "Orders",
column: "SushiId",
principalTable: "SushiList",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "FK_SushiIngredients_SushiList_SushiId",
table: "SushiIngredients",
column: "SushiId",
principalTable: "SushiList",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
}
}
}

View File

@ -1,6 +1,10 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using SushiBarDatabaseImplement;
#nullable disable
@ -71,6 +75,59 @@ namespace SushiBarDatabaseImplement.Migrations
b.ToTable("Orders");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("DateOpening")
.HasColumnType("datetime2");
b.Property<int>("MaxCountSushi")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushi", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.Property<int>("SushiId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ShopId");
b.HasIndex("SushiId");
b.ToTable("ShopSushi");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b =>
{
b.Property<int>("Id")
@ -88,7 +145,7 @@ namespace SushiBarDatabaseImplement.Migrations
b.HasKey("Id");
b.ToTable("SushiList");
b.ToTable("ListSushi");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiIngredient", b =>
@ -128,6 +185,25 @@ namespace SushiBarDatabaseImplement.Migrations
b.Navigation("Sushi");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushi", b =>
{
b.HasOne("SushiBarDatabaseImplement.Models.Shop", "Shop")
.WithMany("ListSushiFk")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi")
.WithMany()
.HasForeignKey("SushiId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Shop");
b.Navigation("Sushi");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiIngredient", b =>
{
b.HasOne("SushiBarDatabaseImplement.Models.Ingredient", "Ingredient")
@ -152,6 +228,11 @@ namespace SushiBarDatabaseImplement.Migrations
b.Navigation("SushiIngredients");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b =>
{
b.Navigation("ListSushiFk");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b =>
{
b.Navigation("Ingredients");

View File

@ -75,7 +75,7 @@ namespace SushiBarDatabaseImplement.Models
public void UpdateSushi(SushiBarDatabase context, ShopBindingModel model)
{
var shopSushi = context.ShopSushi.Where(rec => rec.SushiId == model.Id).ToList();
var shopSushi = context.ShopSushi.Where(rec => rec.ShopId == model.Id).ToList();
if (shopSushi != null && shopSushi.Count > 0)
{ // удалили те, которых нет в модели
context.ShopSushi.RemoveRange(shopSushi.Where(rec => !model.ListSushi.ContainsKey(rec.SushiId)));