PIbd-22_Chernyshev_G.Y._LabWork03_Hard #11
@ -13,7 +13,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GarmentFactoryBusinessLogic
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GarmentFactoryListImplement", "..\GarmentFactoryListImplement\GarmentFactoryListImplement.csproj", "{DEDDF6CC-7C47-4A10-A011-77395F4B7F30}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GarmentFactoryFileImplement", "..\GarmentFactoryFileImplement\GarmentFactoryFileImplement.csproj", "{93AAEAF2-0725-44DE-9EC9-72701506D1F4}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GarmentFactoryFileImplement", "..\GarmentFactoryFileImplement\GarmentFactoryFileImplement.csproj", "{93AAEAF2-0725-44DE-9EC9-72701506D1F4}"
|
||||
ProjectSection(ProjectDependencies) = postProject
|
||||
{1B91BCBE-E608-4D05-A14D-51B1765D5203} = {1B91BCBE-E608-4D05-A14D-51B1765D5203}
|
||||
{DB84150A-FB0D-4FCB-90DD-2CAC6845E1AF} = {DB84150A-FB0D-4FCB-90DD-2CAC6845E1AF}
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GarmentFactoryDatabaseImplement", "..\GarmentFactoryDatabaseImplement\GarmentFactoryDatabaseImplement.csproj", "{AF7C72AA-02C7-4B8F-9182-D6192E2C3D42}"
|
||||
ProjectSection(ProjectDependencies) = postProject
|
||||
{1B91BCBE-E608-4D05-A14D-51B1765D5203} = {1B91BCBE-E608-4D05-A14D-51B1765D5203}
|
||||
{DB84150A-FB0D-4FCB-90DD-2CAC6845E1AF} = {DB84150A-FB0D-4FCB-90DD-2CAC6845E1AF}
|
||||
@ -49,6 +55,10 @@ Global
|
||||
{93AAEAF2-0725-44DE-9EC9-72701506D1F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{93AAEAF2-0725-44DE-9EC9-72701506D1F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{93AAEAF2-0725-44DE-9EC9-72701506D1F4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{AF7C72AA-02C7-4B8F-9182-D6192E2C3D42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{AF7C72AA-02C7-4B8F-9182-D6192E2C3D42}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{AF7C72AA-02C7-4B8F-9182-D6192E2C3D42}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{AF7C72AA-02C7-4B8F-9182-D6192E2C3D42}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -9,11 +9,16 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.17">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\GarmentFactoryBusinessLogic\GarmentFactoryBusinessLogic.csproj" />
|
||||
<ProjectReference Include="..\GarmentFactoryDatabaseImplement\GarmentFactoryDatabaseImplement.csproj" />
|
||||
<ProjectReference Include="..\GarmentFactoryFileImplement\GarmentFactoryFileImplement.csproj" />
|
||||
<ProjectReference Include="..\GarmentFactoryListImplement\GarmentFactoryListImplement.csproj" />
|
||||
</ItemGroup>
|
||||
|
@ -1,7 +1,7 @@
|
||||
using GarmentFactoryBusinessLogic;
|
||||
using GarmentFactoryContracts.BusinessLogicsContracts;
|
||||
using GarmentFactoryContracts.StoragesContracts;
|
||||
using GarmentFactoryFileImplement.Implements;
|
||||
using GarmentFactoryDatabaseImplement.Implements;
|
||||
using GarmentFactoryView;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
35
GarmentFactoryDatabaseImplement/GarmentFactoryDatabase.cs
Normal file
35
GarmentFactoryDatabaseImplement/GarmentFactoryDatabase.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using GarmentFactoryDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GarmentFactoryDatabaseImplement
|
||||
{
|
||||
public class GarmentFactoryDatabase : DbContext
|
||||
{
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (optionsBuilder.IsConfigured == false)
|
||||
{
|
||||
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-1I7RVNF\SQLEXPRESS;Initial Catalog=GarmentFactoryDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
|
||||
}
|
||||
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
|
||||
public virtual DbSet<Component> Components { set; get; }
|
||||
|
||||
public virtual DbSet<Textile> Textiles { set; get; }
|
||||
|
||||
public virtual DbSet<TextileComponent> TextileComponents { set; get; }
|
||||
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
|
||||
public virtual DbSet<Shop> Shops { get; set; }
|
||||
|
||||
public virtual DbSet<ShopTextile> ShopTextiles { get; set; }
|
||||
}
|
||||
}
|
@ -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.17" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.17" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.17">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\GarmentFactoryContracts\GarmentFactoryContracts.csproj" />
|
||||
<ProjectReference Include="..\GarmentFactoryDataModels\GarmentFactoryDataModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,85 @@
|
||||
using GarmentFactoryContracts.BindingModels;
|
||||
using GarmentFactoryContracts.SearchModels;
|
||||
using GarmentFactoryContracts.StoragesContracts;
|
||||
using GarmentFactoryContracts.ViewModels;
|
||||
using GarmentFactoryDatabaseImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GarmentFactoryDatabaseImplement.Implements
|
||||
{
|
||||
public class ComponentStorage : IComponentStorage
|
||||
{
|
||||
public List<ComponentViewModel> GetFullList()
|
||||
{
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
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 GarmentFactoryDatabase();
|
||||
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 GarmentFactoryDatabase();
|
||||
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 GarmentFactoryDatabase();
|
||||
context.Components.Add(newComponent);
|
||||
context.SaveChanges();
|
||||
return newComponent.GetViewModel;
|
||||
}
|
||||
|
||||
public ComponentViewModel? Update(ComponentBindingModel model)
|
||||
{
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
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 GarmentFactoryDatabase();
|
||||
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Components.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
97
GarmentFactoryDatabaseImplement/Implements/OrderStorage.cs
Normal file
97
GarmentFactoryDatabaseImplement/Implements/OrderStorage.cs
Normal file
@ -0,0 +1,97 @@
|
||||
using GarmentFactoryContracts.BindingModels;
|
||||
using GarmentFactoryContracts.SearchModels;
|
||||
using GarmentFactoryContracts.StoragesContracts;
|
||||
using GarmentFactoryContracts.ViewModels;
|
||||
using GarmentFactoryDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GarmentFactoryDatabaseImplement.Implements
|
||||
{
|
||||
public class OrderStorage : IOrderStorage
|
||||
{
|
||||
public List<OrderViewModel> GetFullList()
|
||||
{
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
|
||||
return context.Orders.Include(x => x.Textile).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
|
||||
return context.Orders
|
||||
.Include(x => x.Textile)
|
||||
.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 GarmentFactoryDatabase();
|
||||
|
||||
return context.Orders.Include(x => x.Textile).FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
||||
}
|
||||
|
||||
public OrderViewModel? Insert(OrderBindingModel model)
|
||||
{
|
||||
var newOrder = Order.Create(model);
|
||||
|
||||
if (newOrder == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
context.Orders.Add(newOrder);
|
||||
context.SaveChanges();
|
||||
|
||||
return context.Orders.Include(x => x.Textile).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel;
|
||||
}
|
||||
|
||||
public OrderViewModel? Update(OrderBindingModel model)
|
||||
{
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
|
||||
|
||||
if (order == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
order.Update(model);
|
||||
context.SaveChanges();
|
||||
|
||||
return context.Orders.Include(x => x.Textile).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
|
||||
}
|
||||
|
||||
public OrderViewModel? Delete(OrderBindingModel model)
|
||||
{
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
|
||||
if (element != null)
|
||||
{
|
||||
context.Orders.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
243
GarmentFactoryDatabaseImplement/Implements/ShopStorage.cs
Normal file
243
GarmentFactoryDatabaseImplement/Implements/ShopStorage.cs
Normal file
@ -0,0 +1,243 @@
|
||||
using GarmentFactoryContracts.SearchModels;
|
||||
using GarmentFactoryContracts.StoragesContracts;
|
||||
using GarmentFactoryContracts.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using GarmentFactoryContracts.BindingModels;
|
||||
using GarmentFactoryDatabaseImplement.Models;
|
||||
|
||||
namespace GarmentFactoryDatabaseImplement.Implements
|
||||
{
|
||||
public class ShopStorage : IShopStorage
|
||||
{
|
||||
public List<ShopViewModel> GetFullList()
|
||||
{
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.Textiles)
|
||||
.ThenInclude(x => x.Textile)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ShopName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
|
||||
return context.Shops
|
||||
.Include(x => x.Textiles)
|
||||
.ThenInclude(x => x.Textile)
|
||||
.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 GarmentFactoryDatabase();
|
||||
|
||||
return context.Shops
|
||||
.Include(x => x.Textiles)
|
||||
.ThenInclude(x => x.Textile)
|
||||
.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 GarmentFactoryDatabase();
|
||||
|
||||
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 GarmentFactoryDatabase();
|
||||
|
||||
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.UpdateTextiles(context, model);
|
||||
|
||||
transaction.Commit();
|
||||
return shop.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public ShopViewModel? Delete(ShopBindingModel model)
|
||||
{
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
var element = context.Shops
|
||||
.Include(x => x.Textiles)
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Shops.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
//продажа текстиля из магазинов
|
||||
public bool Sell(SupplySearchModel model)
|
||||
{
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
|
||||
try
|
||||
{
|
||||
//поиск всех магаз-ов, в кот. есть нужный текстиль (сортировка: сначала те, где его больше)
|
||||
var shopsWithThisTextile = context.Shops
|
||||
.Include(x => x.Textiles)
|
||||
.ThenInclude(x => x.Textile)
|
||||
.ToList()
|
||||
.Where(x => x.ShopTextiles.ContainsKey(model.TextileId.Value))
|
||||
.OrderByDescending(x => x.ShopTextiles[model.TextileId.Value].Item2)
|
||||
.ToList();
|
||||
|
||||
foreach (var shop in shopsWithThisTextile)
|
||||
{
|
||||
//сколько надо будет этого текстиля, если продать все из текущего магазина
|
||||
int thisTextileNeeded = model.Count.Value - shop.ShopTextiles[model.TextileId.Value].Item2;
|
||||
|
||||
//если ещё надо будет
|
||||
if (thisTextileNeeded > 0)
|
||||
{
|
||||
//полное удаление этого текстиля из этого магазина
|
||||
shop.ShopTextiles.Remove(model.TextileId.Value);
|
||||
shop.DictionaryTextilesUpdate(context);
|
||||
context.SaveChanges();
|
||||
|
||||
model.Count = thisTextileNeeded;
|
||||
}
|
||||
else
|
||||
{
|
||||
//если в этом магазине ровно столько, сколько надо
|
||||
if (thisTextileNeeded == 0)
|
||||
{
|
||||
shop.ShopTextiles.Remove(model.TextileId.Value);
|
||||
}
|
||||
//если в этом магазине больше, чем надо
|
||||
else
|
||||
{
|
||||
//уменьшение в этом магазине этого текстиля на нужное кол-во
|
||||
var textileAndCount = shop.ShopTextiles[model.TextileId.Value];
|
||||
textileAndCount.Item2 = -thisTextileNeeded;
|
||||
shop.ShopTextiles[model.TextileId.Value] = textileAndCount;
|
||||
}
|
||||
shop.DictionaryTextilesUpdate(context);
|
||||
transaction.Commit();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
//пополнение магазинов текстилем
|
||||
public bool RestockShops(SupplyBindingModel model)
|
||||
{
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
|
||||
//все магазины, где ещё есть свободное место (занятое кол-во < максимально допустимого)
|
||||
var shops = context.Shops
|
||||
.Include(x => x.Textiles)
|
||||
.ThenInclude(x => x.Textile)
|
||||
.ToList()
|
||||
.Where(x => x.ShopTextiles.Select(x => x.Value.Item2).Sum() < x.TextileMaxCount)
|
||||
.ToList();
|
||||
|
||||
try
|
||||
{
|
||||
foreach (Shop shop in shops)
|
||||
{
|
||||
//свободное место в этом магазине
|
||||
int freeSpace = shop.TextileMaxCount - shop.ShopTextiles.Select(x => x.Value.Item2).Sum();
|
||||
|
||||
//кол-во кот. надо для этого магазина (либо до макс. кол-ва, либо кол-во, кот. осталось в "поставке" (что меньше))
|
||||
int restock = Math.Min(freeSpace, model.Count);
|
||||
model.Count -= restock;
|
||||
|
||||
//если в этом магазине есть этот текстиль, то изменение его кол-ва
|
||||
if (shop.ShopTextiles.ContainsKey(model.TextileId))
|
||||
{
|
||||
var textileAndCount = shop.ShopTextiles[model.TextileId];
|
||||
textileAndCount.Item2 += restock;
|
||||
shop.ShopTextiles[model.TextileId] = textileAndCount;
|
||||
}
|
||||
//если нет, то добавление в этом кол-ве
|
||||
else
|
||||
{
|
||||
var textile = context.Textiles.First(x => x.Id == model.TextileId);
|
||||
shop.ShopTextiles.Add(model.TextileId, (textile, restock));
|
||||
}
|
||||
|
||||
shop.DictionaryTextilesUpdate(context);
|
||||
|
||||
//если в "поставке" закончились товары, то всё ок
|
||||
if (model.Count == 0)
|
||||
{
|
||||
transaction.Commit();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
109
GarmentFactoryDatabaseImplement/Implements/TextileStorage.cs
Normal file
109
GarmentFactoryDatabaseImplement/Implements/TextileStorage.cs
Normal file
@ -0,0 +1,109 @@
|
||||
using GarmentFactoryContracts.BindingModels;
|
||||
using GarmentFactoryContracts.SearchModels;
|
||||
using GarmentFactoryContracts.StoragesContracts;
|
||||
using GarmentFactoryContracts.ViewModels;
|
||||
using GarmentFactoryDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GarmentFactoryDatabaseImplement.Implements
|
||||
{
|
||||
public class TextileStorage : ITextileStorage
|
||||
{
|
||||
public List<TextileViewModel> GetFullList()
|
||||
{
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
return context.Textiles
|
||||
.Include(x => x.Components)
|
||||
.ThenInclude(x => x.Component)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<TextileViewModel> GetFilteredList(TextileSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.TextileName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
return context.Textiles
|
||||
.Include(x => x.Components)
|
||||
.ThenInclude(x => x.Component)
|
||||
.Where(x => x.TextileName.Contains(model.TextileName))
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public TextileViewModel? GetElement(TextileSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.TextileName) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
return context.Textiles
|
||||
.Include(x => x.Components)
|
||||
.ThenInclude(x => x.Component)
|
||||
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.TextileName) && x.TextileName == model.TextileName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
||||
}
|
||||
|
||||
public TextileViewModel? Insert(TextileBindingModel model)
|
||||
{
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
var newTextile = Textile.Create(context, model);
|
||||
if (newTextile == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Textiles.Add(newTextile);
|
||||
context.SaveChanges();
|
||||
return newTextile.GetViewModel;
|
||||
}
|
||||
|
||||
public TextileViewModel? Update(TextileBindingModel model)
|
||||
{
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var textile = context.Textiles.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (textile == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
textile.Update(model);
|
||||
context.SaveChanges();
|
||||
textile.UpdateComponents(context, model);
|
||||
transaction.Commit();
|
||||
return textile.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public TextileViewModel? Delete(TextileBindingModel model)
|
||||
{
|
||||
using var context = new GarmentFactoryDatabase();
|
||||
var element = context.Textiles
|
||||
.Include(x => x.Components)
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Textiles.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
171
GarmentFactoryDatabaseImplement/Migrations/20240316194255_InitialCreate.Designer.cs
generated
Normal file
171
GarmentFactoryDatabaseImplement/Migrations/20240316194255_InitialCreate.Designer.cs
generated
Normal file
@ -0,0 +1,171 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GarmentFactoryDatabaseImplement;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GarmentFactoryDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(GarmentFactoryDatabase))]
|
||||
[Migration("20240316194255_InitialCreate")]
|
||||
partial class InitialCreate
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "7.0.17")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.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("GarmentFactoryDatabaseImplement.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?>("DateComplete")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<DateTime>("DateCreate")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.Property<int>("TextileId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("TextileId");
|
||||
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Textile", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<double>("Price")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.Property<string>("TextileName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Textiles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.TextileComponent", 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>("TextileId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ComponentId");
|
||||
|
||||
b.HasIndex("TextileId");
|
||||
|
||||
b.ToTable("TextileComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("GarmentFactoryDatabaseImplement.Models.Textile", "Textile")
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("TextileId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Textile");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.TextileComponent", b =>
|
||||
{
|
||||
b.HasOne("GarmentFactoryDatabaseImplement.Models.Component", "Component")
|
||||
.WithMany("ProductComponents")
|
||||
.HasForeignKey("ComponentId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("GarmentFactoryDatabaseImplement.Models.Textile", "Textile")
|
||||
.WithMany("Components")
|
||||
.HasForeignKey("TextileId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Component");
|
||||
|
||||
b.Navigation("Textile");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("ProductComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Textile", b =>
|
||||
{
|
||||
b.Navigation("Components");
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GarmentFactoryDatabaseImplement.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: "Textiles",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
TextileName = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
Price = table.Column<double>(type: "float", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Textiles", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Orders",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
TextileId = 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),
|
||||
DateComplete = table.Column<DateTime>(type: "datetime2", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Orders", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_Orders_Textiles_TextileId",
|
||||
column: x => x.TextileId,
|
||||
principalTable: "Textiles",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "TextileComponents",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
TextileId = 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_TextileComponents", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_TextileComponents_Components_ComponentId",
|
||||
column: x => x.ComponentId,
|
||||
principalTable: "Components",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_TextileComponents_Textiles_TextileId",
|
||||
column: x => x.TextileId,
|
||||
principalTable: "Textiles",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Orders_TextileId",
|
||||
table: "Orders",
|
||||
column: "TextileId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_TextileComponents_ComponentId",
|
||||
table: "TextileComponents",
|
||||
column: "ComponentId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_TextileComponents_TextileId",
|
||||
table: "TextileComponents",
|
||||
column: "TextileId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "Orders");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "TextileComponents");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Components");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Textiles");
|
||||
}
|
||||
}
|
||||
}
|
171
GarmentFactoryDatabaseImplement/Migrations/20240320054624_Changed.Designer.cs
generated
Normal file
171
GarmentFactoryDatabaseImplement/Migrations/20240320054624_Changed.Designer.cs
generated
Normal file
@ -0,0 +1,171 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GarmentFactoryDatabaseImplement;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GarmentFactoryDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(GarmentFactoryDatabase))]
|
||||
[Migration("20240320054624_Changed")]
|
||||
partial class Changed
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "7.0.17")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.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("GarmentFactoryDatabaseImplement.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?>("DateComplete")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<DateTime>("DateCreate")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.Property<int>("TextileId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("TextileId");
|
||||
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Textile", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<double>("Price")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.Property<string>("TextileName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Textiles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.TextileComponent", 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>("TextileId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ComponentId");
|
||||
|
||||
b.HasIndex("TextileId");
|
||||
|
||||
b.ToTable("TextileComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("GarmentFactoryDatabaseImplement.Models.Textile", "Textile")
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("TextileId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Textile");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.TextileComponent", b =>
|
||||
{
|
||||
b.HasOne("GarmentFactoryDatabaseImplement.Models.Component", "Component")
|
||||
.WithMany("TextileComponents")
|
||||
.HasForeignKey("ComponentId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("GarmentFactoryDatabaseImplement.Models.Textile", "Textile")
|
||||
.WithMany("Components")
|
||||
.HasForeignKey("TextileId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Component");
|
||||
|
||||
b.Navigation("Textile");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("TextileComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Textile", b =>
|
||||
{
|
||||
b.Navigation("Components");
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GarmentFactoryDatabaseImplement.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Changed : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
248
GarmentFactoryDatabaseImplement/Migrations/20240515060701_Lab3_Hard_AddedShops.Designer.cs
generated
Normal file
248
GarmentFactoryDatabaseImplement/Migrations/20240515060701_Lab3_Hard_AddedShops.Designer.cs
generated
Normal file
@ -0,0 +1,248 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GarmentFactoryDatabaseImplement;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GarmentFactoryDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(GarmentFactoryDatabase))]
|
||||
[Migration("20240515060701_Lab3_Hard_AddedShops")]
|
||||
partial class Lab3_Hard_AddedShops
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "7.0.17")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.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("GarmentFactoryDatabaseImplement.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?>("DateComplete")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<DateTime>("DateCreate")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.Property<int>("TextileId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("TextileId");
|
||||
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Shop", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("Address")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<DateTime>("DateOpen")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("ShopName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<int>("TextileMaxCount")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Shops");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.ShopTextile", 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>("TextileId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ShopId");
|
||||
|
||||
b.HasIndex("TextileId");
|
||||
|
||||
b.ToTable("ShopTextiles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Textile", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<double>("Price")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.Property<string>("TextileName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Textiles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.TextileComponent", 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>("TextileId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ComponentId");
|
||||
|
||||
b.HasIndex("TextileId");
|
||||
|
||||
b.ToTable("TextileComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("GarmentFactoryDatabaseImplement.Models.Textile", "Textile")
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("TextileId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Textile");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.ShopTextile", b =>
|
||||
{
|
||||
b.HasOne("GarmentFactoryDatabaseImplement.Models.Shop", "Shop")
|
||||
.WithMany("Textiles")
|
||||
.HasForeignKey("ShopId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("GarmentFactoryDatabaseImplement.Models.Textile", "Textile")
|
||||
.WithMany()
|
||||
.HasForeignKey("TextileId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Shop");
|
||||
|
||||
b.Navigation("Textile");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.TextileComponent", b =>
|
||||
{
|
||||
b.HasOne("GarmentFactoryDatabaseImplement.Models.Component", "Component")
|
||||
.WithMany("TextileComponents")
|
||||
.HasForeignKey("ComponentId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("GarmentFactoryDatabaseImplement.Models.Textile", "Textile")
|
||||
.WithMany("Components")
|
||||
.HasForeignKey("TextileId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Component");
|
||||
|
||||
b.Navigation("Textile");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("TextileComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Shop", b =>
|
||||
{
|
||||
b.Navigation("Textiles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Textile", b =>
|
||||
{
|
||||
b.Navigation("Components");
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GarmentFactoryDatabaseImplement.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Lab3_Hard_AddedShops : 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"),
|
||||
ShopName = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
Address = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
DateOpen = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||
TextileMaxCount = table.Column<int>(type: "int", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Shops", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "ShopTextiles",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
TextileId = 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_ShopTextiles", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_ShopTextiles_Shops_ShopId",
|
||||
column: x => x.ShopId,
|
||||
principalTable: "Shops",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_ShopTextiles_Textiles_TextileId",
|
||||
column: x => x.TextileId,
|
||||
principalTable: "Textiles",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ShopTextiles_ShopId",
|
||||
table: "ShopTextiles",
|
||||
column: "ShopId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ShopTextiles_TextileId",
|
||||
table: "ShopTextiles",
|
||||
column: "TextileId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "ShopTextiles");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Shops");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,245 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GarmentFactoryDatabaseImplement;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GarmentFactoryDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(GarmentFactoryDatabase))]
|
||||
partial class GarmentFactoryDatabaseModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "7.0.17")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.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("GarmentFactoryDatabaseImplement.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?>("DateComplete")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<DateTime>("DateCreate")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.Property<int>("TextileId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("TextileId");
|
||||
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Shop", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("Address")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<DateTime>("DateOpen")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("ShopName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<int>("TextileMaxCount")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Shops");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.ShopTextile", 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>("TextileId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ShopId");
|
||||
|
||||
b.HasIndex("TextileId");
|
||||
|
||||
b.ToTable("ShopTextiles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Textile", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<double>("Price")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.Property<string>("TextileName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Textiles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.TextileComponent", 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>("TextileId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ComponentId");
|
||||
|
||||
b.HasIndex("TextileId");
|
||||
|
||||
b.ToTable("TextileComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("GarmentFactoryDatabaseImplement.Models.Textile", "Textile")
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("TextileId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Textile");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.ShopTextile", b =>
|
||||
{
|
||||
b.HasOne("GarmentFactoryDatabaseImplement.Models.Shop", "Shop")
|
||||
.WithMany("Textiles")
|
||||
.HasForeignKey("ShopId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("GarmentFactoryDatabaseImplement.Models.Textile", "Textile")
|
||||
.WithMany()
|
||||
|
||||
.HasForeignKey("TextileId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Shop");
|
||||
|
||||
b.Navigation("Textile");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.TextileComponent", b =>
|
||||
{
|
||||
b.HasOne("GarmentFactoryDatabaseImplement.Models.Component", "Component")
|
||||
.WithMany("TextileComponents")
|
||||
.HasForeignKey("ComponentId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("GarmentFactoryDatabaseImplement.Models.Textile", "Textile")
|
||||
.WithMany("Components")
|
||||
.HasForeignKey("TextileId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Component");
|
||||
|
||||
b.Navigation("Textile");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("TextileComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Shop", b =>
|
||||
{
|
||||
b.Navigation("Textiles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GarmentFactoryDatabaseImplement.Models.Textile", b =>
|
||||
{
|
||||
b.Navigation("Components");
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
62
GarmentFactoryDatabaseImplement/Models/Component.cs
Normal file
62
GarmentFactoryDatabaseImplement/Models/Component.cs
Normal file
@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using GarmentFactoryContracts.BindingModels;
|
||||
using GarmentFactoryContracts.ViewModels;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using GarmentFactoryDataModels.Models;
|
||||
|
||||
namespace GarmentFactoryDatabaseImplement.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<TextileComponent> TextileComponents { 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
|
||||
};
|
||||
}
|
||||
}
|
80
GarmentFactoryDatabaseImplement/Models/Order.cs
Normal file
80
GarmentFactoryDatabaseImplement/Models/Order.cs
Normal file
@ -0,0 +1,80 @@
|
||||
using GarmentFactoryContracts.BindingModels;
|
||||
using GarmentFactoryContracts.ViewModels;
|
||||
using GarmentFactoryDataModels.Enums;
|
||||
using GarmentFactoryDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GarmentFactoryDatabaseImplement.Models
|
||||
{
|
||||
public class Order : IOrderModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public int TextileId { get; private set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; 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? DateComplete { get; private set; }
|
||||
|
||||
// Для передачи названия изделия
|
||||
public virtual Textile Textile { get; set; }
|
||||
|
||||
public static Order? Create(OrderBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Order()
|
||||
{
|
||||
Id = model.Id,
|
||||
TextileId = model.TextileId,
|
||||
Count = model.Count,
|
||||
Sum = model.Sum,
|
||||
Status = model.Status,
|
||||
DateCreate = model.DateCreate,
|
||||
DateComplete = model.DateComplete,
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(OrderBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Status = model.Status;
|
||||
DateComplete = model.DateComplete;
|
||||
}
|
||||
|
||||
public OrderViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
TextileId = TextileId,
|
||||
Count = Count,
|
||||
Sum = Sum,
|
||||
Status = Status,
|
||||
DateCreate = DateCreate,
|
||||
DateComplete = DateComplete,
|
||||
TextileName = Textile.TextileName
|
||||
};
|
||||
}
|
||||
}
|
136
GarmentFactoryDatabaseImplement/Models/Shop.cs
Normal file
136
GarmentFactoryDatabaseImplement/Models/Shop.cs
Normal file
@ -0,0 +1,136 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using GarmentFactoryDataModels.Models;
|
||||
using GarmentFactoryContracts.BindingModels;
|
||||
using GarmentFactoryContracts.ViewModels;
|
||||
|
||||
namespace GarmentFactoryDatabaseImplement.Models
|
||||
{
|
||||
public class Shop : IShopModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public string ShopName { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public string Address { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public DateTime DateOpen { get; set; }
|
||||
|
||||
[Required]
|
||||
public int TextileMaxCount { get; set; }
|
||||
|
||||
[ForeignKey("ShopId")]
|
||||
public List<ShopTextile> Textiles { get; set; } = new();
|
||||
|
||||
private Dictionary<int, (ITextileModel, int)>? _shopTextiles = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (ITextileModel, int)> ShopTextiles
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_shopTextiles == null)
|
||||
{
|
||||
if (_shopTextiles == null)
|
||||
{
|
||||
_shopTextiles = Textiles.ToDictionary(x => x.TextileId, y => (y.Textile as ITextileModel, y.Count));
|
||||
}
|
||||
return _shopTextiles;
|
||||
}
|
||||
|
||||
return _shopTextiles;
|
||||
}
|
||||
}
|
||||
|
||||
public static Shop Create(GarmentFactoryDatabase context, ShopBindingModel model)
|
||||
{
|
||||
return new Shop()
|
||||
{
|
||||
Id = model.Id,
|
||||
ShopName = model.ShopName,
|
||||
Address = model.Address,
|
||||
DateOpen = model.DateOpen,
|
||||
TextileMaxCount = model.TextileMaxCount,
|
||||
Textiles = model.ShopTextiles.Select(x => new ShopTextile
|
||||
{
|
||||
Textile = context.Textiles.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList(),
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(ShopBindingModel model)
|
||||
{
|
||||
ShopName = model.ShopName;
|
||||
Address = model.Address;
|
||||
DateOpen = model.DateOpen;
|
||||
TextileMaxCount = model.TextileMaxCount;
|
||||
}
|
||||
|
||||
public ShopViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ShopName = ShopName,
|
||||
Address = Address,
|
||||
DateOpen = DateOpen,
|
||||
ShopTextiles = ShopTextiles,
|
||||
TextileMaxCount = TextileMaxCount
|
||||
};
|
||||
|
||||
public void UpdateTextiles(GarmentFactoryDatabase context, ShopBindingModel model)
|
||||
{
|
||||
var ShopTextiles = context.ShopTextiles
|
||||
.Where(rec => rec.ShopId == model.Id)
|
||||
.ToList();
|
||||
|
||||
if (ShopTextiles != null && ShopTextiles.Count > 0)
|
||||
{
|
||||
context.ShopTextiles.RemoveRange(ShopTextiles.Where(rec => !model.ShopTextiles.ContainsKey(rec.TextileId)));
|
||||
context.SaveChanges();
|
||||
|
||||
ShopTextiles = context.ShopTextiles.Where(rec => rec.ShopId == model.Id).ToList();
|
||||
|
||||
foreach (var textileToUpdate in ShopTextiles)
|
||||
{
|
||||
textileToUpdate.Count = model.ShopTextiles[textileToUpdate.TextileId].Item2;
|
||||
model.ShopTextiles.Remove(textileToUpdate.TextileId);
|
||||
}
|
||||
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
var Shop = context.Shops.First(x => x.Id == Id);
|
||||
|
||||
foreach (var ShopTextile in model.ShopTextiles)
|
||||
{
|
||||
context.ShopTextiles.Add(new ShopTextile
|
||||
{
|
||||
Shop = Shop,
|
||||
Textile = context.Textiles.First(x => x.Id == ShopTextile.Key),
|
||||
Count = ShopTextile.Value.Item2
|
||||
});
|
||||
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
_shopTextiles = null;
|
||||
}
|
||||
|
||||
public void DictionaryTextilesUpdate(GarmentFactoryDatabase context)
|
||||
{
|
||||
UpdateTextiles(context, new ShopBindingModel
|
||||
{
|
||||
Id = Id,
|
||||
ShopTextiles = ShopTextiles
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
27
GarmentFactoryDatabaseImplement/Models/ShopTextile.cs
Normal file
27
GarmentFactoryDatabaseImplement/Models/ShopTextile.cs
Normal 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 GarmentFactoryDatabaseImplement.Models
|
||||
{
|
||||
public class ShopTextile
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public int TextileId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ShopId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
|
||||
public virtual Shop Shop { get; set; } = new();
|
||||
|
||||
public virtual Textile Textile { get; set; } = new();
|
||||
}
|
||||
}
|
108
GarmentFactoryDatabaseImplement/Models/Textile.cs
Normal file
108
GarmentFactoryDatabaseImplement/Models/Textile.cs
Normal file
@ -0,0 +1,108 @@
|
||||
using GarmentFactoryContracts.BindingModels;
|
||||
using GarmentFactoryContracts.ViewModels;
|
||||
using GarmentFactoryDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GarmentFactoryDatabaseImplement.Models
|
||||
{
|
||||
public class Textile : ITextileModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public string TextileName { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public double Price { get; set; }
|
||||
|
||||
private Dictionary<int, (IComponentModel, int)>? _textileComponents = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IComponentModel, int)> TextileComponents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_textileComponents == null)
|
||||
{
|
||||
_textileComponents = Components
|
||||
.ToDictionary(recPC => recPC.ComponentId, recPC =>
|
||||
(recPC.Component as IComponentModel, recPC.Count));
|
||||
}
|
||||
return _textileComponents;
|
||||
}
|
||||
}
|
||||
|
||||
[ForeignKey("TextileId")]
|
||||
public virtual List<TextileComponent> Components { get; set; } = new();
|
||||
|
||||
[ForeignKey("TextileId")]
|
||||
public virtual List<Order> Orders { get; set; } = new();
|
||||
|
||||
public static Textile Create(GarmentFactoryDatabase context, TextileBindingModel model)
|
||||
{
|
||||
return new Textile()
|
||||
{
|
||||
Id = model.Id,
|
||||
TextileName = model.TextileName,
|
||||
Price = model.Price,
|
||||
Components = model.TextileComponents.Select(x => new TextileComponent
|
||||
{
|
||||
Component = context.Components.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(TextileBindingModel model)
|
||||
{
|
||||
TextileName = model.TextileName;
|
||||
Price = model.Price;
|
||||
}
|
||||
|
||||
public TextileViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
TextileName = TextileName,
|
||||
Price = Price,
|
||||
TextileComponents = TextileComponents
|
||||
};
|
||||
|
||||
public void UpdateComponents(GarmentFactoryDatabase context, TextileBindingModel model)
|
||||
{
|
||||
var textileComponents = context.TextileComponents.Where(rec =>rec.TextileId == model.Id).ToList();
|
||||
if (textileComponents != null && textileComponents.Count > 0)
|
||||
{
|
||||
// удалили те, которых нет в модели
|
||||
context.TextileComponents.RemoveRange(textileComponents.Where(rec => !model.TextileComponents.ContainsKey(rec.ComponentId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateComponent in textileComponents)
|
||||
{
|
||||
updateComponent.Count =
|
||||
model.TextileComponents[updateComponent.ComponentId].Item2;
|
||||
model.TextileComponents.Remove(updateComponent.ComponentId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var textile = context.Textiles.First(x => x.Id == Id);
|
||||
foreach (var pc in model.TextileComponents)
|
||||
{
|
||||
context.TextileComponents.Add(new TextileComponent
|
||||
{
|
||||
Textile = textile,
|
||||
Component = context.Components.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_textileComponents = null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
22
GarmentFactoryDatabaseImplement/Models/TextileComponent.cs
Normal file
22
GarmentFactoryDatabaseImplement/Models/TextileComponent.cs
Normal 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 GarmentFactoryDatabaseImplement.Models
|
||||
{
|
||||
public class TextileComponent
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int TextileId { get; set; }
|
||||
[Required]
|
||||
public int ComponentId { get; set; }
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
public virtual Component Component { get; set; } = new();
|
||||
public virtual Textile Textile { get; set; } = new();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user
Связь настроена не до конца