сдана 6 усложненка

This commit is contained in:
Inohara 2023-04-25 11:18:23 +04:00
parent 91fdd361b6
commit 016b5d3f6b
18 changed files with 584 additions and 1389 deletions

View File

@ -102,52 +102,131 @@ namespace IceCreamBusinessLogic.BusinessLogics
_logger.LogInformation("Order. OrderID:{Id}.Sum:{ Sum}. DocumentId: { DocumentId}", model.Id, model.Sum, model.IceCreamId); _logger.LogInformation("Order. OrderID:{Id}.Sum:{ Sum}. DocumentId: { DocumentId}", model.Id, model.Sum, model.IceCreamId);
} }
public bool SetNewStatus(OrderBindingModel model, OrderStatus orderStatus) public bool SetNewStatus(OrderBindingModel rawModel, OrderStatus newStatus)
{ {
// Находим статус заказа по его айди var viewModel = _orderStorage.GetElement(new OrderSearchModel
var vmodel = _orderStorage.GetElement(new() { Id = model.Id });
if (vmodel == null)
{ {
throw new ArgumentNullException(nameof(model)); Id = rawModel.Id
} });
if ((int)vmodel.Status + 1 != (int)orderStatus && !(vmodel.Status == OrderStatus.Ожидается && orderStatus == OrderStatus.Готов))
{
throw new InvalidOperationException($"Попытка перевести заказ не в следующий статус: " +
$"Текущий статус: {vmodel.Status} \n" +
$"Планируемый статус: {orderStatus} \n" +
$"Доступный статус: {(OrderStatus)((int)vmodel.Status + 1)}");
}
if (orderStatus == OrderStatus.Готов || orderStatus == OrderStatus.Ожидается)
{
var icecream = _iceCreamStorage.GetElement(new() { Id = vmodel.IceCreamId });
if (icecream == null || !_shopLogic.AddIceCream(icecream, vmodel.Count)) if (viewModel == null)
{
_logger.LogWarning("Order model not found");
return false;
}
OrderBindingModel model = new OrderBindingModel
{
Id = viewModel.Id,
IceCreamId = viewModel.IceCreamId,
Status = viewModel.Status,
DateCreate = viewModel.DateCreate,
DateImplement = viewModel.DateImplement,
Count = viewModel.Count,
Sum = viewModel.Sum,
ImplementerId = viewModel.ImplementerId
};
if (rawModel.ImplementerId.HasValue)
{
model.ImplementerId = rawModel.ImplementerId;
}
CheckModel(model);
if (model.Status + 1 != newStatus && model.Status != OrderStatus.Ожидается)
{
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect.");
return false;
}
if (newStatus == OrderStatus.Готов)
{
var icecream = _iceCreamStorage.GetElement(new IceCreamSearchModel() { Id = model.IceCreamId });
if (icecream == null)
{ {
_logger.LogWarning($"Не удалось заполнить магазины изделием '{icecream?.IceCreamName ?? string.Empty}' из заказа {vmodel.Id}"); _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Icecream not found.");
orderStatus = OrderStatus.Ожидается; return false;
} }
else if (CheckSupply(icecream, model.Count) == false)
{ {
orderStatus = OrderStatus.Готов; _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Shop supply error.");
model.Status = OrderStatus.Ожидается;
_orderStorage.Update(model);
return false;
} }
} }
model.Status = orderStatus;
model.DateCreate = vmodel.DateCreate; model.Status = newStatus;
if (model.DateImplement == null) if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now;
model.DateImplement = vmodel.DateImplement;
if (vmodel.ImplementerId.HasValue)
model.ImplementerId = vmodel.ImplementerId;
model.IceCreamId = vmodel.IceCreamId;
model.Sum = vmodel.Sum;
model.Count = vmodel.Count;
if (_orderStorage.Update(model) == null) if (_orderStorage.Update(model) == null)
{ {
model.Status--;
_logger.LogWarning("Update operation failed"); _logger.LogWarning("Update operation failed");
return false; return false;
} }
return true; return true;
} }
public bool CheckSupply(IIceCreamModel model, int count)
{
if (count <= 0)
{
_logger.LogWarning("Check then supply operation error. icecream count < 0.");
return false;
}
int freeSpace = 0;
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace += shop.IceCreamMaxCount;
foreach (var icecream in shop.ShopIceCreams)
{
freeSpace -= icecream.Value.Item2;
}
}
if (freeSpace - count < 0)
{
_logger.LogWarning("Check then supply operation error. There's no place for new icecreams in shops.");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace = shop.IceCreamMaxCount;
foreach (var icecream in shop.ShopIceCreams)
{
freeSpace -= icecream.Value.Item2;
}
if (freeSpace == 0)
{
continue;
}
if (freeSpace - count >= 0)
{
if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, model, count)) count = 0;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (freeSpace - count < 0)
{
if (_shopLogic.SupplyIceCreams(new() { Id = shop.Id }, model, freeSpace)) count -= freeSpace;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (count <= 0)
{
return true;
}
}
return false;
}
public OrderViewModel? ReadElement(OrderSearchModel model) public OrderViewModel? ReadElement(OrderSearchModel model)
{ {
if (model == null) if (model == null)

View File

@ -76,133 +76,49 @@ namespace IceCreamBusinessLogic.BusinessLogics
throw new ArgumentNullException("Count of iceCreams in supply myst be more than 0", nameof(count)); throw new ArgumentNullException("Count of iceCreams in supply myst be more than 0", nameof(count));
} }
_logger.LogInformation("AddPastryInShop. ShopName:{ShopName}.Id:{ Id}", model.Name, model.Id); var shopElement = _shopStorage.GetElement(model);
var element = _shopStorage.GetElement(model); if (shopElement == null)
if (element == null)
{ {
_logger.LogWarning("AddPastryInShop element not found"); _logger.LogWarning("Required shop element not found in storage");
return false; return false;
} }
_logger.LogInformation("AddPastryInShop find. Id:{Id}", element.Id); _logger.LogInformation("Shop element found. ID: {0}, Name: {1}", shopElement.Id, shopElement.Name);
if (element.ShopIceCreams.TryGetValue(iceCream.Id, out var sameIcecream)) var countIcecreams = 0;
foreach (var icecream in shopElement.ShopIceCreams)
{ {
element.ShopIceCreams[iceCream.Id] = (iceCream, sameIcecream.Item2 + count); countIcecreams += icecream.Value.Item2;
_logger.LogInformation("Same iceCream found by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, element.Name); }
if (shopElement.IceCreamMaxCount - countIcecreams >= count)
{
if (shopElement.ShopIceCreams.TryGetValue(iceCream.Id, out var sameIcecream))
{
shopElement.ShopIceCreams[iceCream.Id] = (iceCream, sameIcecream.Item2 + count);
_logger.LogInformation("Same iceCream found by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name);
}
else
{
shopElement.ShopIceCreams[iceCream.Id] = (iceCream, count);
_logger.LogInformation("New iceCream added by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name);
}
_shopStorage.Update(new()
{
Id = shopElement.Id,
Name = shopElement.Name,
Adress = shopElement.Adress,
OpeningDate = shopElement.OpeningDate,
ShopIceCreams = shopElement.ShopIceCreams,
IceCreamMaxCount = shopElement.IceCreamMaxCount
});
} }
else else
{ {
element.ShopIceCreams[iceCream.Id] = (iceCream, count); _logger.LogWarning("Required shop is overflowed");
_logger.LogInformation("New iceCream added by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, element.Name);
}
/* var prevCount = element.ShopIceCreams.GetValueOrDefault(iceCream.Id, (iceCream, 0)).Item2;
element.ShopIceCreams[iceCream.Id] = (iceCream, prevCount + count);
_logger.LogInformation(
"AddPastryInShop. Has been added {count} {pastry} in {ShopName}",
count, iceCream.IceCreamName, element.Name);
*/
_shopStorage.Update(new()
{
Id = element.Id,
Adress = element.Adress,
Name = element.Name,
OpeningDate = element.OpeningDate,
ShopIceCreams = element.ShopIceCreams
});
return true;
/* var shopElement = _shopStorage.GetElement(model);
if (shopElement == null)
{
_logger.LogWarning("Required shop element not found in storage");
return false;
}
_logger.LogInformation("Shop element found. ID: {0}, Name: {1}", shopElement.Id, shopElement.Name);
var countIcecreams = 0;
foreach (var icecream in shopElement.ShopIceCreams)
{
countIcecreams += icecream.Value.Item2;
}
if (shopElement.IceCreamMaxCount - countIcecreams >= count)
{
if (shopElement.ShopIceCreams.TryGetValue(iceCream.Id, out var sameIcecream))
{
shopElement.ShopIceCreams[iceCream.Id] = (iceCream, sameIcecream.Item2 + count);
_logger.LogInformation("Same iceCream found by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name);
}
else
{
shopElement.ShopIceCreams[iceCream.Id] = (iceCream, count);
_logger.LogInformation("New iceCream added by supply. Added {0} of {1} in {2} shop", count, iceCream.IceCreamName, shopElement.Name);
}
_shopStorage.Update(new()
{
Id = shopElement.Id,
Name = shopElement.Name,
Adress = shopElement.Adress,
OpeningDate = shopElement.OpeningDate,
ShopIceCreams = shopElement.ShopIceCreams,
IceCreamMaxCount = shopElement.IceCreamMaxCount
});
}
else
{
_logger.LogWarning("Required shop is overflowed");
return false;
}
return true;*/
}
public bool AddIceCream(IIceCreamModel icecream, int count)
{
if (count <= 0)
{
_logger.LogWarning("AddPastriesInShops. Количество добавляемых изделий должно быть больше 0. Количество - {count}", count);
return false; return false;
} }
var freePlaces = GetFreePlaces(count);
if (freePlaces < 0)
{
_logger.LogInformation("AddPastriesInShops. Не удалось добавить изделия в магазины, поскольку они переполнены." +
"Освободите магазины на {places} изделий", -freePlaces);
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
/* int freeInShop = shop.IceCreamMaxCount - shop.ShopIceCreams.Select(x => x.Value.Item2).Sum();
if (freeInShop < count) continue;*/
var cnt = Math.Min(count, shop.IceCreamMaxCount - shop.ShopIceCreams.Select(x => x.Value.Item2).Sum());
if (cnt <= 0)
{
continue;
}
if (!SupplyIceCreams(new() { Id = shop.Id }, icecream, cnt))
{
_logger.LogWarning("При добавления изделий во все магазины произошла ошибка");
return false;
}
count -= cnt;
if (count == 0)
{
return true;
}
}
return true; return true;
} }
//свободные места со всех магазинов
public int GetFreePlaces(int countIceCreams)
{
// Сумма разностей между максимальный кол-вом изделий и суммой всех изделий в магазине
return _shopStorage.GetFullList()
.Select(x => x.IceCreamMaxCount - x.ShopIceCreams.Select(p => p.Value.Item2).Sum())
.Sum() - countIceCreams;
}
public bool SellIceCreams(IIceCreamModel iceCream, int count) public bool SellIceCreams(IIceCreamModel iceCream, int count)
{ {
return _shopStorage.SellIceCreams(iceCream, count); return _shopStorage.SellIceCreams(iceCream, count);

View File

@ -57,35 +57,9 @@ namespace IceCreamBusinessLogic.BusinessLogic
return; return;
} }
// Для заказов находящихся в статусе ожидания работник не должен делать никакую работу, а должен просто пытаться перевести их в статус готов await RunWaitingOrder(implementer);
await Task.Run(() =>
{
foreach (var order in orders.Where(x => x.Status == OrderStatus.Ожидается && x.ImplementerId == implementer.Id))
{
try
{
_orderLogic.DeliveryOrder(new OrderBindingModel
{
Id = order.Id
});
}
// кто-то мог уже перехватить
catch (InvalidOperationException ex)
{
_logger.LogWarning(ex, "Error try get work");
}
// заканчиваем выполнение имитации в случае иной ошибки
catch (Exception ex)
{
_logger.LogError(ex, "Error while do work");
throw;
}
// отдыхаем
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
}
});
await RunOrderInWork(implementer, orders); await RunOrderInWork(implementer, orders);
await Task.Run(() => await Task.Run(() =>
{ {
@ -106,6 +80,7 @@ namespace IceCreamBusinessLogic.BusinessLogic
_orderLogic.DeliveryOrder(new OrderBindingModel _orderLogic.DeliveryOrder(new OrderBindingModel
{ {
Id = order.Id, Id = order.Id,
ImplementerId = implementer.Id
}); });
// отдыхаем // отдыхаем
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
@ -168,5 +143,41 @@ namespace IceCreamBusinessLogic.BusinessLogic
throw; throw;
} }
} }
}
private async Task RunWaitingOrder(ImplementerViewModel implementer)
{
if (_orderLogic == null || implementer == null)
{
return;
}
try
{
var order = await Task.Run(() => _orderLogic.ReadElement(new OrderSearchModel
{
ImplementerId = implementer.Id,
Statusses = new() { OrderStatus.Ожидается }
}));
if (order == null)
{
return;
}
_logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, order.Id);
_orderLogic.DeliveryOrder(new OrderBindingModel
{
Id = order.Id,
ImplementerId = implementer.Id
});
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
}
catch (InvalidOperationException ex)
{
_logger.LogWarning(ex, "Error try get work");
}
catch (Exception ex)
{
_logger.LogError(ex, "Error while do work");
throw;
}
}
}
} }

View File

@ -19,7 +19,5 @@ namespace IceCreamShopContracts.BusinessLogicsContracts
bool Delete(ShopBindingModel model); bool Delete(ShopBindingModel model);
bool SupplyIceCreams(ShopSearchModel model, IIceCreamModel iceCream, int count); bool SupplyIceCreams(ShopSearchModel model, IIceCreamModel iceCream, int count);
bool SellIceCreams(IIceCreamModel iceCream, int count); bool SellIceCreams(IIceCreamModel iceCream, int count);
bool AddIceCream(IIceCreamModel icecream, int count);
int GetFreePlaces(int countIceCreams);
} }
} }

View File

@ -20,4 +20,8 @@
<ProjectReference Include="..\IceCreamShopDataModels\IceCreamShopDataModels.csproj" /> <ProjectReference Include="..\IceCreamShopDataModels\IceCreamShopDataModels.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>
</Project> </Project>

View File

@ -93,31 +93,40 @@ namespace IceCreamShopDatabaseImplement.Implements
public ShopViewModel? Update(ShopBindingModel model) public ShopViewModel? Update(ShopBindingModel model)
{ {
using var context = new IceCreamShopDatabase(); using var context = new IceCreamShopDatabase();
var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id); using var transaction = context.Database.BeginTransaction();
if (shop == null) try
{ {
return null; var shop = context.Shops.Include(x => x.IceCreams).FirstOrDefault(x => x.Id == model.Id);
if (shop == null)
{
return null;
}
shop.Update(model);
context.SaveChanges();
if (model.ShopIceCreams.Count > 0)
{
shop.UpdateIceCreams(context, model);
}
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
} }
shop.Update(model);
shop.UpdateIceCreams(context, model);
context.SaveChanges();
return shop.GetViewModel;
} }
public bool SellIceCreams(IIceCreamModel icecream, int needCount) public bool SellIceCreams(IIceCreamModel icecream, int needCount)
{ {
using var context = new IceCreamShopDatabase(); using var context = new IceCreamShopDatabase();
using var transaction = context.Database.BeginTransaction(); using var transaction = context.Database.BeginTransaction();
foreach (var sp in context.ShopIcecreams.Where(x => x.IceCreamId == icecream.Id)) foreach (var shopDocs in context.ShopIcecreams.Where(x => x.IceCreamId == icecream.Id))
{ {
var res = Math.Min(needCount, sp.Count); var min = Math.Min(needCount, shopDocs.Count);
sp.Count -= res; shopDocs.Count -= min;
needCount -= res; needCount -= min;
if (sp.Count == 0) // Изделия больше нет в магазине, значит удаляем его if (needCount <= 0)
{
context.ShopIcecreams.Remove(sp);
}
if (needCount == 0) // Нельзя коммитить изменения в цикле, что использует контекст, поэтому выходим
{ {
break; break;
} }
@ -128,9 +137,14 @@ namespace IceCreamShopDatabaseImplement.Implements
transaction.Commit(); transaction.Commit();
} }
else else
{
transaction.Rollback(); transaction.Rollback();
}
return needCount == 0; if (needCount > 0)
{
return false;
}
return true;
} }
} }
} }

View File

@ -1,160 +0,0 @@
// <auto-generated />
using System;
using IceCreamShopDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace IceCreamShopDatabaseImplement.Migrations
{
[DbContext(typeof(IceCreamShopDatabase))]
[Migration("20230226160632_InitMigration")]
partial class InitMigration
{
/// <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("IceCreamShopDatabaseImplement.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("IceCreamShopDatabaseImplement.Models.IceCream", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("IceCreamName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("IceCreams");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", 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>("IceCreamId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("IceCreamId");
b.ToTable("IceCreamComponents");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.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>("IceCreamId")
.HasColumnType("int");
b.Property<string>("IceCreamName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Orders");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b =>
{
b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component")
.WithMany("IceCreamComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream")
.WithMany("Components")
.HasForeignKey("IceCreamId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("IceCream");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("IceCreamComponents");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b =>
{
b.Navigation("Components");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -1,115 +0,0 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IceCreamShopDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class InitMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Components",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ComponentName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Cost = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Components", x => x.Id);
});
migrationBuilder.CreateTable(
name: "IceCreams",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
IceCreamName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_IceCreams", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
IceCreamId = table.Column<int>(type: "int", nullable: false),
IceCreamName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Count = table.Column<int>(type: "int", nullable: false),
Sum = table.Column<double>(type: "float", nullable: false),
Status = table.Column<int>(type: "int", nullable: false),
DateCreate = table.Column<DateTime>(type: "datetime2", nullable: false),
DateImplement = table.Column<DateTime>(type: "datetime2", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Orders", x => x.Id);
});
migrationBuilder.CreateTable(
name: "IceCreamComponents",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
IceCreamId = table.Column<int>(type: "int", nullable: false),
ComponentId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_IceCreamComponents", x => x.Id);
table.ForeignKey(
name: "FK_IceCreamComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_IceCreamComponents_IceCreams_IceCreamId",
column: x => x.IceCreamId,
principalTable: "IceCreams",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_IceCreamComponents_ComponentId",
table: "IceCreamComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_IceCreamComponents_IceCreamId",
table: "IceCreamComponents",
column: "IceCreamId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "IceCreamComponents");
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "IceCreams");
}
}
}

View File

@ -1,171 +0,0 @@
// <auto-generated />
using System;
using IceCreamShopDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace IceCreamShopDatabaseImplement.Migrations
{
[DbContext(typeof(IceCreamShopDatabase))]
[Migration("20230313191542_NewMigration")]
partial class NewMigration
{
/// <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("IceCreamShopDatabaseImplement.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("IceCreamShopDatabaseImplement.Models.IceCream", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("IceCreamName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("IceCreams");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", 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>("IceCreamId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("IceCreamId");
b.ToTable("IceCreamComponents");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.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>("IceCreamId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("IceCreamId");
b.ToTable("Orders");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b =>
{
b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component")
.WithMany("IceCreamComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream")
.WithMany("Components")
.HasForeignKey("IceCreamId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("IceCream");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream")
.WithMany("Orders")
.HasForeignKey("IceCreamId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("IceCream");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("IceCreamComponents");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -1,22 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IceCreamShopDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class NewMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

View File

@ -1,214 +0,0 @@
// <auto-generated />
using System;
using IceCreamShopDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace IceCreamShopDatabaseImplement.Migrations
{
[DbContext(typeof(IceCreamShopDatabase))]
[Migration("20230327111426_LabWork05Migr")]
partial class LabWork05Migr
{
/// <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("IceCreamShopDatabaseImplement.Models.Client", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ClientFIO")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Email")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Clients");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.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("IceCreamShopDatabaseImplement.Models.IceCream", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("IceCreamName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("IceCreams");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", 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>("IceCreamId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("IceCreamId");
b.ToTable("IceCreamComponents");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ClientId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("IceCreamId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("ClientId");
b.HasIndex("IceCreamId");
b.ToTable("Orders");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b =>
{
b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component")
.WithMany("IceCreamComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream")
.WithMany("Components")
.HasForeignKey("IceCreamId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("IceCream");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("IceCreamShopDatabaseImplement.Models.Client", "Client")
.WithMany("Orders")
.HasForeignKey("ClientId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream")
.WithMany("Orders")
.HasForeignKey("IceCreamId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Client");
b.Navigation("IceCream");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Client", b =>
{
b.Navigation("Orders");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("IceCreamComponents");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -1,69 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IceCreamShopDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class LabWork05Migr : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("TRUNCATE TABLE Orders");
migrationBuilder.AddColumn<int>(
name: "ClientId",
table: "Orders",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.CreateTable(
name: "Clients",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ClientFIO = table.Column<string>(type: "nvarchar(max)", nullable: false),
Email = table.Column<string>(type: "nvarchar(max)", nullable: false),
Password = table.Column<string>(type: "nvarchar(max)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Clients", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_Orders_ClientId",
table: "Orders",
column: "ClientId");
migrationBuilder.AddForeignKey(
name: "FK_Orders_Clients_ClientId",
table: "Orders",
column: "ClientId",
principalTable: "Clients",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Orders_Clients_ClientId",
table: "Orders");
migrationBuilder.DropTable(
name: "Clients");
migrationBuilder.DropIndex(
name: "IX_Orders_ClientId",
table: "Orders");
migrationBuilder.DropColumn(
name: "ClientId",
table: "Orders");
}
}
}

View File

@ -1,257 +0,0 @@
// <auto-generated />
using System;
using IceCreamShopDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace IceCreamShopDatabaseImplement.Migrations
{
[DbContext(typeof(IceCreamShopDatabase))]
[Migration("20230408135215_implementer")]
partial class implementer
{
/// <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("IceCreamShopDatabaseImplement.Models.Client", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ClientFIO")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Email")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Clients");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.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("IceCreamShopDatabaseImplement.Models.IceCream", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("IceCreamName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("IceCreams");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", 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>("IceCreamId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("IceCreamId");
b.ToTable("IceCreamComponents");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Implementer", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ImplementerFIO")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Qualification")
.HasColumnType("int");
b.Property<int>("WorkExperience")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Implementers");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ClientId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("IceCreamId")
.HasColumnType("int");
b.Property<int?>("ImplementerId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("ClientId");
b.HasIndex("IceCreamId");
b.HasIndex("ImplementerId");
b.ToTable("Orders");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b =>
{
b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component")
.WithMany("IceCreamComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream")
.WithMany("Components")
.HasForeignKey("IceCreamId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("IceCream");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("IceCreamShopDatabaseImplement.Models.Client", "Client")
.WithMany("Orders")
.HasForeignKey("ClientId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream")
.WithMany("Orders")
.HasForeignKey("IceCreamId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("IceCreamShopDatabaseImplement.Models.Implementer", "Implementer")
.WithMany("Orders")
.HasForeignKey("ImplementerId");
b.Navigation("Client");
b.Navigation("IceCream");
b.Navigation("Implementer");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Client", b =>
{
b.Navigation("Orders");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("IceCreamComponents");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Implementer", b =>
{
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -1,67 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IceCreamShopDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class implementer : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "ImplementerId",
table: "Orders",
type: "int",
nullable: true);
migrationBuilder.CreateTable(
name: "Implementers",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ImplementerFIO = table.Column<string>(type: "nvarchar(max)", nullable: false),
Password = table.Column<string>(type: "nvarchar(max)", nullable: false),
WorkExperience = table.Column<int>(type: "int", nullable: false),
Qualification = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Implementers", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_Orders_ImplementerId",
table: "Orders",
column: "ImplementerId");
migrationBuilder.AddForeignKey(
name: "FK_Orders_Implementers_ImplementerId",
table: "Orders",
column: "ImplementerId",
principalTable: "Implementers",
principalColumn: "Id");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Orders_Implementers_ImplementerId",
table: "Orders");
migrationBuilder.DropTable(
name: "Implementers");
migrationBuilder.DropIndex(
name: "IX_Orders_ImplementerId",
table: "Orders");
migrationBuilder.DropColumn(
name: "ImplementerId",
table: "Orders");
}
}
}

View File

@ -1,95 +0,0 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IceCreamShopDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class shops : 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"),
Name = 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),
IceCreamMaxCount = table.Column<int>(type: "int", nullable: false),
IceCreamId = table.Column<int>(type: "int", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
table.ForeignKey(
name: "FK_Shops_IceCreams_IceCreamId",
column: x => x.IceCreamId,
principalTable: "IceCreams",
principalColumn: "Id");
});
migrationBuilder.CreateTable(
name: "IceCreams",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
IceCreamId = 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_ShopIcecreams", x => x.Id);
table.ForeignKey(
name: "FK_ShopIcecreams_IceCreams_IceCreamId",
column: x => x.IceCreamId,
principalTable: "IceCreams",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopIcecreams_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_ShopIcecreams_IceCreamId",
table: "IceCreams",
column: "IceCreamId");
migrationBuilder.CreateIndex(
name: "IX_ShopIcecreams_ShopId",
table: "IceCreams",
column: "ShopId");
migrationBuilder.CreateIndex(
name: "IX_Shops_IceCreamId",
table: "Shops",
column: "IceCreamId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "IceCreams");
migrationBuilder.DropTable(
name: "Shops");
migrationBuilder.DropColumn(
name: "IceCreamName",
table: "Orders");
}
}
}

View File

@ -12,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace IceCreamShopDatabaseImplement.Migrations namespace IceCreamShopDatabaseImplement.Migrations
{ {
[DbContext(typeof(IceCreamShopDatabase))] [DbContext(typeof(IceCreamShopDatabase))]
[Migration("20230409123459_shops")] [Migration("20230425055945_lab6_migr")]
partial class shops partial class lab6_migr
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -25,6 +25,31 @@ namespace IceCreamShopDatabaseImplement.Migrations
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Client", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ClientFIO")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Email")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Clients");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
@ -91,6 +116,33 @@ namespace IceCreamShopDatabaseImplement.Migrations
b.ToTable("IceCreamComponents"); b.ToTable("IceCreamComponents");
}); });
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Implementer", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ImplementerFIO")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Qualification")
.HasColumnType("int");
b.Property<int>("WorkExperience")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Implementers");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
@ -99,6 +151,9 @@ namespace IceCreamShopDatabaseImplement.Migrations
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id")); SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ClientId")
.HasColumnType("int");
b.Property<int>("Count") b.Property<int>("Count")
.HasColumnType("int"); .HasColumnType("int");
@ -111,9 +166,8 @@ namespace IceCreamShopDatabaseImplement.Migrations
b.Property<int>("IceCreamId") b.Property<int>("IceCreamId")
.HasColumnType("int"); .HasColumnType("int");
b.Property<string>("IceCreamName") b.Property<int?>("ImplementerId")
.IsRequired() .HasColumnType("int");
.HasColumnType("nvarchar(max)");
b.Property<int>("Status") b.Property<int>("Status")
.HasColumnType("int"); .HasColumnType("int");
@ -123,8 +177,12 @@ namespace IceCreamShopDatabaseImplement.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("ClientId");
b.HasIndex("IceCreamId"); b.HasIndex("IceCreamId");
b.HasIndex("ImplementerId");
b.ToTable("Orders"); b.ToTable("Orders");
}); });
@ -183,7 +241,7 @@ namespace IceCreamShopDatabaseImplement.Migrations
b.HasIndex("ShopId"); b.HasIndex("ShopId");
b.ToTable("IceCreams"); b.ToTable("ShopIcecreams");
}); });
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b =>
@ -207,11 +265,27 @@ namespace IceCreamShopDatabaseImplement.Migrations
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b =>
{ {
b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", null) b.HasOne("IceCreamShopDatabaseImplement.Models.Client", "Client")
.WithMany("Orders")
.HasForeignKey("ClientId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream")
.WithMany("Orders") .WithMany("Orders")
.HasForeignKey("IceCreamId") .HasForeignKey("IceCreamId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .IsRequired();
b.HasOne("IceCreamShopDatabaseImplement.Models.Implementer", "Implementer")
.WithMany("Orders")
.HasForeignKey("ImplementerId");
b.Navigation("Client");
b.Navigation("IceCream");
b.Navigation("Implementer");
}); });
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Shop", b => modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Shop", b =>
@ -240,6 +314,11 @@ namespace IceCreamShopDatabaseImplement.Migrations
b.Navigation("Shop"); b.Navigation("Shop");
}); });
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Client", b =>
{
b.Navigation("Orders");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b =>
{ {
b.Navigation("IceCreamComponents"); b.Navigation("IceCreamComponents");
@ -254,6 +333,11 @@ namespace IceCreamShopDatabaseImplement.Migrations
b.Navigation("Shops"); b.Navigation("Shops");
}); });
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Implementer", b =>
{
b.Navigation("Orders");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Shop", b => modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Shop", b =>
{ {
b.Navigation("IceCreams"); b.Navigation("IceCreams");

View File

@ -0,0 +1,255 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IceCreamShopDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class lab6_migr : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Clients",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ClientFIO = table.Column<string>(type: "nvarchar(max)", nullable: false),
Email = table.Column<string>(type: "nvarchar(max)", nullable: false),
Password = table.Column<string>(type: "nvarchar(max)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Clients", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Components",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ComponentName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Cost = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Components", x => x.Id);
});
migrationBuilder.CreateTable(
name: "IceCreams",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
IceCreamName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_IceCreams", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Implementers",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ImplementerFIO = table.Column<string>(type: "nvarchar(max)", nullable: false),
Password = table.Column<string>(type: "nvarchar(max)", nullable: false),
WorkExperience = table.Column<int>(type: "int", nullable: false),
Qualification = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Implementers", x => x.Id);
});
migrationBuilder.CreateTable(
name: "IceCreamComponents",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
IceCreamId = table.Column<int>(type: "int", nullable: false),
ComponentId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_IceCreamComponents", x => x.Id);
table.ForeignKey(
name: "FK_IceCreamComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_IceCreamComponents_IceCreams_IceCreamId",
column: x => x.IceCreamId,
principalTable: "IceCreams",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Shops",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = 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),
IceCreamMaxCount = table.Column<int>(type: "int", nullable: false),
IceCreamId = table.Column<int>(type: "int", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
table.ForeignKey(
name: "FK_Shops_IceCreams_IceCreamId",
column: x => x.IceCreamId,
principalTable: "IceCreams",
principalColumn: "Id");
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
IceCreamId = table.Column<int>(type: "int", nullable: false),
ClientId = table.Column<int>(type: "int", nullable: false),
ImplementerId = table.Column<int>(type: "int", nullable: true),
Count = table.Column<int>(type: "int", nullable: false),
Sum = table.Column<double>(type: "float", nullable: false),
Status = table.Column<int>(type: "int", nullable: false),
DateCreate = table.Column<DateTime>(type: "datetime2", nullable: false),
DateImplement = table.Column<DateTime>(type: "datetime2", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Orders", x => x.Id);
table.ForeignKey(
name: "FK_Orders_Clients_ClientId",
column: x => x.ClientId,
principalTable: "Clients",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_Orders_IceCreams_IceCreamId",
column: x => x.IceCreamId,
principalTable: "IceCreams",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_Orders_Implementers_ImplementerId",
column: x => x.ImplementerId,
principalTable: "Implementers",
principalColumn: "Id");
});
migrationBuilder.CreateTable(
name: "ShopIcecreams",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
IceCreamId = 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_ShopIcecreams", x => x.Id);
table.ForeignKey(
name: "FK_ShopIcecreams_IceCreams_IceCreamId",
column: x => x.IceCreamId,
principalTable: "IceCreams",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopIcecreams_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_IceCreamComponents_ComponentId",
table: "IceCreamComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_IceCreamComponents_IceCreamId",
table: "IceCreamComponents",
column: "IceCreamId");
migrationBuilder.CreateIndex(
name: "IX_Orders_ClientId",
table: "Orders",
column: "ClientId");
migrationBuilder.CreateIndex(
name: "IX_Orders_IceCreamId",
table: "Orders",
column: "IceCreamId");
migrationBuilder.CreateIndex(
name: "IX_Orders_ImplementerId",
table: "Orders",
column: "ImplementerId");
migrationBuilder.CreateIndex(
name: "IX_ShopIcecreams_IceCreamId",
table: "ShopIcecreams",
column: "IceCreamId");
migrationBuilder.CreateIndex(
name: "IX_ShopIcecreams_ShopId",
table: "ShopIcecreams",
column: "ShopId");
migrationBuilder.CreateIndex(
name: "IX_Shops_IceCreamId",
table: "Shops",
column: "IceCreamId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "IceCreamComponents");
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "ShopIcecreams");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Clients");
migrationBuilder.DropTable(
name: "Implementers");
migrationBuilder.DropTable(
name: "Shops");
migrationBuilder.DropTable(
name: "IceCreams");
}
}
}

View File

@ -70,6 +70,7 @@ namespace IceCreamShopDatabaseImplement.Models
Name = model.Name; Name = model.Name;
Adress = model.Adress; Adress = model.Adress;
OpeningDate = model.OpeningDate; OpeningDate = model.OpeningDate;
IceCreamMaxCount = IceCreamMaxCount;
} }
public ShopViewModel GetViewModel => new() public ShopViewModel GetViewModel => new()
{ {
@ -83,30 +84,33 @@ namespace IceCreamShopDatabaseImplement.Models
public void UpdateIceCreams(IceCreamShopDatabase context, ShopBindingModel model) public void UpdateIceCreams(IceCreamShopDatabase context, ShopBindingModel model)
{ {
var shopIceCreams = context.ShopIcecreams //if (model.ShopIceCreams == null) return;
.Where(rec => rec.ShopId == model.Id) var iceCreams = context.ShopIcecreams.Where(rec => rec.ShopId == model.Id).ToList();
.ToList(); if (iceCreams != null && iceCreams.Count > 0)
// удалили те, которых нет в модели
if (shopIceCreams != null && shopIceCreams.Count > 0)
{ {
context.ShopIcecreams // удалили те, которых нет в модели
.RemoveRange(shopIceCreams context.ShopIcecreams.RemoveRange(iceCreams.Where(rec => !model.ShopIceCreams.ContainsKey(rec.IceCreamId)));
.Where(rec => !model.ShopIceCreams context.SaveChanges();
.ContainsKey(rec.IceCreamId))); iceCreams = context.ShopIcecreams.Where(rec => rec.ShopId == model.Id).ToList();
// обновили количество у существующих записей // обновили количество у существующих записей
foreach (var updateIcecream in shopIceCreams.Where(x => model.ShopIceCreams.ContainsKey(x.IceCreamId))) foreach (var updateIceCream in iceCreams)
{ {
updateIcecream.Count = model.ShopIceCreams[updateIcecream.IceCreamId].Item2; updateIceCream.Count = model.ShopIceCreams[updateIceCream.IceCreamId].Item2;
model.ShopIceCreams.Remove(updateIcecream.IceCreamId); model.ShopIceCreams.Remove(updateIceCream.IceCreamId);
} }
context.SaveChanges();
} }
var shop = context.Shops.First(x => x.Id == model.Id); var shop = context.Shops.First(x => x.Id == Id);
shop.IceCreams.AddRange(model.ShopIceCreams.Select(x => new ShopIcecream foreach (var elem in model.ShopIceCreams)
{ {
IceCream = context.IceCreams.First(y => y.Id == x.Key), context.ShopIcecreams.Add(new ShopIcecream
Count = x.Value.Item2, {
}).Except(shopIceCreams ?? new())); Shop = shop,
context.SaveChanges(); IceCream = context.IceCreams.First(x => x.Id == elem.Key),
Count = elem.Value.Item2
});
context.SaveChanges();
}
_cashedIcecreams = null; _cashedIcecreams = null;
} }
} }