Добавить бд т продебажить

This commit is contained in:
goblinrf 2024-04-30 00:56:34 +04:00
parent 315ebdf4a5
commit 7772bf7f38
16 changed files with 902 additions and 2 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

@ -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

@ -19,7 +19,10 @@
<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

@ -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=SHADOWIK\SHADOWIK;Initial Catalog=PizzeriaHardDatabase;Integrated Security=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,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();
}
}