PIbd-21 Afanasev S.S. LabWork03_Hard #13
@ -33,6 +33,8 @@ namespace MotorPlantDatabaseImplement.Models
|
||||
public virtual List<EngineComponent> Components { get; set; } = new();
|
||||
[ForeignKey("EngineId")]
|
||||
public virtual List<Order> Orders { get; set; } = new();
|
||||
[ForeignKey("EngineId")]
|
||||
public virtual List<ShopEngines> ShopEngines { get; set; } = new();
|
||||
public static Engine Create(MotorPlantDataBase context,
|
||||
EngineBindingModel model)
|
||||
{
|
||||
|
@ -12,7 +12,7 @@ using MotorPlantDatabaseImplement;
|
||||
namespace MotorPlantDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(MotorPlantDataBase))]
|
||||
[Migration("20240308215333_InitialCreate")]
|
||||
[Migration("20240502092321_Initial-Create")]
|
||||
partial class InitialCreate
|
||||
{
|
||||
/// <inheritdoc />
|
||||
@ -124,6 +124,59 @@ namespace MotorPlantDatabaseImplement.Migrations
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.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<int>("EngineMaxCount")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTime>("OpeningDate")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("ShopName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Shops");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngines", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("EngineId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("ShopId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("EngineId");
|
||||
|
||||
b.HasIndex("ShopId");
|
||||
|
||||
b.ToTable("ShopEngines");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", b =>
|
||||
{
|
||||
b.HasOne("MotorPlantDatabaseImplement.Models.Component", "Component")
|
||||
@ -152,6 +205,25 @@ namespace MotorPlantDatabaseImplement.Migrations
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngines", b =>
|
||||
{
|
||||
b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine")
|
||||
.WithMany("ShopEngines")
|
||||
.HasForeignKey("EngineId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("MotorPlantDatabaseImplement.Models.Shop", "Shop")
|
||||
.WithMany("Engines")
|
||||
.HasForeignKey("ShopId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Engine");
|
||||
|
||||
b.Navigation("Shop");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("EngineComponents");
|
||||
@ -162,6 +234,13 @@ namespace MotorPlantDatabaseImplement.Migrations
|
||||
b.Navigation("Components");
|
||||
|
||||
b.Navigation("Orders");
|
||||
|
||||
b.Navigation("ShopEngines");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Shop", b =>
|
||||
{
|
||||
b.Navigation("Engines");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
@ -39,6 +39,22 @@ namespace MotorPlantDatabaseImplement.Migrations
|
||||
table.PrimaryKey("PK_Engines", x => x.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),
|
||||
Adress = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
OpeningDate = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||
EngineMaxCount = table.Column<int>(type: "int", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Shops", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "EngineComponents",
|
||||
columns: table => new
|
||||
@ -90,6 +106,33 @@ namespace MotorPlantDatabaseImplement.Migrations
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "ShopEngines",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
EngineId = 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_ShopEngines", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_ShopEngines_Engines_EngineId",
|
||||
column: x => x.EngineId,
|
||||
principalTable: "Engines",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_ShopEngines_Shops_ShopId",
|
||||
column: x => x.ShopId,
|
||||
principalTable: "Shops",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_EngineComponents_ComponentId",
|
||||
table: "EngineComponents",
|
||||
@ -104,6 +147,16 @@ namespace MotorPlantDatabaseImplement.Migrations
|
||||
name: "IX_Orders_EngineId",
|
||||
table: "Orders",
|
||||
column: "EngineId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ShopEngines_EngineId",
|
||||
table: "ShopEngines",
|
||||
column: "EngineId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ShopEngines_ShopId",
|
||||
table: "ShopEngines",
|
||||
column: "ShopId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@ -115,11 +168,17 @@ namespace MotorPlantDatabaseImplement.Migrations
|
||||
migrationBuilder.DropTable(
|
||||
name: "Orders");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "ShopEngines");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Components");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Engines");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Shops");
|
||||
}
|
||||
}
|
||||
}
|
@ -121,6 +121,59 @@ namespace MotorPlantDatabaseImplement.Migrations
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.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<int>("EngineMaxCount")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTime>("OpeningDate")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("ShopName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Shops");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngines", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("EngineId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("ShopId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("EngineId");
|
||||
|
||||
b.HasIndex("ShopId");
|
||||
|
||||
b.ToTable("ShopEngines");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.EngineComponent", b =>
|
||||
{
|
||||
b.HasOne("MotorPlantDatabaseImplement.Models.Component", "Component")
|
||||
@ -149,6 +202,25 @@ namespace MotorPlantDatabaseImplement.Migrations
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngines", b =>
|
||||
{
|
||||
b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine")
|
||||
.WithMany("ShopEngines")
|
||||
.HasForeignKey("EngineId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("MotorPlantDatabaseImplement.Models.Shop", "Shop")
|
||||
.WithMany("Engines")
|
||||
.HasForeignKey("ShopId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Engine");
|
||||
|
||||
b.Navigation("Shop");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("EngineComponents");
|
||||
@ -159,6 +231,13 @@ namespace MotorPlantDatabaseImplement.Migrations
|
||||
b.Navigation("Components");
|
||||
|
||||
b.Navigation("Orders");
|
||||
|
||||
b.Navigation("ShopEngines");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Shop", b =>
|
||||
{
|
||||
b.Navigation("Engines");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ optionsBuilder)
|
||||
{
|
||||
if (optionsBuilder.IsConfigured == false)
|
||||
{
|
||||
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-ETBGTEE;Initial Catalog=MotorPlantDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
|
||||
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-ETBGTEE;Initial Catalog=MotorPlantHardDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
@ -24,6 +24,7 @@ optionsBuilder)
|
||||
public virtual DbSet<Engine> Engines { set; get; }
|
||||
public virtual DbSet<EngineComponent> EngineComponents { set; get; }
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
|
||||
public virtual DbSet<Shop> Shops { get; set; }
|
||||
public virtual DbSet<ShopEngines> ShopEngines { get; set; }
|
||||
}
|
||||
}
|
||||
|
107
MotorPlant/MotorPlantDatabaseImplement/Shop.cs
Normal file
107
MotorPlant/MotorPlantDatabaseImplement/Shop.cs
Normal file
@ -0,0 +1,107 @@
|
||||
using MotorPlantContracts.BindingModels;
|
||||
using MotorPlantContracts.ViewModels;
|
||||
using MotorPlantDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MotorPlantDatabaseImplement.Models
|
||||
{
|
||||
public class Shop : IShopModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string ShopName { get; set; } = string.Empty;
|
||||
public string Adress { get; set; } = string.Empty;
|
||||
public DateTime OpeningDate { get; set; }
|
||||
public int EngineMaxCount { get; set; }
|
||||
private Dictionary<int, (IEngineModel, int)>? _shopEngines = null;
|
||||
public Dictionary<int, (IEngineModel, int)> ShopEngines
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_shopEngines == null)
|
||||
{
|
||||
if (_shopEngines == null)
|
||||
{
|
||||
_shopEngines = Engines
|
||||
.ToDictionary(recSP => recSP.EngineId, recSP => (recSP.Engine as IEngineModel, recSP.Count));
|
||||
}
|
||||
return _shopEngines;
|
||||
}
|
||||
return _shopEngines;
|
||||
}
|
||||
}
|
||||
|
||||
[ForeignKey("ShopId")]
|
||||
public List<ShopEngines> Engines { get; set; } = new();
|
||||
public static Shop Create(MotorPlantDataBase context, ShopBindingModel model)
|
||||
{
|
||||
return new Shop()
|
||||
{
|
||||
Id = model.Id,
|
||||
ShopName = model.ShopName,
|
||||
Adress = model.Adress,
|
||||
OpeningDate = model.OpeningDate,
|
||||
Engines = model.ShopEngines.Select(x => new ShopEngines
|
||||
{
|
||||
Engine = context.Engines.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList(),
|
||||
EngineMaxCount = model.EngineMaxCount
|
||||
};
|
||||
}
|
||||
public void Update(ShopBindingModel model)
|
||||
{
|
||||
ShopName = model.ShopName;
|
||||
Adress = model.Adress;
|
||||
OpeningDate = model.OpeningDate;
|
||||
EngineMaxCount = model.EngineMaxCount;
|
||||
}
|
||||
public ShopViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ShopName = ShopName,
|
||||
Adress = Adress,
|
||||
OpeningDate = OpeningDate,
|
||||
ShopEngines = ShopEngines,
|
||||
EngineMaxCount = EngineMaxCount
|
||||
};
|
||||
public void UpdateEngines(MotorPlantDataBase context, ShopBindingModel model)
|
||||
{
|
||||
var ShopEngines = context.ShopEngines.Where(rec => rec.ShopId == model.Id).ToList();
|
||||
if (ShopEngines != null && ShopEngines.Count > 0)
|
||||
{
|
||||
context.ShopEngines.RemoveRange(ShopEngines.Where(rec => !model.ShopEngines.ContainsKey(rec.EngineId)));
|
||||
context.SaveChanges();
|
||||
ShopEngines = context.ShopEngines.Where(rec => rec.ShopId == model.Id).ToList();
|
||||
foreach (var updateEngine in ShopEngines)
|
||||
{
|
||||
updateEngine.Count = model.ShopEngines[updateEngine.EngineId].Item2;
|
||||
model.ShopEngines.Remove(updateEngine.EngineId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var shop = context.Shops.First(x => x.Id == Id);
|
||||
foreach (var ar in model.ShopEngines)
|
||||
{
|
||||
context.ShopEngines.Add(new ShopEngines
|
||||
{
|
||||
Shop = shop,
|
||||
Engine = context.Engines.First(x => x.Id == ar.Key),
|
||||
Count = ar.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_shopEngines = null;
|
||||
}
|
||||
public void EnginesDictionatyUpdate(MotorPlantDataBase context)
|
||||
{
|
||||
UpdateEngines(context, new ShopBindingModel
|
||||
{
|
||||
Id = Id,
|
||||
ShopEngines = ShopEngines,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
24
MotorPlant/MotorPlantDatabaseImplement/ShopEngines.cs
Normal file
24
MotorPlant/MotorPlantDatabaseImplement/ShopEngines.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using MotorPlantDatabaseImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MotorPlantDatabaseImplement.Models
|
||||
{
|
||||
public class ShopEngines
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int EngineId { get; set; }
|
||||
[Required]
|
||||
public int ShopId { get; set; }
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
public virtual Shop Shop { get; set; } = new();
|
||||
public virtual Engine Engine { get; set; } = new();
|
||||
}
|
||||
}
|
182
MotorPlant/MotorPlantDatabaseImplement/ShopStorage.cs
Normal file
182
MotorPlant/MotorPlantDatabaseImplement/ShopStorage.cs
Normal file
@ -0,0 +1,182 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MotorPlantContracts.BindingModels;
|
||||
using MotorPlantContracts.SearchModels;
|
||||
using MotorPlantContracts.StoragesContracts;
|
||||
using MotorPlantContracts.ViewModels;
|
||||
using MotorPlantDatabaseImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MotorPlantDatabaseImplement.Implements
|
||||
{
|
||||
public class ShopStorage : IShopStorage
|
||||
{
|
||||
public List<ShopViewModel> GetFullList()
|
||||
{
|
||||
using var context = new MotorPlantDataBase();
|
||||
return context.Shops.Include(x => x.Engines).ThenInclude(x => x.Engine).ToList().
|
||||
Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ShopName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new MotorPlantDataBase();
|
||||
return context.Shops.Include(x => x.Engines).ThenInclude(x => x.Engine).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 MotorPlantDataBase();
|
||||
return context.Shops.Include(x => x.Engines).ThenInclude(x => x.Engine)
|
||||
.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 MotorPlantDataBase();
|
||||
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 MotorPlantDataBase();
|
||||
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.UpdateEngines(context, model);
|
||||
transaction.Commit();
|
||||
return shop.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public ShopViewModel? Delete(ShopBindingModel model)
|
||||
{
|
||||
using var context = new MotorPlantDataBase();
|
||||
var shop = context.Shops.Include(x => x.Engines).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 MotorPlantDataBase();
|
||||
var transaction = context.Database.BeginTransaction();
|
||||
var Shops = context.Shops.Include(x => x.Engines).ThenInclude(x => x.Engine).ToList().
|
||||
Where(x => x.EngineMaxCount > x.ShopEngines.Select(x => x.Value.Item2).Sum()).ToList();
|
||||
if (model == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
try
|
||||
{
|
||||
foreach (Shop shop in Shops)
|
||||
{
|
||||
int difference = shop.EngineMaxCount - shop.ShopEngines.Select(x => x.Value.Item2).Sum();
|
||||
int refill = Math.Min(difference, model.Count);
|
||||
model.Count -= refill;
|
||||
if (shop.ShopEngines.ContainsKey(model.EngineId))
|
||||
{
|
||||
var datePair = shop.ShopEngines[model.EngineId];
|
||||
datePair.Item2 += refill;
|
||||
shop.ShopEngines[model.EngineId] = datePair;
|
||||
}
|
||||
else
|
||||
{
|
||||
var Engine = context.Engines.First(x => x.Id == model.EngineId);
|
||||
shop.ShopEngines.Add(model.EngineId, (Engine, refill));
|
||||
}
|
||||
shop.EnginesDictionatyUpdate(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 MotorPlantDataBase();
|
||||
var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var shops = context.Shops.Include(x => x.Engines).ThenInclude(x => x.Engine).ToList().
|
||||
Where(x => x.ShopEngines.ContainsKey(model.EngineId.Value)).OrderByDescending(x => x.ShopEngines[model.EngineId.Value].Item2).ToList();
|
||||
|
||||
foreach (var shop in shops)
|
||||
{
|
||||
int residue = model.Count.Value - shop.ShopEngines[model.EngineId.Value].Item2;
|
||||
if (residue > 0)
|
||||
{
|
||||
shop.ShopEngines.Remove(model.EngineId.Value);
|
||||
shop.EnginesDictionatyUpdate(context);
|
||||
context.SaveChanges();
|
||||
model.Count = residue;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if (residue == 0)
|
||||
shop.ShopEngines.Remove(model.EngineId.Value);
|
||||
else
|
||||
{
|
||||
var dataPair = shop.ShopEngines[model.EngineId.Value];
|
||||
dataPair.Item2 = -residue;
|
||||
shop.ShopEngines[model.EngineId.Value] = dataPair;
|
||||
}
|
||||
|
||||
shop.EnginesDictionatyUpdate(context);
|
||||
transaction.Commit();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user