This commit is contained in:
shadowik 2023-04-06 04:11:31 +04:00
parent 787432c6ec
commit 5259f0c89f
7 changed files with 634 additions and 26 deletions

View File

@ -117,7 +117,17 @@ namespace PizzeriaBusinessLogic.BusinessLogics
throw new ArgumentException($"Поставка: Товар с id:{model.PizzaId} не найденн");
}
shop.ShopPizzas.Add(model.PizzaId, (pizza, model.Count));
}
_shopStorage.Update(new ShopBindingModel()
{
Id = shop.Id,
ShopName = shop.ShopName,
Adress = shop.Adress,
OpeningDate = shop.OpeningDate,
ShopPizzas = shop.ShopPizzas,
PizzaMaxCount = shop.PizzaMaxCount,
});
return true;
}
private void CheckModel(ShopBindingModel model, bool withParams = true)

View File

@ -0,0 +1,191 @@
using Microsoft.EntityFrameworkCore;
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StoragesContracts;
using PizzeriaContracts.ViewModels;
using PizzeriaDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PizzeriaDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public List<ShopViewModel> GetFullList()
{
using var context = new PizzeriaDatabase();
return context.Shops.Include(x => x.Pizzas).ThenInclude(x => x.Pizza).ToList().
Select(x => x.GetViewModel).ToList();
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
using var context = new PizzeriaDatabase();
return context.Shops.Include(x => x.Pizzas).ThenInclude(x => x.Pizza).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 new();
}
using var context = new PizzeriaDatabase();
return context.Shops.Include(x => x.Pizzas).ThenInclude(x => x.Pizza)
.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 PizzeriaDatabase();
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 PizzeriaDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id);
if (shop == null)
{
return null;
}
shop.Update(model);
context.SaveChanges();
shop.UpdatePizzas(context, model);
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new PizzeriaDatabase();
var shop = context.Shops.Include(x => x.Pizzas).FirstOrDefault(x => x.Id == model.Id);
if (shop != null)
{
context.Shops.Remove(shop);
context.SaveChanges();
return shop.GetViewModel;
}
return null;
}
public bool RestockingShops(SupplyBindingModel model)
{
using var context = new PizzeriaDatabase();
var transaction = context.Database.BeginTransaction();
var Shops = context.Shops.Include(x => x.Pizzas).ThenInclude(x => x.Pizza).ToList().
Where(x => x.PizzaMaxCount > x.ShopPizzas.Select(x => x.Value.Item2).Sum()).ToList();
if (model == null)
{
return false;
}
try
{
foreach (Shop shop in Shops)
{
int difference = shop.PizzaMaxCount - shop.ShopPizzas.Select(x => x.Value.Item2).Sum();
int refill = Math.Min(difference, model.Count);
model.Count -= refill;
if (shop.ShopPizzas.ContainsKey(model.PizzaId))
{
var datePair = shop.ShopPizzas[model.PizzaId];
datePair.Item2 += refill;
shop.ShopPizzas[model.PizzaId] = datePair;
}
else
{
var pizza = context.Pizzas.First(x => x.Id == model.PizzaId);
shop.ShopPizzas.Add(model.PizzaId, (pizza, refill));
}
shop.PizzasDictionatyUpdate(context);
if (model.Count == 0)
{
transaction.Commit();
return true;
}
}
transaction.Rollback();
return false;
}
catch
{
transaction.Rollback();
throw;
}
}
public bool Sale(SupplySearchModel model)
{
using var context = new PizzeriaDatabase();
var transaction = context.Database.BeginTransaction();
try
{
var shops = context.Shops.Include(x => x.Pizzas).ThenInclude(x => x.Pizza).ToList().
Where(x => x.ShopPizzas.ContainsKey(model.PizzaId.Value)).OrderByDescending(x => x.ShopPizzas[model.PizzaId.Value].Item2).ToList();
foreach (var shop in shops)
{
int residue = model.Count.Value - shop.ShopPizzas[model.PizzaId.Value].Item2;
if (residue > 0)
{
shop.ShopPizzas.Remove(model.PizzaId.Value);
shop.PizzasDictionatyUpdate(context);
context.SaveChanges();
model.Count = residue;
}
else
{
if (residue == 0)
shop.ShopPizzas.Remove(model.PizzaId.Value);
else
{
var dataPair = shop.ShopPizzas[model.PizzaId.Value];
dataPair.Item2 = -residue;
shop.ShopPizzas[model.PizzaId.Value] = dataPair;
}
shop.PizzasDictionatyUpdate(context);
transaction.Commit();
return true;
}
}
transaction.Rollback();
return false;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

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 PizzeriaDatabaseImplement;
#nullable disable
namespace PizzeriaDatabaseImplement.Migrations
{
[DbContext(typeof(PizzeriaDatabase))]
[Migration("20230405223259_Hard_init")]
partial class Hard_init
{
/// <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("PizzeriaDatabaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.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>("PizzaId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("PizzaId");
b.ToTable("Orders");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Pizza", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("PizzaName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Pizzas");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.Models.PizzaComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("PizzaId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("PizzaId");
b.ToTable("PizzaComponents");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Adress")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("OpeningDate")
.HasColumnType("datetime2");
b.Property<int>("PizzaMaxCount")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.Models.ShopPizzas", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("PizzaId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("PizzaId");
b.HasIndex("ShopId");
b.ToTable("ShopPizzas");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Order", b =>
{
b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza")
.WithMany("Orders")
.HasForeignKey("PizzaId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Pizza");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.Models.PizzaComponent", b =>
{
b.HasOne("PizzeriaDatabaseImplement.Models.Component", "Component")
.WithMany("PizzaComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza")
.WithMany("Components")
.HasForeignKey("PizzaId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Pizza");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.Models.ShopPizzas", b =>
{
b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza")
.WithMany()
.HasForeignKey("PizzaId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PizzeriaDatabaseImplement.Models.Shop", "Shop")
.WithMany("Pizzas")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Pizza");
b.Navigation("Shop");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Component", b =>
{
b.Navigation("PizzaComponents");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Pizza", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Pizzas");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,78 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace PizzeriaDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class Hard_init : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
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),
Adress = table.Column<string>(type: "nvarchar(max)", nullable: false),
OpeningDate = table.Column<DateTime>(type: "datetime2", nullable: false),
PizzaMaxCount = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable(
name: "ShopPizzas",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
PizzaId = 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_ShopPizzas", x => x.Id);
table.ForeignKey(
name: "FK_ShopPizzas_Pizzas_PizzaId",
column: x => x.PizzaId,
principalTable: "Pizzas",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopPizzas_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_ShopPizzas_PizzaId",
table: "ShopPizzas",
column: "PizzaId");
migrationBuilder.CreateIndex(
name: "IX_ShopPizzas_ShopId",
table: "ShopPizzas",
column: "ShopId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ShopPizzas");
migrationBuilder.DropTable(
name: "Shops");
}
}
}

View File

@ -121,6 +121,59 @@ namespace PizzeriaDatabaseImplement.Migrations
b.ToTable("PizzaComponents");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Adress")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("OpeningDate")
.HasColumnType("datetime2");
b.Property<int>("PizzaMaxCount")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.Models.ShopPizzas", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("PizzaId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("PizzaId");
b.HasIndex("ShopId");
b.ToTable("ShopPizzas");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Order", b =>
{
b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza")
@ -151,6 +204,25 @@ namespace PizzeriaDatabaseImplement.Migrations
b.Navigation("Pizza");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.Models.ShopPizzas", b =>
{
b.HasOne("PizzeriaDatabaseImplement.Models.Pizza", "Pizza")
.WithMany()
.HasForeignKey("PizzaId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PizzeriaDatabaseImplement.Models.Shop", "Shop")
.WithMany("Pizzas")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Pizza");
b.Navigation("Shop");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Component", b =>
{
b.Navigation("PizzaComponents");
@ -162,6 +234,11 @@ namespace PizzeriaDatabaseImplement.Migrations
b.Navigation("Orders");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Pizzas");
});
#pragma warning restore 612, 618
}
}

View File

@ -19,6 +19,8 @@ namespace PizzeriaDatabaseImplement.Models
public DateTime OpeningDate { get; set; }
public int PizzaMaxCount { get; set; }
private Dictionary<int, (IPizzaModel, int)>? _shopPizzas = null;
public Dictionary<int, (IPizzaModel, int)> ShopPizzas
{
@ -52,7 +54,7 @@ namespace PizzeriaDatabaseImplement.Models
Pizza = context.Pizzas.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList(),
Piz = model.ReinforcedMaxCount
PizzaMaxCount = model.PizzaMaxCount
};
}
@ -61,7 +63,7 @@ namespace PizzeriaDatabaseImplement.Models
ShopName = model.ShopName;
Adress = model.Adress;
OpeningDate = model.OpeningDate;
ReinforcedMaxCount = model.ReinforcedMaxCount;
PizzaMaxCount = model.PizzaMaxCount;
}
public ShopViewModel GetViewModel => new()
@ -70,47 +72,47 @@ namespace PizzeriaDatabaseImplement.Models
ShopName = ShopName,
Adress = Adress,
OpeningDate = OpeningDate,
ShopReinforcedes = ShopReinforcedes,
ReinforcedMaxCount = ReinforcedMaxCount
ShopPizzas = ShopPizzas,
PizzaMaxCount = PizzaMaxCount
};
public void UpdateReinforcedes(PrecastConcretePlantDatabase context, ShopBindingModel model)
public void UpdatePizzas(PizzeriaDatabase context, ShopBindingModel model)
{
var ShopReinforcedes = context.ShopReinforcedes.Where(rec => rec.ShopId == model.Id).ToList();
if (ShopReinforcedes != null && ShopReinforcedes.Count > 0)
var ShopPizzas = context.ShopPizzas.Where(rec => rec.ShopId == model.Id).ToList();
if (ShopPizzas != null && ShopPizzas.Count > 0)
{
// удалили те, которых нет в модели
context.ShopReinforcedes.RemoveRange(ShopReinforcedes.Where(rec => !model.ShopReinforcedes.ContainsKey(rec.ReinforcedId)));
context.ShopPizzas.RemoveRange(ShopPizzas.Where(rec => !model.ShopPizzas.ContainsKey(rec.PizzaId)));
context.SaveChanges();
ShopReinforcedes = context.ShopReinforcedes.Where(rec => rec.ShopId == model.Id).ToList();
// обновили количество у существующих записей
foreach (var updateReinforced in ShopReinforcedes)
ShopPizzas = context.ShopPizzas.Where(rec => rec.ShopId == model.Id).ToList();
foreach (var updatePizza in ShopPizzas)
{
updateReinforced.Count = model.ShopReinforcedes[updateReinforced.ReinforcedId].Item2;
model.ShopReinforcedes.Remove(updateReinforced.ReinforcedId);
updatePizza.Count = model.ShopPizzas[updatePizza.PizzaId].Item2;
model.ShopPizzas.Remove(updatePizza.PizzaId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var ar in model.ShopReinforcedes)
foreach (var ar in model.ShopPizzas)
{
context.ShopReinforcedes.Add(new ShopReinforced
context.ShopPizzas.Add(new ShopPizzas
{
Shop = shop,
Reinforced = context.Reinforceds.First(x => x.Id == ar.Key),
Pizza = context.Pizzas.First(x => x.Id == ar.Key),
Count = ar.Value.Item2
});
context.SaveChanges();
}
_shopReinforcedes = null;
_shopPizzas = null;
}
public void ReinforcedDictionatyUpdate(PrecastConcretePlantDatabase context)
public void PizzasDictionatyUpdate(PizzeriaDatabase context)
{
UpdateReinforcedes(context, new ShopBindingModel
UpdatePizzas(context, new ShopBindingModel
{
Id = Id,
ShopReinforcedes = ShopReinforcedes,
ShopPizzas = ShopPizzas,
});
}

View File

@ -10,13 +10,15 @@ namespace PizzeriaDatabaseImplement
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(@"Data Source=SHADOWIK\SHADOWIK;Initial Catalog=PizzeriaDatabase;Integrated Security=True;TrustServerCertificate=True");
optionsBuilder.UseSqlServer(@"Data Source=SHADOWIK\SHADOWIK;Initial Catalog=PizzeriaHardDatabase;Integrated Security=True;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Pizza> Pizzas { set; get; }
public virtual DbSet<PizzaComponent> PizzaComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Component> Components { get; set; }
public virtual DbSet<Pizza> Pizzas { get; set; }
public virtual DbSet<PizzaComponent> PizzaComponents { get; set; }
public virtual DbSet<Order> Orders { get; set; }
public virtual DbSet<Shop> Shops { get; set; }
public virtual DbSet<ShopPizzas> ShopPizzas { get; set; }
}
}