2023-04-07 20:46:13 +04:00
|
|
|
|
using ComputerStoreContracts.BindingModels;
|
|
|
|
|
using ComputerStoreContracts.ViewModels;
|
|
|
|
|
using ComputerStoreDataModels.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-04-09 01:07:38 +04:00
|
|
|
|
using System.Runtime.ConstrainedExecution;
|
2023-04-07 20:46:13 +04:00
|
|
|
|
|
|
|
|
|
namespace ComputerStoreDatabaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Consignment : IConsignmentModel
|
|
|
|
|
{
|
|
|
|
|
public int ID { get; private set; }
|
|
|
|
|
|
2023-05-20 05:52:38 +04:00
|
|
|
|
public int? OrderID { get; private set; }
|
2023-05-21 02:44:53 +04:00
|
|
|
|
public virtual Order? Order { get; set; }
|
2023-04-07 20:46:13 +04:00
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public double Price { get; private set; }
|
|
|
|
|
|
|
|
|
|
private Dictionary<int, (IProductModel, int)>? _consignmentProducts = null;
|
|
|
|
|
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, (IProductModel, int)> ConsignmentProducts
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_consignmentProducts == null)
|
|
|
|
|
{
|
|
|
|
|
_consignmentProducts = Products.ToDictionary(recPC => recPC.ProductID, recPC => (recPC.Product as IProductModel, recPC.Count));
|
|
|
|
|
}
|
|
|
|
|
return _consignmentProducts;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ForeignKey("ConsignmentID")]
|
2023-04-09 01:07:38 +04:00
|
|
|
|
public virtual List<ConsignmentProduct> Products { get; set; } = new();
|
2023-04-07 20:46:13 +04:00
|
|
|
|
|
2023-04-09 01:07:38 +04:00
|
|
|
|
public static Consignment? Create(ComputerStoreDatabase context, ConsignmentBindingModel model)
|
2023-04-07 20:46:13 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-04-09 01:07:38 +04:00
|
|
|
|
var products = context.Products;
|
2023-04-07 20:46:13 +04:00
|
|
|
|
return new Consignment()
|
|
|
|
|
{
|
|
|
|
|
ID = model.ID,
|
|
|
|
|
OrderID = model.OrderID,
|
|
|
|
|
Price = model.Price,
|
2023-04-09 01:07:38 +04:00
|
|
|
|
Products = model.ConsignmentProducts.Select(x => new ConsignmentProduct
|
|
|
|
|
{
|
|
|
|
|
Product = context.Products.First(y => y.ID == x.Key),
|
|
|
|
|
Count = x.Value.Item2
|
|
|
|
|
}).ToList()
|
2023-04-07 20:46:13 +04:00
|
|
|
|
};
|
|
|
|
|
}
|
2023-04-09 01:07:38 +04:00
|
|
|
|
|
|
|
|
|
public void Update(ConsignmentBindingModel model)
|
2023-04-07 20:46:13 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Price = model.Price;
|
2023-05-20 05:52:38 +04:00
|
|
|
|
OrderID = model.OrderID;
|
2023-04-07 20:46:13 +04:00
|
|
|
|
}
|
|
|
|
|
public ConsignmentViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
ID = ID,
|
|
|
|
|
OrderID = OrderID,
|
2023-04-09 01:07:38 +04:00
|
|
|
|
Price = Price,
|
|
|
|
|
ConsignmentProducts = ConsignmentProducts
|
2023-04-07 20:46:13 +04:00
|
|
|
|
};
|
2023-04-09 01:07:38 +04:00
|
|
|
|
|
|
|
|
|
public void UpdateProducts(ComputerStoreDatabase context, ConsignmentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var consignmentProducts = context.ConsignmentProducts.Where(rec => rec.ConsignmentID == model.ID).ToList();
|
|
|
|
|
if (consignmentProducts != null && consignmentProducts.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
context.ConsignmentProducts.RemoveRange(consignmentProducts.Where(rec => !model.ConsignmentProducts.ContainsKey(rec.ProductID)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
foreach (var updateProduct in consignmentProducts)
|
|
|
|
|
{
|
2023-06-20 21:41:33 +04:00
|
|
|
|
updateProduct.Count = model.ConsignmentProducts[updateProduct.ProductID].Item2;
|
|
|
|
|
model.ConsignmentProducts.Remove(updateProduct.ProductID);
|
2023-04-09 01:07:38 +04:00
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var consignment = context.Consignments.First(x => x.ID == ID);
|
|
|
|
|
foreach (var pc in model.ConsignmentProducts)
|
|
|
|
|
{
|
|
|
|
|
context.ConsignmentProducts.Add(new ConsignmentProduct
|
|
|
|
|
{
|
|
|
|
|
Consignment = consignment,
|
|
|
|
|
Product = context.Products.First(x => x.ID == pc.Key),
|
|
|
|
|
Count = pc.Value.Item2
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_consignmentProducts = null;
|
|
|
|
|
}
|
2023-04-07 20:46:13 +04:00
|
|
|
|
}
|
|
|
|
|
}
|