Purchase и связи
This commit is contained in:
parent
ed846686ce
commit
ca6035d7a1
@ -8,7 +8,7 @@ namespace ComputerHardwareStoreContracts.BindingModels
|
|||||||
public int VendorId { get; set; }
|
public int VendorId { get; set; }
|
||||||
public double Cost { get; set; }
|
public double Cost { get; set; }
|
||||||
public DateTime DateCreate { get; set; }
|
public DateTime DateCreate { 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,8 +13,8 @@ namespace ComputerHardwareStoreContracts.ViewModels
|
|||||||
public int VendorId { get; set; }
|
public int VendorId { get; set; }
|
||||||
[DisplayName("Сумма")]
|
[DisplayName("Сумма")]
|
||||||
public double Sum { get; set; }
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
int VendorId { get; }
|
int VendorId { get; }
|
||||||
double Cost { get; }
|
double Cost { get; }
|
||||||
DateTime DateCreate { get; }
|
DateTime DateCreate { get; }
|
||||||
public Dictionary<int, (IBuildModel, int)> PurchaseBuild { get; }
|
public Dictionary<int, (IBuildModel, int)> PurchaseBuilds { get; }
|
||||||
public Dictionary<int, (IProductModel, int)> PurchaseProduct { get; }
|
public Dictionary<int, (IProductModel, int)> PurchaseProducts { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,9 @@ namespace ComputerHardwareStoreDatabaseImplement
|
|||||||
public virtual DbSet<OrderProduct> OrderProducts { set; get; }
|
public virtual DbSet<OrderProduct> OrderProducts { set; get; }
|
||||||
public virtual DbSet<BuildComponent> BuildComponents { set; get; }
|
public virtual DbSet<BuildComponent> BuildComponents { set; get; }
|
||||||
public virtual DbSet<Comment> Comments { set; get; }
|
public virtual DbSet<Comment> Comments { 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; }
|
public virtual DbSet<Vendor> Vendors { set; get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,151 @@
|
|||||||
|
|
||||||
|
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;
|
||||||
|
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 = model.PurchaseBuilds.Select(x =>
|
||||||
|
new PurchaseBuild
|
||||||
|
{
|
||||||
|
Build = context.Builds.First(y => y.Id == x.Key),
|
||||||
|
Count = x.Value.Item2
|
||||||
|
}).ToList(),
|
||||||
|
Products = model.PurchaseProducts.Select(x =>
|
||||||
|
new PurchaseProduct
|
||||||
|
{
|
||||||
|
Product = context.Products.First(z => z.Id == z.Key),
|
||||||
|
Count = x.Value.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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user