100 lines
3.5 KiB
C#
100 lines
3.5 KiB
C#
using AccountingWarehouseProductsContracts.BindingModels;
|
|
using AccountingWarehouseProductsContracts.ViewModels;
|
|
using AccountingWarehouseProductsDataModels.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 AccountingWarehouseProductsDatabaseImplement.Models
|
|
{
|
|
public class Warehouse
|
|
{
|
|
public int Id { get; set; }
|
|
|
|
[Required]
|
|
public string WarehouseName { get; set; } = string.Empty;
|
|
|
|
public string Address { get; set; } = string.Empty;
|
|
|
|
[Required]
|
|
public int Capacity { get; set; }
|
|
|
|
private Dictionary<int, (IProductModel, int)>? _warehouseProduct = null;
|
|
|
|
[NotMapped]
|
|
public Dictionary<int, (IProductModel, int)> WarehouseProducts
|
|
{
|
|
get
|
|
{
|
|
if (_warehouseProduct == null)
|
|
{
|
|
_warehouseProduct = Products.ToDictionary(recWP => recWP.ProductId, recWP => (recWP.Product as IProductModel, recWP.Count));
|
|
}
|
|
return _warehouseProduct;
|
|
}
|
|
}
|
|
|
|
[ForeignKey("WarehouseId")]
|
|
public virtual List<WarehouseProduct> Products { get; set; } = new();
|
|
|
|
public static Warehouse Create(AccountingWarehouseProductsDatabase context, WarehouseBindingModel model)
|
|
{
|
|
return new Warehouse()
|
|
{
|
|
Id = model.Id,
|
|
WarehouseName = model.WarehouseName,
|
|
Address = model.Address,
|
|
Capacity = model.Capacity,
|
|
Products = model.WarehouseProducts.Select(x => new WarehouseProduct
|
|
{
|
|
Product = context.Products.First(y => y.Id == x.Key),
|
|
Count = x.Value.Item2
|
|
}).ToList(),
|
|
};
|
|
}
|
|
|
|
public WarehouseViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
WarehouseName = WarehouseName,
|
|
Address = Address,
|
|
Capacity = Capacity,
|
|
WarehouseProducts = WarehouseProducts
|
|
};
|
|
|
|
public void UpdateProducts(AccountingWarehouseProductsDatabase context, WarehouseBindingModel model)
|
|
{
|
|
var warehouseProducts = context.WarehouseProducts.Where(rec => rec.WarehouseId == model.Id).ToList();
|
|
if (warehouseProducts != null && warehouseProducts.Count > 0)
|
|
{
|
|
context.WarehouseProducts.RemoveRange(warehouseProducts.Where(rec => !model.WarehouseProducts.ContainsKey(rec.ProductId)));
|
|
context.SaveChanges();
|
|
|
|
foreach (var updateProduct in warehouseProducts)
|
|
{
|
|
updateProduct.Count = model.WarehouseProducts[updateProduct.ProductId].Item2;
|
|
model.WarehouseProducts.Remove(updateProduct.ProductId);
|
|
}
|
|
context.SaveChanges();
|
|
}
|
|
|
|
var warehouse = context.Warehouses.First(x => x.Id == Id);
|
|
foreach (var rc in model.WarehouseProducts)
|
|
{
|
|
context.WarehouseProducts.Add(new WarehouseProduct
|
|
{
|
|
Warehouse = warehouse,
|
|
Product = context.Products.First(x => x.Id == rc.Key),
|
|
Count = rc.Value.Item2
|
|
});
|
|
context.SaveChanges();
|
|
}
|
|
_warehouseProduct = null;
|
|
}
|
|
}
|
|
}
|