Merge pull request 'BACKDEV-408' (#8) from BACKDEV-408 into dev

Reviewed-on: #8
This commit is contained in:
bekodeg 2024-05-01 12:40:04 +04:00
commit 80f5b2fde7
44 changed files with 1611 additions and 28 deletions

View File

@ -9,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreDataMo
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreContracts", "ComputerHardwareStoreContracts\ComputerHardwareStoreContracts.csproj", "{5F394E21-2597-432B-AE73-BBAFD8D9F50E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputerHardwareStoreBusinessLogic", "ComputerHardwareStoreBusinessLogic\ComputerHardwareStoreBusinessLogic.csproj", "{D32DEB60-AF40-46AF-8914-DC6A19BD66CD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreBusinessLogic", "ComputerHardwareStoreBusinessLogic\ComputerHardwareStoreBusinessLogic.csproj", "{D32DEB60-AF40-46AF-8914-DC6A19BD66CD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreDatabaseImplement", "ComputerHardwareStoreDatabaseImplement\ComputerHardwareStoreDatabaseImplement.csproj", "{93BD4E28-48D8-4D3A-87FB-FB96F00DA64B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -6,6 +6,12 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Remove="BusinessLogic\**" />
<EmbeddedResource Remove="BusinessLogic\**" />
<None Remove="BusinessLogic\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
</ItemGroup>

View File

@ -7,7 +7,8 @@ namespace ComputerHardwareStoreContracts.BindingModels
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public double Price { get; set; }
public int VendorId { get; set; }
public IVendorModel Vendor { get; set; }
public Dictionary<int, (IComponentModel, int)> BuildComponents { get; set; } = new();
public List<ICommentModel> Comments { get; set; } = new();
}
}

View File

@ -7,6 +7,6 @@ namespace ComputerHardwareStoreContracts.BindingModels
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public double Cost { get; set; }
public int StoreKeeperId { get; set; }
public IStoreKeeperModel StoreKeeper { get; set; }
}
}

View File

@ -10,6 +10,6 @@ namespace ComputerHardwareStoreContracts.BindingModels
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
public DateTime DateCreate { get; set; } = DateTime.Now;
public DateTime? DateImplement { get; set; } = null;
public Dictionary<int, (IProductModel, int)> OrderProduct { get; set; } = new();
public Dictionary<int, (IProductModel, int)> OrderProducts { get; set; } = new();
}
}

View File

@ -8,7 +8,8 @@ namespace ComputerHardwareStoreContracts.BindingModels
public int VendorId { get; set; }
public double Cost { get; set; }
public DateTime DateCreate { get; set; }
public Dictionary<int, (IBuildModel, int)> PurchaseBuild { get; set; } = new();
public Dictionary<int, (IProductModel, int)> PurchaseProduct { get; set; } = new();
public DateTime? DateImplement { get; set; }
public Dictionary<int, (IBuildModel, int)> PurchaseBuilds { get; set; } = new();
public Dictionary<int, (IProductModel, int)> PurchaseProducts { get; set; } = new();
}
}

View File

@ -3,7 +3,7 @@
public class BuildSearchModel
{
public int? Id { get; set; }
public int VendorId { get; set; }
public string? Name { get; set; }
public int? VendorId { get; set; }
}
}

View File

@ -3,6 +3,5 @@
public class CommentSearchModel
{
public int? Id { get; set; }
public DateTime? Date { get; set; }
}
}

View File

@ -4,6 +4,7 @@
{
public int? Id { get; set; }
public string? Name { get; set; }
public int? StoreKeeperId { get; set; }
public int VendorId { get; set; }
public List<int>? Builds { get; set; }
}
}

View File

@ -4,6 +4,5 @@
{
public int? Id { get; set; }
public string? Login { get; set; }
public string? Password { get; set; }
}
}

View File

@ -7,10 +7,11 @@ namespace ComputerHardwareStoreContracts.ViewModels
{
public int Id { get; set; }
[DisplayName("Название сборки")]
public string Name { get; } = string.Empty;
public string Name { get; set; } = string.Empty;
[DisplayName("Стоимость")]
public double Price { get; }
public int VendorId { get; }
public Dictionary<int, (IComponentModel, int)> BuildComponents { get; } = new();
public double Price { get; set; }
public IVendorModel Vendor { get; set; }
public Dictionary<int, (IComponentModel, int)> BuildComponents { get; set; } = new();
public List<ICommentModel> Comments { get; set; } = new();
}
}

View File

@ -7,6 +7,5 @@ namespace ComputerHardwareStoreContracts.ViewModels
public int Id { get; set; }
public DateTime Date { get; set; }
public string Text { get; set; } = string.Empty;
public int BuildId { get; set; }
}
}

View File

@ -10,6 +10,6 @@ namespace ComputerHardwareStoreContracts.ViewModels
public string Name { get; set; } = string.Empty;
[DisplayName("Цена")]
public double Cost { get; set; }
public int StoreKeeperId { get; set; }
public IStoreKeeperModel StoreKeeper { get; set; }
}
}

View File

@ -16,6 +16,6 @@ namespace ComputerHardwareStoreContracts.ViewModels
public DateTime DateCreate { get; set; }
[DisplayName("Дата выполнения")]
public DateTime? DateImplement { get; set; }
public Dictionary<int, (IProductModel, int)> OrderProduct { get; set; } = new();
public Dictionary<int, (IProductModel, int)> OrderProducts { get; set; } = new();
}
}

View File

@ -10,8 +10,6 @@ namespace ComputerHardwareStoreContracts.ViewModels
public string Name { get; set; } = string.Empty;
[DisplayName("Цена")]
public double Price { get; set; }
public Dictionary<int, (IComponentModel, int)> CannedComponents { get; set; } = new();
public Dictionary<int, (IComponentModel, int)> ProductComponents => throw new NotImplementedException();
public Dictionary<int, (IComponentModel, int)> ProductComponents { get; set; } = new();
}
}

View File

@ -10,11 +10,12 @@ namespace ComputerHardwareStoreContracts.ViewModels
public double Cost { get; set; }
[DisplayName("Дата создания")]
public DateTime DateCreate { get; set; }
public DateTime? DateImplement { get; set; }
public int VendorId { get; set; }
[DisplayName("Сумма")]
public double Sum { get; set; }
public Dictionary<int, (IBuildModel, int)> PurchaseBuild { get; set; } = new();
public Dictionary<int, (IBuildModel, int)> PurchaseBuilds { get; set; } = new();
public Dictionary<int, (IProductModel, int)> PurchaseProduct { get; set; } = new();
public Dictionary<int, (IProductModel, int)> PurchaseProducts { get; set; } = new();
}
}

View File

@ -4,7 +4,8 @@
{
string Name { get; }
double Price { get; }
int VendorId { get; }
IVendorModel Vendor { get; }
public Dictionary<int, (IComponentModel, int)> BuildComponents { get; }
public List<ICommentModel> Comments { get; }
}
}

View File

@ -4,6 +4,5 @@
{
DateTime Date { get; }
string Text { get; }
int BuildId { get; }
}
}

View File

@ -4,6 +4,6 @@
{
string Name { get; }
double Cost { get; }
int StoreKeeperId { get; }
IStoreKeeperModel StoreKeeper { get; }
}
}

View File

@ -8,6 +8,6 @@ namespace ComputerHardwareStoreDataModels.Models
OrderStatus Status { get; }
DateTime DateCreate { get; }
DateTime? DateImplement { get; }
public Dictionary<int, (IProductModel, int)> OrderProduct { get; }
public Dictionary<int, (IProductModel, int)> OrderProducts { get; }
}
}

View File

@ -5,7 +5,8 @@
int VendorId { get; }
double Cost { get; }
DateTime DateCreate { get; }
public Dictionary<int, (IBuildModel, int)> PurchaseBuild { get; }
public Dictionary<int, (IProductModel, int)> PurchaseProduct { get; }
DateTime? DateImplement { get; }
public Dictionary<int, (IBuildModel, int)> PurchaseBuilds { get; }
public Dictionary<int, (IProductModel, int)> PurchaseProducts { get; }
}
}

View File

@ -0,0 +1,44 @@
using ComputerHardwareStoreDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ComputerHardwareStoreDatabaseImplement
{
public class ComputerHardwareStoreDBContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
/*
* в program добавить:
* // получаем строку подключения из файла конфигурации
* string connection = builder.Configuration.GetConnectionString("DefaultConnection");
*
* // добавляем контекст ApplicationContext в качестве сервиса в приложение
* builder.Services.AddDbContext<ApplicationContext>(options => options.UseSqlServer(connection));
*
* в appsettings:
* "ConnectionStrings": {
* "DefaultConnection": "Host=localhost;Database=ProductBar_db;Username=compstore;Password=compstore"
* },
*/
base.OnConfiguring(optionsBuilder);
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Product> Products { set; get; }
public virtual DbSet<ProductComponent> ProductComponents { set; get; }
public virtual DbSet<StoreKeeper> StoreKeepers { set; get; }
public virtual DbSet<Build> Builds { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<OrderProduct> OrderProducts { set; get; }
public virtual DbSet<BuildComponent> BuildComponents { set; get; }
public virtual DbSet<Comment> Comments { set; get; }
public virtual DbSet<Vendor> Vendors { set; get; }
public virtual DbSet<Purchase> Purchases { set; get; }
public virtual DbSet<PurchaseBuild> PurchaseBuilds { set; get; }
public virtual DbSet<PurchaseProduct> PurchaseProducts { set; get; }
public virtual DbSet<Vendor> Vendors { set; get; }
}
}

View File

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ComputerHardwareStoreContracts\ComputerHardwareStoreContracts.csproj" />
<ProjectReference Include="..\ComputerHardwareStoreDataModels\ComputerHardwareStoreDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,117 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ComputerHardwareStoreDatabaseImplement.Implements
{
public class BuildStorage : IBuildStorage
{
public List<BuildViewModel> GetFullList()
{
using var context = new ComputerHardwareStoreDBContext();
return context.Builds
.Include(b => b.Comments)
.Include(b => b.Components)
.ThenInclude(b => b.Component)
.Include(b => b.Vendor)
.Select(b => b.GetViewModel)
.ToList();
}
public List<BuildViewModel> GetFilteredList(BuildSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new ComputerHardwareStoreDBContext();
return context.Builds
.Include(b => b.Vendor)
.Where(b => b.Vendor.Id == model.VendorId )
.Where(b => b.Name.Contains(model.Name))
.Include(b => b.Comments)
.Include(b => b.Components)
.ThenInclude(b => b.Component)
.Select(b => b.GetViewModel)
.ToList();
}
public BuildViewModel? GetElement(BuildSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new ComputerHardwareStoreDBContext();
return context.Builds
.Include(b => b.Vendor)
.Where(b => b.Vendor.Id == model.VendorId)
.Include(b => b.Comments)
.Include(b => b.Components)
.ThenInclude(b => b.Component)
.FirstOrDefault(b =>
(!string.IsNullOrEmpty(model.Name) && b.Name == model.Name) ||
(model.Id.HasValue && b.Id == model.Id))?
.GetViewModel;
}
public BuildViewModel? Insert(BuildBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var newBuild = Build.Create(context, model);
if (newBuild == null)
{
return null;
}
context.Builds.Add(newBuild);
context.SaveChanges();
return context.Builds
.Include(b => b.Vendor)
.Include(b => b.Comments)
.Include(b => b.Components)
.ThenInclude(b => b.Component)
.FirstOrDefault(p => p.Id == newBuild.Id)?
.GetViewModel;
}
public BuildViewModel? Update(BuildBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var build = context.Builds
.Include(b => b.Comments)
.Include(b => b.Components)
.ThenInclude(b => b.Component)
.Include(b => b.Vendor)
.Where(b => b.Vendor.Id == model.Vendor.Id)
.FirstOrDefault(p => p.Id == model.Id);
if (build == null)
{
return null;
}
context.Update(model);
context.SaveChanges();
return build.GetViewModel;
}
public BuildViewModel? Delete(BuildBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var build = context.Builds
.Include(b => b.Comments)
.Include(b => b.Components)
.ThenInclude(b => b.Component)
.Include(b => b.Vendor)
.Where(b => b.Vendor.Id == model.Vendor.Id)
.FirstOrDefault(p => p.Id == model.Id);
if (build == null)
{
return null;
}
context.Builds.Remove(build);
context.SaveChanges();
return build.GetViewModel;
}
}
}

View File

@ -0,0 +1,77 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDatabaseImplement.Models;
namespace ComputerHardwareStoreDatabaseImplement.Implements
{
public class CommentStorage : ICommentStorage
{
public List<CommentViewModel> GetFullList()
{
using var context = new ComputerHardwareStoreDBContext();
return context.Comments
.Select(x => x.GetViewModel)
.ToList();
}
public List<CommentViewModel> GetFilteredList(CommentSearchModel model)
{
using var context = new ComputerHardwareStoreDBContext();
return context.Comments
.Select(x => x.GetViewModel)
.ToList();
}
public CommentViewModel? GetElement(CommentSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new ComputerHardwareStoreDBContext();
return context.Comments
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public CommentViewModel? Insert(CommentBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var newComment = Comment.Create(model);
if (newComment == null)
{
return null;
}
context.Comments.Add(newComment);
context.SaveChanges();
return newComment.GetViewModel;
}
public CommentViewModel? Update(CommentBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var element = context.Comments.FirstOrDefault(x => x.Id == model.Id);
if (element == null)
{
return null;
}
element.Update(model);
context.SaveChanges();
return element.GetViewModel;
}
public CommentViewModel? Delete(CommentBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var element = context.Comments.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Comments.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

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

View File

@ -0,0 +1,108 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ComputerHardwareStoreDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel> GetFullList()
{
using var context = new ComputerHardwareStoreDBContext();
return context.Orders
.Include(o => o.Products)
.ThenInclude(o => o.Product)
.Select(o => o.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
using var context = new ComputerHardwareStoreDBContext();
return context.Orders
.Include(o => o.Products)
.ThenInclude(o => o.Product)
.Where(o =>
(model.Id.HasValue && o.Id == model.Id) ||
((model.DateFrom.HasValue && model.DateTo.HasValue) &&
((model.DateFrom <= o.DateCreate && o.DateCreate <= model.DateTo) ||
(o.DateImplement.HasValue && o.DateCreate < model.DateFrom && model.DateFrom < o.DateImplement)))
)
.Select(o => o.GetViewModel)
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new ComputerHardwareStoreDBContext();
return context.Orders
.Include(o => o.Products)
.ThenInclude(o => o.Product)
.FirstOrDefault(o => o.Id == model.Id)?
.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
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 ComputerHardwareStoreDBContext();
using var transaction = context.Database.BeginTransaction();
try
{
var order = context.Orders
.Include(o => o.Products)
.ThenInclude(o => o.Product)
.FirstOrDefault(o => o.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
transaction.Commit();
return order.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var element = context.Orders
.Include(o => o.Products)
.ThenInclude(o => o.Product)
.FirstOrDefault(o => o.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,102 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ComputerHardwareStoreDatabaseImplement.Implements
{
public class ProductStorage : IProductStorage
{
public List<ProductViewModel> GetFullList()
{
using var context = new ComputerHardwareStoreDBContext();
return context.Products
.Include(p => p.Components)
.ThenInclude(p => p.Component)
.Select(p => p.GetViewModel)
.ToList();
}
public List<ProductViewModel> GetFilteredList(ProductSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new ComputerHardwareStoreDBContext();
return context.Products
.Where(p => p.Name.Contains(model.Name))
.Include(p => p.Components)
.ThenInclude(p => p.Component)
.Select(p => p.GetViewModel)
.ToList();
}
public ProductViewModel? GetElement(ProductSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new ComputerHardwareStoreDBContext();
return context.Products
.Include(p => p.Components)
.ThenInclude(p => p.Component)
.FirstOrDefault(p =>
(!string.IsNullOrEmpty(model.Name) && p.Name == model.Name) ||
(model.Id.HasValue && p.Id == model.Id))?
.GetViewModel;
}
public ProductViewModel? Insert(ProductBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var newProduct = Product.Create(context, model);
if (newProduct == null)
{
return null;
}
context.Products.Add(newProduct);
context.SaveChanges();
return context.Products
.Include(p => p.Components)
.ThenInclude(p => p.Component)
.FirstOrDefault(p => p.Id == newProduct.Id)?
.GetViewModel;
}
public ProductViewModel? Update(ProductBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var product = context.Products
.Include(p => p.Components)
.ThenInclude(p => p.Component)
.FirstOrDefault(p => p.Id == model.Id);
if (product == null)
{
return null;
}
context.Update(model);
context.SaveChanges();
return product.GetViewModel;
}
public ProductViewModel? Delete(ProductBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var product = context.Products
.Include(p => p.Components)
.ThenInclude(p => p.Component)
.FirstOrDefault(p => p.Id == model.Id);
if (product == null)
{
return null;
}
context.Products.Remove(product);
context.SaveChanges();
return product.GetViewModel;
}
}
}

View File

@ -0,0 +1,117 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ComputerHardwareStoreDatabaseImplement.Implements
{
public class PurchaseStorage : IPurchaseStorage
{
public List<PurchaseViewModel> GetFullList()
{
using var context = new ComputerHardwareStoreDBContext();
return context.Purchases
.Include(p => p.Builds)
.ThenInclude(p => p.Build)
.Include(p => p.Products)
.ThenInclude(p => p.Product)
.Select(p => p.GetViewModel)
.ToList();
}
public List<PurchaseViewModel> GetFilteredList(PurchaseSearchModel model)
{
using var context = new ComputerHardwareStoreDBContext();
return context.Purchases
.Include(p => p.Builds)
.ThenInclude(p => p.Build)
.Include(p => p.Products)
.ThenInclude(p => p.Product)
.Where(p =>
(model.Id.HasValue && p.Id == model.Id) ||
((model.DateFrom.HasValue && model.DateTo.HasValue) &&
((model.DateFrom <= p.DateCreate && p.DateCreate <= model.DateTo) ||
(p.DateImplement.HasValue && p.DateCreate < model.DateFrom && model.DateFrom < p.DateImplement)))
)
.Select(p => p.GetViewModel)
.ToList();
}
public PurchaseViewModel? GetElement(PurchaseSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new ComputerHardwareStoreDBContext();
return context.Purchases
.Include(p => p.Builds)
.ThenInclude(p => p.Build)
.Include(p => p.Products)
.ThenInclude(p => p.Product)
.FirstOrDefault(p => p.Id == model.Id)?
.GetViewModel;
}
public PurchaseViewModel? Insert(PurchaseBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var newPurchase = Purchase.Create(context, model);
if (newPurchase == null)
{
return null;
}
context.Purchases.Add(newPurchase);
context.SaveChanges();
return newPurchase.GetViewModel;
}
public PurchaseViewModel? Update(PurchaseBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
using var transaction = context.Database.BeginTransaction();
try
{
var purchase = context.Purchases
.Include(p => p.Builds)
.ThenInclude(p => p.Build)
.Include(p => p.Products)
.ThenInclude(p => p.Product)
.FirstOrDefault(p => p.Id == model.Id);
if (purchase == null)
{
return null;
}
purchase.Update(model);
context.SaveChanges();
transaction.Commit();
return purchase.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public PurchaseViewModel? Delete(PurchaseBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var element = context.Purchases
.Include(p => p.Builds)
.ThenInclude(p => p.Build)
.Include(p => p.Products)
.ThenInclude(p => p.Product)
.FirstOrDefault(o => o.Id == model.Id);
if (element != null)
{
context.Purchases.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,86 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ComputerHardwareStoreDatabaseImplement.Implements
{
public class StoreKeeperStorage : IStoreKeeperStorage
{
public StoreKeeperViewModel? GetElement(StoreKeeperSearchModel model)
{
if (string.IsNullOrEmpty(model.Login) && !model.Id.HasValue)
{
return null;
}
using var context = new ComputerHardwareStoreDBContext();
return context.StoreKeepers
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login)
&& x.Login == model.Login)
|| (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public List<StoreKeeperViewModel> GetFilteredList(StoreKeeperSearchModel model)
{
if (string.IsNullOrEmpty(model.Login))
{
return new();
}
using var context = new ComputerHardwareStoreDBContext();
return context.StoreKeepers
.Where(x => x.Login.Contains(model.Login))
.Select(x => x.GetViewModel)
.ToList();
}
public List<StoreKeeperViewModel> GetFullList()
{
using var context = new ComputerHardwareStoreDBContext();
return context.StoreKeepers
.Include(x => x.Components)
.Select(x => x.GetViewModel)
.ToList();
}
public StoreKeeperViewModel? Insert(StoreKeeperBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var newStoreKeeper = StoreKeeper.Create(context, model);
if (newStoreKeeper == null)
{
return null;
}
context.StoreKeepers.Add(newStoreKeeper);
context.SaveChanges();
return newStoreKeeper.GetViewModel;
}
public StoreKeeperViewModel? Update(StoreKeeperBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var element = context.StoreKeepers.FirstOrDefault(x => x.Id == model.Id);
if (element == null)
{
return null;
}
element.Update(model);
context.SaveChanges();
return element.GetViewModel;
}
public StoreKeeperViewModel? Delete(StoreKeeperBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var element = context.StoreKeepers.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.StoreKeepers.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,84 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDatabaseImplement.Models;
namespace ComputerHardwareStoreDatabaseImplement.Implements
{
public class VendorStorage : IVendorStorage
{
public VendorViewModel? GetElement(VendorSearchModel model)
{
if (string.IsNullOrEmpty(model.Login) && !model.Id.HasValue)
{
return null;
}
using var context = new ComputerHardwareStoreDBContext();
return context.Vendors
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login)
&& x.Login == model.Login)
|| (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public List<VendorViewModel> GetFilteredList(VendorSearchModel model)
{
if (string.IsNullOrEmpty(model.Login))
{
return new();
}
using var context = new ComputerHardwareStoreDBContext();
return context.Vendors
.Where(x => x.Login.Contains(model.Login))
.Select(x => x.GetViewModel)
.ToList();
}
public List<VendorViewModel> GetFullList()
{
using var context = new ComputerHardwareStoreDBContext();
return context.Vendors
.Select(x => x.GetViewModel)
.ToList();
}
public VendorViewModel? Insert(VendorBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var newVendor = Vendor.Create(context, model);
if (newVendor == null)
{
return null;
}
context.Vendors.Add(newVendor);
context.SaveChanges();
return newVendor.GetViewModel;
}
public VendorViewModel? Update(VendorBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var element = context.Vendors.FirstOrDefault(x => x.Id == model.Id);
if (element == null)
{
return null;
}
element.Update(model);
context.SaveChanges();
return element.GetViewModel;
}
public VendorViewModel? Delete(VendorBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var element = context.Vendors.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Vendors.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,88 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDataModels.Models;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class Build : IBuildModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
[NotMapped]
private Dictionary<int, (IComponentModel, int)>? _buildComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> BuildComponents
{
get
{
if (_buildComponents == null)
{
_buildComponents = Components
.ToDictionary(bc => bc.ComponentId, bc =>
(bc.Component as IComponentModel, bc.Count));
}
return _buildComponents;
}
}
[ForeignKey("BuildId")]
public virtual List<BuildComponent> Components { get; set; } = new();
[ForeignKey("BuildId")]
public virtual List<Comment> Comments { get; set; } = new();
[NotMapped]
List<ICommentModel> IBuildModel.Comments => Comments.Select(c => c as ICommentModel).ToList();
public virtual Vendor Vendor { get; private set; } = new();
IVendorModel IBuildModel.Vendor => Vendor as IVendorModel;
public static Build? Create(ComputerHardwareStoreDBContext context, BuildBindingModel model)
{
if (model == null)
{
return null;
}
return new()
{
Id = model.Id,
Name = model.Name,
Price = model.Price,
Vendor = context.Vendors.First(v => v.Id == model.Vendor.Id),
Components = context.Components
.Where(c => model.BuildComponents.ContainsKey(c.Id))
.Select(c => new BuildComponent()
{
BuildId = model.Id,
ComponentId = c.Id,
Component = c,
Count = model.BuildComponents[c.Id].Item2
}).ToList()
};
}
public void Update(BuildBindingModel model)
{
if (model == null)
{
return;
}
Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name;
Price = model.Price;
}
public BuildViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Price = Price,
Vendor = Vendor,
Comments = Comments.Select(c => c as ICommentModel).ToList(),
BuildComponents = BuildComponents
};
}
}

View 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 ComputerHardwareStoreDatabaseImplement.Models
{
public class BuildComponent
{
public int Id { get; set; }
[Required]
public int BuildId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Build Build { get; set; } = new();
public virtual Component Component { get; set; } = new();
}
}

View File

@ -0,0 +1,48 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class Comment : ICommentModel
{
public int Id { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
public string Text { get; set; } = string.Empty;
[Required]
public int BuildId { get; set; }
public static Comment? Create(CommentBindingModel model)
{
if (model == null)
{
return null;
}
return new()
{
Id = model.Id,
Date = model.Date,
Text = model.Text,
BuildId = model.BuildId,
};
}
public void Update(CommentBindingModel model)
{
if (model == null)
{
return;
}
Text = model.Text;
}
public CommentViewModel GetViewModel => new()
{
Id = Id,
Date = Date,
Text = Text,
};
}
}

View File

@ -0,0 +1,54 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
[Required]
public double Cost { get; set; }
[Required]
public virtual StoreKeeper? StoreKeeperЗЛ { get; set; }
[NotMapped]
public IStoreKeeperModel? StoreKeeper { get; set; }
[ForeignKey("ComponentId")]
public virtual List<ProductComponent> ProductComponents { get; set; } = new();
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
Name = model.Name,
Cost = model.Cost,
StoreKeeper = model.StoreKeeper
};
}
public void Update (ComponentBindingModel model)
{
if (model == null)
{
return;
}
Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Cost = Cost
};
}
}

View File

@ -0,0 +1,84 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDataModels.Enums;
using ComputerHardwareStoreDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
[Required]
public int Count { get; set; }
[Required]
public double Sum { get; set; }
[Required]
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
[Required]
public DateTime DateCreate { get; set; } = DateTime.Now;
public DateTime? DateImplement { get; set; }
private Dictionary<int, (IProductModel, int)>? _orderProducts = null;
[NotMapped]
public Dictionary<int, (IProductModel, int)> OrderProducts
{
get
{
if (_orderProducts == null)
{
_orderProducts = Products
.ToDictionary(
op => op.ProductId,
op => (op.Product as IProductModel, op.Count));
}
return _orderProducts;
}
}
[ForeignKey("OrderId")]
public virtual List<OrderProduct> Products { get; set; } = new();
public static Order? Create(ComputerHardwareStoreDBContext context, OrderBindingModel model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
Products = context.Products
.Where(p => model.OrderProducts.ContainsKey(p.Id))
.Select(p => new OrderProduct()
{
OrderId = model.Id,
ProductId = p.Id,
Product = p,
Count = model.OrderProducts[p.Id].Item2
})
.ToList()
};
}
public void Update(OrderBindingModel model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
}
}

View File

@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class OrderProduct
{
public int Id { get; set; }
[Required]
public int OrderId { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public int Count { get; set; }
public virtual Product Product { get; set; } = new();
public virtual Order Order { get; set; } = new();
}
}

View File

@ -0,0 +1,98 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDataModels.Models;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class Product : IProductModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _productComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> ProductComponents
{
get
{
if (_productComponents == null)
{
_productComponents = Components
.ToDictionary(c => c.ComponentId, c => (c.Component as IComponentModel, c.Count));
}
return _productComponents;
}
}
[ForeignKey("ProductId")]
public virtual List<ProductComponent> Components { get; set; } = new();
[ForeignKey("ProductId")]
public virtual List<Order> Orders { get; set; } = new();
public static Product Create(ComputerHardwareStoreDBContext context, ProductBindingModel model)
{
return new Product()
{
Id = model.Id,
Name = model.Name,
Price = model.Price,
Components = context.Components
.Where(c => model.ProductComponents.ContainsKey(c.Id))
.Select(c => new ProductComponent()
{
ProductId = model.Id,
ComponentId = c.Id,
Component = c,
Count = model.ProductComponents[c.Id].Item2
}).ToList(),
};
}
public void Update(ProductBindingModel model)
{
Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name;
Price = model.Price;
}
public ProductViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Price = Price,
ProductComponents = ProductComponents
};
public static void UpdateComponents(ComputerHardwareStoreDBContext context, ProductBindingModel model)
{
var productComponents = context.ProductComponents
.Where(pc => pc.ProductId == model.Id)
.ToList();
if (productComponents.Count != 0 && productComponents.Count > 0)
{
// удалили те, которых нет в модели
context.ProductComponents
.Where(pc => !model.ProductComponents.ContainsKey(pc.ComponentId))
.ExecuteDelete();
// обновили количество у существующих записей
productComponents
.ForEach(updateComponent =>
{
updateComponent.Count = model.ProductComponents[updateComponent.ComponentId].Item2;
model.ProductComponents.Remove(updateComponent.ComponentId);
});
}
// добавили новые
context.ProductComponents
.AddRange(model.ProductComponents.Values
.Select(val => new ProductComponent()
{
ProductId = model.Id,
ComponentId = val.Item1.Id,
Count = val.Item2
}));
context.SaveChanges();
}
}
}

View File

@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class ProductComponent
{
public int Id { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Product Product { get; set; } = new();
}
}

View File

@ -0,0 +1,159 @@

using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDataModels.Models;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class Purchase : IPurchaseModel
{
public int Id { get; private set; }
[Required]
public int VendorId { get; set; }
[Required]
public double Cost { get; set; }
[Required]
public DateTime DateCreate { get; set; } = DateTime.Now;
public DateTime? DateImplement { get; set; } = null;
private Dictionary<int, (IBuildModel, int)>? _purchaseBuilds = null;
[NotMapped]
public Dictionary<int, (IBuildModel, int)> PurchaseBuilds
{
get
{
if (_purchaseBuilds == null)
{
_purchaseBuilds = Builds
.ToDictionary(op => op.BuildId, op => (op.Build as IBuildModel, op.Count));
}
return _purchaseBuilds;
}
}
[ForeignKey("PurchaseId")]
public virtual List<PurchaseBuild> Builds { get; set; } = new();
private Dictionary<int, (IProductModel, int)>? _purchaseProducts = null;
[NotMapped]
public Dictionary<int, (IProductModel, int)> PurchaseProducts
{
get
{
if (_purchaseProducts == null)
{
_purchaseProducts = Products
.ToDictionary(op => op.ProductId, op => (op.Product as IProductModel, op.Count));
}
return _purchaseProducts;
}
}
[ForeignKey("PurchaseId")]
public virtual List<PurchaseProduct> Products { get; set; } = new();
public static Purchase Create(ComputerHardwareStoreDBContext context, PurchaseBindingModel model)
{
return new Purchase()
{
Id = model.Id,
Cost = model.Cost,
DateCreate = model.DateCreate,
Builds = context.Builds
.Where(b => model.PurchaseBuilds.ContainsKey(b.Id))
.Select(b => new PurchaseBuild()
{
PurchaseId = model.Id,
BuildId = b.Id,
Build = b,
Count = model.PurchaseProducts[b.Id].Item2
}).ToList(),
Products = context.Products
.Where(p => model.PurchaseProducts.ContainsKey(p.Id))
.Select (p => new PurchaseProduct()
{
PurchaseId = model.Id,
ProductId = p.Id,
Product = p,
Count = model.PurchaseProducts[p.Id].Item2
})
.ToList()
};
}
public void Update(PurchaseBindingModel model)
{
Cost = model.Cost;
}
public PurchaseViewModel GetViewModel => new()
{
Id = Id,
Cost = Cost,
DateCreate = DateCreate,
PurchaseBuilds = PurchaseBuilds,
PurchaseProducts = PurchaseProducts
};
public static void UpdateBuilds(ComputerHardwareStoreDBContext context, PurchaseBindingModel model)
{
var productBuilds = context.PurchaseBuilds
.Where(pc => pc.PurchaseId == model.Id)
.ToList();
if (productBuilds.Count != 0 && productBuilds.Count > 0)
{
// удалили те, которых нет в модели
context.PurchaseBuilds
.Where(pc => !model.PurchaseBuilds.ContainsKey(pc.BuildId))
.ExecuteDelete();
// обновили количество у существующих записей
productBuilds
.ForEach(updateBuild =>
{
updateBuild.Count = model.PurchaseBuilds[updateBuild.BuildId].Item2;
model.PurchaseBuilds.Remove(updateBuild.BuildId);
});
}
// добавили новые
context.PurchaseBuilds
.AddRange(model.PurchaseBuilds.Values
.Select(val => new PurchaseBuild()
{
PurchaseId = model.Id,
BuildId = val.Item1.Id,
Count = val.Item2
}));
context.SaveChanges();
}
public static void UpdateProducts(ComputerHardwareStoreDBContext context, PurchaseBindingModel model)
{
var productProducts = context.PurchaseProducts
.Where(pc => pc.PurchaseId == model.Id)
.ToList();
if (productProducts.Count != 0 && productProducts.Count > 0)
{
// удалили те, которых нет в модели
context.PurchaseProducts
.Where(pc => !model.PurchaseProducts.ContainsKey(pc.ProductId))
.ExecuteDelete();
// обновили количество у существующих записей
productProducts
.ForEach(updateProduct =>
{
updateProduct.Count = model.PurchaseProducts[updateProduct.ProductId].Item2;
model.PurchaseProducts.Remove(updateProduct.ProductId);
});
}
// добавили новые
context.PurchaseProducts
.AddRange(model.PurchaseProducts.Values
.Select(val => new PurchaseProduct()
{
PurchaseId = model.Id,
ProductId = val.Item1.Id,
Count = val.Item2
}));
context.SaveChanges();
}
}
}

View File

@ -0,0 +1,18 @@

using System.ComponentModel.DataAnnotations;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class PurchaseBuild
{
public int Id { get; set; }
[Required]
public int PurchaseId { get; set; }
[Required]
public int BuildId { get; set; }
[Required]
public int Count { get; set; }
public virtual Purchase Purchase { get; set; } = new();
public virtual Build Build { get; set; } = new();
}
}

View File

@ -0,0 +1,18 @@

using System.ComponentModel.DataAnnotations;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class PurchaseProduct
{
public int Id { get; set; }
[Required]
public int PurchaseId { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public int Count { get; set; }
public virtual Purchase Purchase { get; set; } = new();
public virtual Product Product { get; set; } = new();
}
}

View File

@ -0,0 +1,56 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class StoreKeeper : IStoreKeeperModel
{
public int Id { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
[Required]
public string Login { get; private set; } = string.Empty;
[Required]
public string Password { get; private set; } = string.Empty;
[ForeignKey("StoreKeeperId")]
public virtual List<Product> Products { get; set; } = new();
[ForeignKey("StoreKeeperId")]
public virtual List<Component> Components { get; set; } = new();
public static StoreKeeper? Create(ComputerHardwareStoreDBContext context, StoreKeeperBindingModel model)
{
if (model == null)
{
return null;
}
return new StoreKeeper()
{
Id = model.Id,
Name = model.Name,
Login = model.Login,
Password = model.Password,
};
}
public void Update(StoreKeeperBindingModel model)
{
if (model == null)
{
return;
}
Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name;
Password = string.IsNullOrEmpty(model.Password) ? Password : model.Password;
}
public StoreKeeperViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Login = Login,
Password = Password
};
}
}

View File

@ -0,0 +1,57 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
public class Vendor : IVendorModel
{
public int Id { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
[Required]
public string Login { get; private set; } = string.Empty;
[Required]
public string Password { get; private set; } = string.Empty;
[ForeignKey("VendorId")]
public virtual List<Purchase> Purchases { get; set; } = new();
[ForeignKey("VendorId")]
public virtual List<Build> Builds { get; set; } = new();
public static Vendor? Create(ComputerHardwareStoreDBContext context, VendorBindingModel model)
{
if (model == null)
{
return null;
}
return new Vendor()
{
Id = model.Id,
Name = model.Name,
Login = model.Login,
Password = model.Password,
};
}
public void Update(VendorBindingModel model)
{
if (model == null)
{
return;
}
Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name;
Password = string.IsNullOrEmpty(model.Password) ? Password : model.Password;
}
public VendorViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Login = Login,
Password = Password
};
}
}