ПИбд-23 Кислица Егор Дмитриевич Лабораторная работа №3 (усложненная) #8

Closed
Egor wants to merge 2 commits from Lab3_Hard into Lab2_Hard
23 changed files with 1596 additions and 14 deletions

View File

@ -126,7 +126,16 @@ namespace DinerBusinessLogic.BusinessLogics
}
shop.ShopSnacks.Add(model.SnackId, (snack, model.Count));
}
return true;
_shopStorage.Update(new ShopBindingModel()
{
Id = shop.Id,
ShopName = shop.ShopName,
Adress = shop.Adress,
OpeningDate = shop.OpeningDate,
ShopSnacks = shop.ShopSnacks,
SnackMaxCount = shop.SnackMaxCount,
});
return true;
}
private void CheckModel(ShopBindingModel model, bool withParams = true)

View File

@ -8,6 +8,7 @@ namespace DinerContracts.SearchModels
{
public class SupplySearchModel
{
public int? SnackId { get; set; }
public int? Count { get; set; }
}

View File

@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerListImplement", "Abstr
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerFileImplement", "DinerFileImplement\DinerFileImplement.csproj", "{13294C1E-C8DF-4E4B-B645-A61900A797EB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerDatabaseImplement", "DinerDatabaseImplements\DinerDatabaseImplement.csproj", "{BE778091-B058-4BDD-8AEE-2773F4E00979}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -45,6 +47,10 @@ Global
{13294C1E-C8DF-4E4B-B645-A61900A797EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{13294C1E-C8DF-4E4B-B645-A61900A797EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{13294C1E-C8DF-4E4B-B645-A61900A797EB}.Release|Any CPU.Build.0 = Release|Any CPU
{BE778091-B058-4BDD-8AEE-2773F4E00979}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BE778091-B058-4BDD-8AEE-2773F4E00979}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE778091-B058-4BDD-8AEE-2773F4E00979}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE778091-B058-4BDD-8AEE-2773F4E00979}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -13,13 +13,20 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.14">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\AbstractShopBusinessLogic\DinerBusinessLogic.csproj" />
<ProjectReference Include="..\..\AbstractShopContracts\DinerContracts.csproj" />
<ProjectReference Include="..\..\AbstractShopDataModels\DinerDataModels.csproj" />
<ProjectReference Include="..\AbstractShopListImplement\DinerListImplement.csproj" />
<ProjectReference Include="..\DinerDatabaseImplements\DinerDatabaseImplement.csproj" />
<ProjectReference Include="..\DinerFileImplement\DinerFileImplement.csproj" />
</ItemGroup>

View File

@ -30,7 +30,7 @@
{
comboBoxShop = new ComboBox();
labelShop = new Label();
labelPizza = new Label();
labelSnack = new Label();
comboBoxSnack = new ComboBox();
labelCount = new Label();
textBoxCount = new TextBox();
@ -55,14 +55,14 @@
labelShop.TabIndex = 1;
labelShop.Text = "Магазин: ";
//
// labelPizza
// labelSnack
//
labelPizza.AutoSize = true;
labelPizza.Location = new Point(12, 49);
labelPizza.Name = "labelPizza";
labelPizza.Size = new Size(75, 20);
labelPizza.TabIndex = 2;
labelPizza.Text = "Изделие: ";
labelSnack.AutoSize = true;
labelSnack.Location = new Point(12, 49);
labelSnack.Name = "labelSnack";
labelSnack.Size = new Size(75, 20);
labelSnack.TabIndex = 2;
labelSnack.Text = "Изделие: ";
//
// comboBoxSnack
//
@ -118,7 +118,7 @@
Controls.Add(textBoxCount);
Controls.Add(labelCount);
Controls.Add(comboBoxSnack);
Controls.Add(labelPizza);
Controls.Add(labelSnack);
Controls.Add(labelShop);
Controls.Add(comboBoxShop);
Name = "FormCreateSupply";
@ -132,7 +132,7 @@
private ComboBox comboBoxShop;
private Label labelShop;
private Label labelPizza;
private Label labelSnack;
private ComboBox comboBoxSnack;
private Label labelCount;
private TextBox textBoxCount;

View File

@ -102,7 +102,7 @@
Controls.Add(labelSnack);
Name = "FormSellSnack";
Text = "Продажа закуски";
Load += FormSellingPizza_Load;
Load += FormSellingSnack_Load;
ResumeLayout(false);
PerformLayout();
}

View File

@ -28,7 +28,7 @@ namespace DinerView
_logicP = logicP;
_logicS = logicS;
}
private void FormSellingPizza_Load(object sender, EventArgs e)
private void FormSellingSnack_Load(object sender, EventArgs e)
{
_snackList = _logicP.ReadList(null);
if (_snackList != null)

View File

@ -1,10 +1,11 @@
using DinerContracts.BusinessLogicsContracts;
using DinerContracts.StoragesContracts;
using DinerFileImplement.Implements;
using DinerDatabaseImplement.Implements;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using DinerBusinessLogic.BusinessLogics;
using DinerView;
namespace Diner

View File

@ -0,0 +1,24 @@
using DinerDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
namespace DinerDatabaseImplement
{
public class DinerDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS01;Initial Catalog=DinerDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { get; set; }
public virtual DbSet<Snack> Snacks { get; set; }
public virtual DbSet<SnackComponent> SnackComponents { get; set; }
public virtual DbSet<Order> Orders { get; set; }
public virtual DbSet<Shop> Shops { get; set; }
public virtual DbSet<ShopSnacks> ShopSnacks { get; set; }
}
}

View File

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.14">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\AbstractShopContracts\DinerContracts.csproj" />
<ProjectReference Include="..\..\AbstractShopDataModels\DinerDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,79 @@
using DinerContracts.BindingModels;
using DinerContracts.SearchModels;
using DinerContracts.StoragesContracts;
using DinerContracts.ViewModels;
using DinerDatabaseImplement.Models;
namespace DinerDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
public List<ComponentViewModel> GetFullList()
{
using var context = new DinerDatabase();
return context.Components.Select(x => x.GetViewModel).ToList();
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
using var context = new DinerDatabase();
return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList();
}
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
{
return null;
}
using var context = new DinerDatabase();
return context.Components.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
var newComponent = Component.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new DinerDatabase();
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new DinerDatabase();
var component = context.Components.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
public ComponentViewModel? Delete(ComponentBindingModel model)
{
using var context = new DinerDatabase();
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,79 @@
using DinerContracts.BindingModels;
using DinerContracts.SearchModels;
using DinerContracts.StoragesContracts;
using DinerContracts.ViewModels;
using DinerDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace DinerDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel> GetFullList()
{
using var context = new DinerDatabase();
return context.Orders.Include(x => x.Snack).Select(x => x.GetViewModel).ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new DinerDatabase();
return context.Orders.Include(x => x.Snack).Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new DinerDatabase();
return context.Orders.Include(x => x.Snack).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
using var context = new DinerDatabase();
if (model == null)
return null;
var newOrder = Order.Create(context, model);
if (newOrder == null)
{
return null;
}
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new DinerDatabase();
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return order.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new DinerDatabase();
var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (order != null)
{
context.Orders.Remove(order);
context.SaveChanges();
return order.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,191 @@
using Microsoft.EntityFrameworkCore;
using DinerContracts.BindingModels;
using DinerContracts.SearchModels;
using DinerContracts.StoragesContracts;
using DinerContracts.ViewModels;
using DinerDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DinerDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public List<ShopViewModel> GetFullList()
{
using var context = new DinerDatabase();
return context.Shops.Include(x => x.Snacks).ThenInclude(x => x.Snack).ToList().
Select(x => x.GetViewModel).ToList();
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
using var context = new DinerDatabase();
return context.Shops.Include(x => x.Snacks).ThenInclude(x => x.Snack).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 DinerDatabase();
return context.Shops.Include(x => x.Snacks).ThenInclude(x => x.Snack)
.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 DinerDatabase();
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 DinerDatabase();
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.UpdateSnacks(context, model);
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new DinerDatabase();
var shop = context.Shops.Include(x => x.Snacks).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 DinerDatabase();
var transaction = context.Database.BeginTransaction();
var Shops = context.Shops.Include(x => x.Snacks).ThenInclude(x => x.Snack).ToList().
Where(x => x.SnackMaxCount > x.ShopSnacks.Select(x => x.Value.Item2).Sum()).ToList();
if (model == null)
{
return false;
}
try
{
foreach (Shop shop in Shops)
{
int difference = shop.SnackMaxCount - shop.ShopSnacks.Select(x => x.Value.Item2).Sum();
int refill = Math.Min(difference, model.Count);
model.Count -= refill;
if (shop.ShopSnacks.ContainsKey(model.SnackId))
{
var datePair = shop.ShopSnacks[model.SnackId];
datePair.Item2 += refill;
shop.ShopSnacks[model.SnackId] = datePair;
}
else
{
var snack = context.Snacks.First(x => x.Id == model.SnackId);
shop.ShopSnacks.Add(model.SnackId, (snack, refill));
}
shop.SnacksDictionatyUpdate(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 DinerDatabase();
var transaction = context.Database.BeginTransaction();
try
{
var shops = context.Shops.Include(x => x.Snacks).ThenInclude(x => x.Snack).ToList().
Where(x => x.ShopSnacks.ContainsKey(model.SnackId.Value)).OrderByDescending(x => x.ShopSnacks[model.SnackId.Value].Item2).ToList();
foreach (var shop in shops)
{
int residue = model.Count.Value - shop.ShopSnacks[model.SnackId.Value].Item2;
if (residue > 0)
{
shop.ShopSnacks.Remove(model.SnackId.Value);
shop.SnacksDictionatyUpdate(context);
context.SaveChanges();
model.Count = residue;
}
else
{
if (residue == 0)
shop.ShopSnacks.Remove(model.SnackId.Value);
else
{
var dataPair = shop.ShopSnacks[model.SnackId.Value];
dataPair.Item2 = -residue;
shop.ShopSnacks[model.SnackId.Value] = dataPair;
}
shop.SnacksDictionatyUpdate(context);
transaction.Commit();
return true;
}
}
transaction.Rollback();
return false;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -0,0 +1,94 @@
using DinerContracts.BindingModels;
using DinerContracts.SearchModels;
using DinerContracts.StoragesContracts;
using DinerContracts.ViewModels;
using DinerDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace DinerDatabaseImplement.Implements
{
public class SnackStorage : ISnackStorage
{
public List<SnackViewModel> GetFullList()
{
using var context = new DinerDatabase();
return context.Snacks.Include(x => x.Components).ThenInclude(x => x.Component).ToList()
.Select(x => x.GetViewModel).ToList();
}
public List<SnackViewModel> GetFilteredList(SnackSearchModel model)
{
if (string.IsNullOrEmpty(model.SnackName))
{
return new();
}
using var context = new DinerDatabase();
return context.Snacks.Include(x => x.Components).ThenInclude(x => x.Component)
.Where(x => x.SnackName.Contains(model.SnackName)).ToList().Select(x => x.GetViewModel).ToList();
}
public SnackViewModel? GetElement(SnackSearchModel model)
{
if (string.IsNullOrEmpty(model.SnackName) && !model.Id.HasValue)
{
return null;
}
using var context = new DinerDatabase();
return context.Snacks.Include(x => x.Components).ThenInclude(x => x.Component)
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.SnackName) && x.SnackName == model.SnackName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public SnackViewModel? Insert(SnackBindingModel model)
{
using var context = new DinerDatabase();
var newSnack = Snack.Create(context, model);
if (newSnack == null)
{
return null;
}
context.Snacks.Add(newSnack);
context.SaveChanges();
return newSnack.GetViewModel;
}
public SnackViewModel? Update(SnackBindingModel model)
{
using var context = new DinerDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var Snack = context.Snacks.FirstOrDefault(rec => rec.Id == model.Id);
if (Snack == null)
{
return null;
}
Snack.Update(model);
context.SaveChanges();
Snack.UpdateComponents(context, model);
transaction.Commit();
return Snack.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public SnackViewModel? Delete(SnackBindingModel model)
{
using var context = new DinerDatabase();
var element = context.Snacks.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Snacks.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,248 @@
// <auto-generated />
using System;
using DinerDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DinerDatabaseImplement.Migrations
{
[DbContext(typeof(DinerDatabase))]
[Migration("20240506162016_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.14")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("DinerDatabaseImplement.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("DinerDatabaseImplement.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>("SnackId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("SnackId");
b.ToTable("Orders");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Adress")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("OpeningDate")
.HasColumnType("datetime2");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("SnackMaxCount")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.ShopSnacks", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.Property<int>("SnackId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ShopId");
b.HasIndex("SnackId");
b.ToTable("ShopSnacks");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.Snack", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<double>("Price")
.HasColumnType("float");
b.Property<string>("SnackName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Snacks");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.SnackComponent", 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>("SnackId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("SnackId");
b.ToTable("SnackComponents");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.Order", b =>
{
b.HasOne("DinerDatabaseImplement.Models.Snack", "Snack")
.WithMany("Orders")
.HasForeignKey("SnackId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Snack");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.ShopSnacks", b =>
{
b.HasOne("DinerDatabaseImplement.Models.Shop", "Shop")
.WithMany("Snacks")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("DinerDatabaseImplement.Models.Snack", "Snack")
.WithMany()
.HasForeignKey("SnackId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Shop");
b.Navigation("Snack");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.SnackComponent", b =>
{
b.HasOne("DinerDatabaseImplement.Models.Component", "Component")
.WithMany("SnackComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("DinerDatabaseImplement.Models.Snack", "Snack")
.WithMany("Components")
.HasForeignKey("SnackId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Snack");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.Component", b =>
{
b.Navigation("SnackComponents");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Snacks");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.Snack", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,184 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace DinerDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : 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: "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),
SnackMaxCount = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Snacks",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
SnackName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Snacks", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
SnackId = table.Column<int>(type: "int", 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);
table.ForeignKey(
name: "FK_Orders_Snacks_SnackId",
column: x => x.SnackId,
principalTable: "Snacks",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ShopSnacks",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
SnackId = 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_ShopSnacks", x => x.Id);
table.ForeignKey(
name: "FK_ShopSnacks_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopSnacks_Snacks_SnackId",
column: x => x.SnackId,
principalTable: "Snacks",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "SnackComponents",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
SnackId = 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_SnackComponents", x => x.Id);
table.ForeignKey(
name: "FK_SnackComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_SnackComponents_Snacks_SnackId",
column: x => x.SnackId,
principalTable: "Snacks",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Orders_SnackId",
table: "Orders",
column: "SnackId");
migrationBuilder.CreateIndex(
name: "IX_ShopSnacks_ShopId",
table: "ShopSnacks",
column: "ShopId");
migrationBuilder.CreateIndex(
name: "IX_ShopSnacks_SnackId",
table: "ShopSnacks",
column: "SnackId");
migrationBuilder.CreateIndex(
name: "IX_SnackComponents_ComponentId",
table: "SnackComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_SnackComponents_SnackId",
table: "SnackComponents",
column: "SnackId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "ShopSnacks");
migrationBuilder.DropTable(
name: "SnackComponents");
migrationBuilder.DropTable(
name: "Shops");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Snacks");
}
}
}

View File

@ -0,0 +1,245 @@
// <auto-generated />
using System;
using DinerDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DinerDatabaseImplement.Migrations
{
[DbContext(typeof(DinerDatabase))]
partial class DinerDatabaseModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.14")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("DinerDatabaseImplement.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("DinerDatabaseImplement.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>("SnackId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("SnackId");
b.ToTable("Orders");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Adress")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("OpeningDate")
.HasColumnType("datetime2");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("SnackMaxCount")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.ShopSnacks", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.Property<int>("SnackId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ShopId");
b.HasIndex("SnackId");
b.ToTable("ShopSnacks");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.Snack", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<double>("Price")
.HasColumnType("float");
b.Property<string>("SnackName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Snacks");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.SnackComponent", 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>("SnackId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("SnackId");
b.ToTable("SnackComponents");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.Order", b =>
{
b.HasOne("DinerDatabaseImplement.Models.Snack", "Snack")
.WithMany("Orders")
.HasForeignKey("SnackId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Snack");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.ShopSnacks", b =>
{
b.HasOne("DinerDatabaseImplement.Models.Shop", "Shop")
.WithMany("Snacks")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("DinerDatabaseImplement.Models.Snack", "Snack")
.WithMany()
Review

Связь настроена не до конца

Связь настроена не до конца
.HasForeignKey("SnackId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Shop");
b.Navigation("Snack");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.SnackComponent", b =>
{
b.HasOne("DinerDatabaseImplement.Models.Component", "Component")
.WithMany("SnackComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("DinerDatabaseImplement.Models.Snack", "Snack")
.WithMany("Components")
.HasForeignKey("SnackId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Snack");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.Component", b =>
{
b.Navigation("SnackComponents");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Snacks");
});
modelBuilder.Entity("DinerDatabaseImplement.Models.Snack", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,58 @@
using DinerContracts.BindingModels;
using DinerContracts.ViewModels;
using DinerDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace DinerDatabaseImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
[Required]
public string ComponentName { get; private set; } = string.Empty;
[Required]
public double Cost { get; set; }
[ForeignKey("ComponentId")]
public virtual List<SnackComponent> SnackComponents { get; set; } = new();
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public static Component Create(ComponentViewModel model)
{
return new Component
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public void Update(ComponentBindingModel model)
{
if (model == null)
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
}
}

View File

@ -0,0 +1,73 @@
using DinerContracts.BindingModels;
using DinerContracts.ViewModels;
using DinerDataModels.Enum;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DinerDatabaseImplement.Models
{
public class Order
{
public int Id { get; private set; }
[Required]
public int SnackId { get; private set; }
public virtual Snack Snack { get; set; } = new();
[Required]
public int Count { get; private set; }
[Required]
public double Sum { get; private set; }
[Required]
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public static Order Create(DinerDatabase context, OrderBindingModel model)
{
return new Order()
{
Id = model.Id,
SnackId = model.SnackId,
Snack = context.Snacks.First(x => x.Id == model.SnackId),
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
SnackId = SnackId,
SnackName = Snack.SnackName,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
}
}

View File

@ -0,0 +1,120 @@
using DinerContracts.BindingModels;
using DinerContracts.ViewModels;
using DinerDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DinerDatabaseImplement.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 SnackMaxCount { get; set; }
private Dictionary<int, (ISnackModel, int)>? _shopSnacks = null;
public Dictionary<int, (ISnackModel, int)> ShopSnacks
{
get
{
if (_shopSnacks == null) {
if (_shopSnacks == null)
{
_shopSnacks = Snacks
.ToDictionary(recSP => recSP.SnackId, recSP => (recSP.Snack as ISnackModel, recSP.Count));
}
return _shopSnacks;
}
return _shopSnacks;
}
}
[ForeignKey("ShopId")]
public List<ShopSnacks> Snacks { get; set; } = new();
public static Shop Create(DinerDatabase context, ShopBindingModel model)
{
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
Adress = model.Adress,
OpeningDate = model.OpeningDate,
Snacks = model.ShopSnacks.Select(x => new ShopSnacks
{
Snack = context.Snacks.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList(),
SnackMaxCount = model.SnackMaxCount
};
}
public void Update(ShopBindingModel model)
{
ShopName = model.ShopName;
Adress = model.Adress;
OpeningDate = model.OpeningDate;
SnackMaxCount = model.SnackMaxCount;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Adress = Adress,
OpeningDate = OpeningDate,
ShopSnacks = ShopSnacks,
SnackMaxCount = SnackMaxCount
};
public void UpdateSnacks(DinerDatabase context, ShopBindingModel model)
{
var ShopSnacks = context.ShopSnacks.Where(rec => rec.ShopId == model.Id).ToList();
if (ShopSnacks != null && this.ShopSnacks.Count > 0)
{
context.ShopSnacks.RemoveRange(ShopSnacks.Where(rec => !model.ShopSnacks.ContainsKey(rec.SnackId)));
context.SaveChanges();
ShopSnacks = context.ShopSnacks.Where(rec => rec.ShopId == model.Id).ToList();
foreach (var updateSnack in ShopSnacks)
{
updateSnack.Count = model.ShopSnacks[updateSnack.SnackId].Item2;
model.ShopSnacks.Remove(updateSnack.SnackId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var ar in model.ShopSnacks)
{
context.ShopSnacks.Add(new ShopSnacks
{
Shop = shop,
Snack = context.Snacks.First(x => x.Id == ar.Key),
Count = ar.Value.Item2
});
context.SaveChanges();
}
_shopSnacks = null;
}
public void SnacksDictionatyUpdate(DinerDatabase context)
{
UpdateSnacks(context, new ShopBindingModel
{
Id = Id,
ShopSnacks = ShopSnacks,
});
}
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DinerDatabaseImplement.Models
{
public class ShopSnacks
{
public int Id { get; set; }
[Required]
public int SnackId { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int Count { get; set; }
public virtual Shop Shop { get; set; } = new();
public virtual Snack Snack { get; set; } = new();
}
}

View File

@ -0,0 +1,91 @@
using DinerContracts.BindingModels;
using DinerContracts.ViewModels;
using DinerDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace DinerDatabaseImplement.Models
{
public class Snack : ISnackModel
{
public int Id { get; set; }
[Required]
public string SnackName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _snackComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> SnackComponents
{
get
{
if (_snackComponents == null)
{
_snackComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
return _snackComponents;
}
}
[ForeignKey("SnackId")]
public virtual List<SnackComponent> Components { get; set; } = new();
[ForeignKey("SnackId")]
public virtual List<Order> Orders { get; set; } = new();
public static Snack
Create(DinerDatabase context, SnackBindingModel model)
{
return new Snack()
{
Id = model.Id,
SnackName = model.SnackName,
Price = model.Price,
Components = model.SnackComponents.Select(x => new SnackComponent {
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(SnackBindingModel model)
{
SnackName = model.SnackName;
Price = model.Price;
}
public SnackViewModel GetViewModel => new()
{
Id = Id,
SnackName = SnackName,
Price = Price,
SnackComponents = SnackComponents
};
public void UpdateComponents(DinerDatabase context, SnackBindingModel model)
{
var snackComponents = context.SnackComponents.Where(rec => rec.SnackId == model.Id).ToList();
if (snackComponents != null && snackComponents.Count > 0) {
context.SnackComponents.RemoveRange(snackComponents.Where(rec => !model.SnackComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
foreach (var updateComponent in snackComponents)
{
updateComponent.Count = model.SnackComponents[updateComponent.ComponentId].Item2;
model.SnackComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var snack = context.Snacks.First(x => x.Id == Id);
foreach (var pc in model.SnackComponents) {
context.SnackComponents.Add(new SnackComponent
{
Snack = snack,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_snackComponents = null;
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
namespace DinerDatabaseImplement.Models
{
public class SnackComponent
{
public int Id { get; set; }
[Required]
public int SnackId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Snack Snack { get; set; } = new();
}
}