ComputerHardwareStore_YouAr.../ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs
2024-05-01 12:36:17 +04:00

152 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();
}
}
}