почти
This commit is contained in:
parent
3ca588732e
commit
a8a93a1f82
@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerBusinessLogic", "..\Ab
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerListImplement", "AbstractShopListImplement\DinerListImplement.csproj", "{A24E7474-4B43-4E81-A6BF-2B7323D5C26A}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerFileImplement", "DinerFileImplement\DinerFileImplement.csproj", "{BF814E5C-E62C-4BFC-9B14-3D2F97F21CE6}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerFileImplement", "DinerFileImplement\DinerFileImplement.csproj", "{BF814E5C-E62C-4BFC-9B14-3D2F97F21CE6}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerDataBaseImplement", "DinerDataBaseImplement\DinerDataBaseImplement.csproj", "{3AC09806-4792-4A84-BCB8-87B29E0E1C8A}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -45,6 +47,10 @@ Global
|
||||
{BF814E5C-E62C-4BFC-9B14-3D2F97F21CE6}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{BF814E5C-E62C-4BFC-9B14-3D2F97F21CE6}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{BF814E5C-E62C-4BFC-9B14-3D2F97F21CE6}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{3AC09806-4792-4A84-BCB8-87B29E0E1C8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{3AC09806-4792-4A84-BCB8-87B29E0E1C8A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{3AC09806-4792-4A84-BCB8-87B29E0E1C8A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{3AC09806-4792-4A84-BCB8-87B29E0E1C8A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -9,12 +9,17 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.14">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\AbstractShopBusinessLogic\DinerBusinessLogic.csproj" />
|
||||
<ProjectReference Include="..\AbstractShopListImplement\DinerListImplement.csproj" />
|
||||
<ProjectReference Include="..\DinerDataBaseImplement\DinerDataBaseImplement.csproj" />
|
||||
<ProjectReference Include="..\DinerFileImplement\DinerFileImplement.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
using DinerContracts.BusinessLogicsContracts;
|
||||
using DinerContracts.StoragesContracts;
|
||||
using DinerFileImplement.Implements;
|
||||
using DinerDataBaseImplement.Implements;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NLog.Extensions.Logging;
|
||||
|
19
Diner/DinerDataBaseImplement/DinerDataBaseImplement.csproj
Normal file
19
Diner/DinerDataBaseImplement/DinerDataBaseImplement.csproj
Normal file
@ -0,0 +1,19 @@
|
||||
<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.Relational" Version="7.0.14" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.14" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\AbstractShopContracts\DinerContracts.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
21
Diner/DinerDataBaseImplement/DinerDatabase.cs
Normal file
21
Diner/DinerDataBaseImplement/DinerDatabase.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using DinerDataBaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DinerDataBaseImplement
|
||||
{
|
||||
public class DinerDatabase : DbContext
|
||||
{
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (optionsBuilder.IsConfigured == false)
|
||||
{
|
||||
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-BUS07H5\SQLEXPRESS;Initial Catalog=DinerDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
public virtual DbSet<Component> Components { set; get; }
|
||||
public virtual DbSet<Snack> Snacks { set; get; }
|
||||
public virtual DbSet<SnackComponent> SnackComponents { set; get; }
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
}
|
||||
}
|
78
Diner/DinerDataBaseImplement/Implements/ComponentStorage.cs
Normal file
78
Diner/DinerDataBaseImplement/Implements/ComponentStorage.cs
Normal file
@ -0,0 +1,78 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
93
Diner/DinerDataBaseImplement/Implements/OrderStorage.cs
Normal file
93
Diner/DinerDataBaseImplement/Implements/OrderStorage.cs
Normal file
@ -0,0 +1,93 @@
|
||||
using DinerContracts.BindingModels;
|
||||
using DinerContracts.SearchModels;
|
||||
using DinerContracts.StoragesContracts;
|
||||
using DinerContracts.ViewModels;
|
||||
using DinerDataBaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
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 null;
|
||||
}
|
||||
using var context = new DinerDatabase();
|
||||
return context.Orders.Include(x => x.Snack)
|
||||
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
||||
}
|
||||
public OrderViewModel? Insert(OrderBindingModel model)
|
||||
{
|
||||
using var context = new DinerDatabase();
|
||||
var newOrder = Order.Create(model);
|
||||
if (newOrder == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Orders.Add(newOrder);
|
||||
context.SaveChanges();
|
||||
return context.Orders.Include(x => x.Snack)
|
||||
.FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; ;
|
||||
}
|
||||
public OrderViewModel? Update(OrderBindingModel model)
|
||||
{
|
||||
using var context = new DinerDatabase();
|
||||
var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (order == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
order.Update(model);
|
||||
context.SaveChanges();
|
||||
|
||||
return context.Orders
|
||||
.Include(x => x.Snack)
|
||||
.FirstOrDefault(x => x.Id == model.Id)?
|
||||
.GetViewModel;
|
||||
}
|
||||
public OrderViewModel? Delete(OrderBindingModel model)
|
||||
{
|
||||
using var context = new DinerDatabase();
|
||||
var element = context.Orders
|
||||
.FirstOrDefault(x => x.Id == model.Id);
|
||||
|
||||
if (element != null)
|
||||
{
|
||||
context.Orders.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
100
Diner/DinerDataBaseImplement/Implements/SnackStorage.cs
Normal file
100
Diner/DinerDataBaseImplement/Implements/SnackStorage.cs
Normal file
@ -0,0 +1,100 @@
|
||||
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)
|
||||
.Include(x => x.Orders)
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Snacks.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
56
Diner/DinerDataBaseImplement/Models/Component.cs
Normal file
56
Diner/DinerDataBaseImplement/Models/Component.cs
Normal file
@ -0,0 +1,56 @@
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
67
Diner/DinerDataBaseImplement/Models/Order.cs
Normal file
67
Diner/DinerDataBaseImplement/Models/Order.cs
Normal file
@ -0,0 +1,67 @@
|
||||
|
||||
using DinerContracts.BindingModels;
|
||||
using DinerContracts.ViewModels;
|
||||
using DinerDataModels.Enum;
|
||||
using DinerDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace DinerDataBaseImplement.Models
|
||||
{
|
||||
public class Order : IOrderModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
[Required]
|
||||
public int SnackId { get; private set; }
|
||||
[Required]
|
||||
public int Count { get; private set; }
|
||||
[Required]
|
||||
public double Sum { get; private set; }
|
||||
[Required]
|
||||
public OrderStatus Status { get; private set; }
|
||||
[Required]
|
||||
public DateTime DateCreate { get; private set; }
|
||||
|
||||
public DateTime? DateImplement { get; private set; }
|
||||
|
||||
public virtual Snack Snack { get; set; }
|
||||
public static Order? Create(OrderBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Order()
|
||||
{
|
||||
Id = model.Id,
|
||||
SnackId = 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;
|
||||
}
|
||||
Sum = model.Sum;
|
||||
Status = model.Status;
|
||||
DateImplement = model.DateImplement;
|
||||
}
|
||||
public OrderViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
SnackId = SnackId,
|
||||
Count = Count,
|
||||
Sum = Sum,
|
||||
Status = Status,
|
||||
DateCreate = DateCreate,
|
||||
DateImplement = DateImplement,
|
||||
SnackName = Snack.SnackName
|
||||
};
|
||||
}
|
||||
}
|
89
Diner/DinerDataBaseImplement/Models/Snack.cs
Normal file
89
Diner/DinerDataBaseImplement/Models/Snack.cs
Normal file
@ -0,0 +1,89 @@
|
||||
using DinerContracts.BindingModels;
|
||||
using DinerContracts.ViewModels;
|
||||
using DinerDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
22
Diner/DinerDataBaseImplement/Models/SnackComponent.cs
Normal file
22
Diner/DinerDataBaseImplement/Models/SnackComponent.cs
Normal file
@ -0,0 +1,22 @@
|
||||
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 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user