начало работы
This commit is contained in:
parent
857768c1f3
commit
1203148896
@ -125,6 +125,16 @@ namespace SushiBarBusinessLogic.BusinessLogics
|
|||||||
}
|
}
|
||||||
shop.ShopSushis.Add(model.SushiId, (sushi, model.Count));
|
shop.ShopSushis.Add(model.SushiId, (sushi, model.Count));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_shopStorage.Update(new ShopBindingModel()
|
||||||
|
{
|
||||||
|
Id = shop.Id,
|
||||||
|
ShopName = shop.ShopName,
|
||||||
|
Adress = shop.Adress,
|
||||||
|
OpeningDate = shop.OpeningDate,
|
||||||
|
ShopSushis = shop.ShopSushis,
|
||||||
|
SushiMaxCount = shop.SushiMaxCount,
|
||||||
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
185
SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs
Normal file
185
SushiBar/SushiBarDatabaseImplement/Implements/ShopStorage.cs
Normal file
@ -0,0 +1,185 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using SushiBarContracts.BindingModels;
|
||||||
|
using SushiBarContracts.SearchModels;
|
||||||
|
using SushiBarContracts.StoragesContracts;
|
||||||
|
using SushiBarContracts.ViewModels;
|
||||||
|
using SushiBarDatabaseImplement.Models;
|
||||||
|
|
||||||
|
namespace SushiBarDatabaseImplement.Implements
|
||||||
|
{
|
||||||
|
public class ShopStorage : IShopStorage
|
||||||
|
{
|
||||||
|
public List<ShopViewModel> GetFullList()
|
||||||
|
{
|
||||||
|
using var context = new SushiBarDatabase();
|
||||||
|
return context.Shops.Include(x => x.Sushis).ThenInclude(x => x.Sushi).ToList().
|
||||||
|
Select(x => x.GetViewModel).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(model.ShopName))
|
||||||
|
{
|
||||||
|
return new();
|
||||||
|
}
|
||||||
|
using var context = new SushiBarDatabase();
|
||||||
|
return context.Shops.Include(x => x.Sushis).ThenInclude(x => x.Sushi).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 SushiBarDatabase();
|
||||||
|
return context.Shops.Include(x => x.Sushis).ThenInclude(x => x.Sushi)
|
||||||
|
.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 SushiBarDatabase();
|
||||||
|
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 SushiBarDatabase();
|
||||||
|
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.UpdateSushis(context, model);
|
||||||
|
transaction.Commit();
|
||||||
|
return shop.GetViewModel;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
transaction.Rollback();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ShopViewModel? Delete(ShopBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new SushiBarDatabase();
|
||||||
|
var shop = context.Shops.Include(x => x.Sushis).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 SushiBarDatabase();
|
||||||
|
var transaction = context.Database.BeginTransaction();
|
||||||
|
var Shops = context.Shops.Include(x => x.Sushis).ThenInclude(x => x.Sushi).ToList().
|
||||||
|
Where(x => x.SushiMaxCount > x.ShopSushis.Select(x => x.Value.Item2).Sum()).ToList();
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
foreach (Shop shop in Shops)
|
||||||
|
{
|
||||||
|
int difference = shop.SushiMaxCount - shop.ShopSushis.Select(x => x.Value.Item2).Sum();
|
||||||
|
int refill = Math.Min(difference, model.Count);
|
||||||
|
model.Count -= refill;
|
||||||
|
if (shop.ShopSushis.ContainsKey(model.SushiId))
|
||||||
|
{
|
||||||
|
var datePair = shop.ShopSushis[model.SushiId];
|
||||||
|
datePair.Item2 += refill;
|
||||||
|
shop.ShopSushis[model.SushiId] = datePair;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var sushi = context.Sushis.First(x => x.Id == model.SushiId);
|
||||||
|
shop.ShopSushis.Add(model.SushiId, (sushi, refill));
|
||||||
|
}
|
||||||
|
shop.SushisDictionatyUpdate(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 SushiBarDatabase();
|
||||||
|
var transaction = context.Database.BeginTransaction();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var shops = context.Shops.Include(x => x.Sushis).ThenInclude(x => x.Sushi).ToList().
|
||||||
|
Where(x => x.ShopSushis.ContainsKey(model.SushiId.Value)).OrderByDescending(x => x.ShopSushis[model.SushiId.Value].Item2).ToList();
|
||||||
|
|
||||||
|
foreach (var shop in shops)
|
||||||
|
{
|
||||||
|
int residue = model.Count.Value - shop.ShopSushis[model.SushiId.Value].Item2;
|
||||||
|
if (residue > 0)
|
||||||
|
{
|
||||||
|
shop.ShopSushis.Remove(model.SushiId.Value);
|
||||||
|
shop.SushisDictionatyUpdate(context);
|
||||||
|
context.SaveChanges();
|
||||||
|
model.Count = residue;
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (residue == 0)
|
||||||
|
shop.ShopSushis.Remove(model.SushiId.Value);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var dataPair = shop.ShopSushis[model.SushiId.Value];
|
||||||
|
dataPair.Item2 = -residue;
|
||||||
|
shop.ShopSushis[model.SushiId.Value] = dataPair;
|
||||||
|
}
|
||||||
|
|
||||||
|
shop.SushisDictionatyUpdate(context);
|
||||||
|
transaction.Commit();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
transaction.Rollback();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
transaction.Rollback();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -8,118 +8,118 @@ namespace SushiBarDatabaseImplement.Migrations
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public partial class InitialCreate : Migration
|
public partial class InitialCreate : Migration
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void Up(MigrationBuilder migrationBuilder)
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
{
|
{
|
||||||
migrationBuilder.CreateTable(
|
migrationBuilder.CreateTable(
|
||||||
name: "Components",
|
name: "Components",
|
||||||
columns: table => new
|
columns: table => new
|
||||||
{
|
{
|
||||||
Id = table.Column<int>(type: "int", nullable: false)
|
Id = table.Column<int>(type: "int", nullable: false)
|
||||||
.Annotation("SqlServer:Identity", "1, 1"),
|
.Annotation("SqlServer:Identity", "1, 1"),
|
||||||
ComponentName = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
ComponentName = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||||
Cost = table.Column<double>(type: "float", nullable: false)
|
Cost = table.Column<double>(type: "float", nullable: false)
|
||||||
},
|
},
|
||||||
constraints: table =>
|
constraints: table =>
|
||||||
{
|
{
|
||||||
table.PrimaryKey("PK_Components", x => x.Id);
|
table.PrimaryKey("PK_Components", x => x.Id);
|
||||||
});
|
});
|
||||||
|
|
||||||
migrationBuilder.CreateTable(
|
migrationBuilder.CreateTable(
|
||||||
name: "Sushis",
|
name: "Sushis",
|
||||||
columns: table => new
|
columns: table => new
|
||||||
{
|
{
|
||||||
Id = table.Column<int>(type: "int", nullable: false)
|
Id = table.Column<int>(type: "int", nullable: false)
|
||||||
.Annotation("SqlServer:Identity", "1, 1"),
|
.Annotation("SqlServer:Identity", "1, 1"),
|
||||||
SushiName = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
SushiName = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||||
Price = table.Column<double>(type: "float", nullable: false)
|
Price = table.Column<double>(type: "float", nullable: false)
|
||||||
},
|
},
|
||||||
constraints: table =>
|
constraints: table =>
|
||||||
{
|
{
|
||||||
table.PrimaryKey("PK_Sushis", x => x.Id);
|
table.PrimaryKey("PK_Sushis", x => x.Id);
|
||||||
});
|
});
|
||||||
|
|
||||||
migrationBuilder.CreateTable(
|
migrationBuilder.CreateTable(
|
||||||
name: "Orders",
|
name: "Orders",
|
||||||
columns: table => new
|
columns: table => new
|
||||||
{
|
{
|
||||||
Id = table.Column<int>(type: "int", nullable: false)
|
Id = table.Column<int>(type: "int", nullable: false)
|
||||||
.Annotation("SqlServer:Identity", "1, 1"),
|
.Annotation("SqlServer:Identity", "1, 1"),
|
||||||
SushiId = table.Column<int>(type: "int", nullable: false),
|
SushiId = table.Column<int>(type: "int", nullable: false),
|
||||||
Count = table.Column<int>(type: "int", nullable: false),
|
Count = table.Column<int>(type: "int", nullable: false),
|
||||||
Sum = table.Column<double>(type: "float", nullable: false),
|
Sum = table.Column<double>(type: "float", nullable: false),
|
||||||
Status = table.Column<int>(type: "int", nullable: false),
|
Status = table.Column<int>(type: "int", nullable: false),
|
||||||
DateCreate = table.Column<DateTime>(type: "datetime2", nullable: false),
|
DateCreate = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||||
DateImplement = table.Column<DateTime>(type: "datetime2", nullable: true)
|
DateImplement = table.Column<DateTime>(type: "datetime2", nullable: true)
|
||||||
},
|
},
|
||||||
constraints: table =>
|
constraints: table =>
|
||||||
{
|
{
|
||||||
table.PrimaryKey("PK_Orders", x => x.Id);
|
table.PrimaryKey("PK_Orders", x => x.Id);
|
||||||
table.ForeignKey(
|
table.ForeignKey(
|
||||||
name: "FK_Orders_Sushis_SushiId",
|
name: "FK_Orders_Sushis_SushiId",
|
||||||
column: x => x.SushiId,
|
column: x => x.SushiId,
|
||||||
principalTable: "Sushis",
|
principalTable: "Sushis",
|
||||||
principalColumn: "Id",
|
principalColumn: "Id",
|
||||||
onDelete: ReferentialAction.Cascade);
|
onDelete: ReferentialAction.Cascade);
|
||||||
});
|
});
|
||||||
|
|
||||||
migrationBuilder.CreateTable(
|
migrationBuilder.CreateTable(
|
||||||
name: "SushiComponents",
|
name: "SushiComponents",
|
||||||
columns: table => new
|
columns: table => new
|
||||||
{
|
{
|
||||||
Id = table.Column<int>(type: "int", nullable: false)
|
Id = table.Column<int>(type: "int", nullable: false)
|
||||||
.Annotation("SqlServer:Identity", "1, 1"),
|
.Annotation("SqlServer:Identity", "1, 1"),
|
||||||
SushiId = table.Column<int>(type: "int", nullable: false),
|
SushiId = table.Column<int>(type: "int", nullable: false),
|
||||||
ComponentId = table.Column<int>(type: "int", nullable: false),
|
ComponentId = table.Column<int>(type: "int", nullable: false),
|
||||||
Count = table.Column<int>(type: "int", nullable: false)
|
Count = table.Column<int>(type: "int", nullable: false)
|
||||||
},
|
},
|
||||||
constraints: table =>
|
constraints: table =>
|
||||||
{
|
{
|
||||||
table.PrimaryKey("PK_SushiComponents", x => x.Id);
|
table.PrimaryKey("PK_SushiComponents", x => x.Id);
|
||||||
table.ForeignKey(
|
table.ForeignKey(
|
||||||
name: "FK_SushiComponents_Components_ComponentId",
|
name: "FK_SushiComponents_Components_ComponentId",
|
||||||
column: x => x.ComponentId,
|
column: x => x.ComponentId,
|
||||||
principalTable: "Components",
|
principalTable: "Components",
|
||||||
principalColumn: "Id",
|
principalColumn: "Id",
|
||||||
onDelete: ReferentialAction.Cascade);
|
onDelete: ReferentialAction.Cascade);
|
||||||
table.ForeignKey(
|
table.ForeignKey(
|
||||||
name: "FK_SushiComponents_Sushis_SushiId",
|
name: "FK_SushiComponents_Sushis_SushiId",
|
||||||
column: x => x.SushiId,
|
column: x => x.SushiId,
|
||||||
principalTable: "Sushis",
|
principalTable: "Sushis",
|
||||||
principalColumn: "Id",
|
principalColumn: "Id",
|
||||||
onDelete: ReferentialAction.Cascade);
|
onDelete: ReferentialAction.Cascade);
|
||||||
});
|
});
|
||||||
|
|
||||||
migrationBuilder.CreateIndex(
|
migrationBuilder.CreateIndex(
|
||||||
name: "IX_Orders_SushiId",
|
name: "IX_Orders_SushiId",
|
||||||
table: "Orders",
|
table: "Orders",
|
||||||
column: "SushiId");
|
column: "SushiId");
|
||||||
|
|
||||||
migrationBuilder.CreateIndex(
|
migrationBuilder.CreateIndex(
|
||||||
name: "IX_SushiComponents_ComponentId",
|
name: "IX_SushiComponents_ComponentId",
|
||||||
table: "SushiComponents",
|
table: "SushiComponents",
|
||||||
column: "ComponentId");
|
column: "ComponentId");
|
||||||
|
|
||||||
migrationBuilder.CreateIndex(
|
migrationBuilder.CreateIndex(
|
||||||
name: "IX_SushiComponents_SushiId",
|
name: "IX_SushiComponents_SushiId",
|
||||||
table: "SushiComponents",
|
table: "SushiComponents",
|
||||||
column: "SushiId");
|
column: "SushiId");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void Down(MigrationBuilder migrationBuilder)
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
{
|
{
|
||||||
migrationBuilder.DropTable(
|
migrationBuilder.DropTable(
|
||||||
name: "Orders");
|
name: "Orders");
|
||||||
|
|
||||||
migrationBuilder.DropTable(
|
migrationBuilder.DropTable(
|
||||||
name: "SushiComponents");
|
name: "SushiComponents");
|
||||||
|
|
||||||
migrationBuilder.DropTable(
|
migrationBuilder.DropTable(
|
||||||
name: "Components");
|
name: "Components");
|
||||||
|
|
||||||
migrationBuilder.DropTable(
|
migrationBuilder.DropTable(
|
||||||
name: "Sushis");
|
name: "Sushis");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user