PIbd-21_BatylkinaAO_MusoevD.../Canteen/CanteenDatabaseImplement/Models/Product.cs

108 lines
3.4 KiB
C#
Raw Normal View History

2023-04-07 10:55:40 +04:00
using CanteenContracts.BindingModels;
using CanteenContracts.View;
using CanteenDataModels.Models;
2023-04-09 00:34:25 +04:00
using FluentNHibernate.Conventions.Inspections;
using Microsoft.EntityFrameworkCore;
2023-04-07 10:55:40 +04:00
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace CanteenDatabaseImplement.Models
{
public class Product : IProductModel
{
2023-04-07 23:35:33 +04:00
public int Id { get; set; }
2023-04-07 10:55:40 +04:00
[Required]
2023-04-07 23:35:33 +04:00
public string ProductName { get; set; } = string.Empty;
2023-04-07 10:55:40 +04:00
[Required]
2023-04-07 23:35:33 +04:00
public double Price { get; set; }
2023-04-07 10:55:40 +04:00
[Required]
2023-04-07 23:35:33 +04:00
public int ManagerId { get; set; }
2023-04-07 10:55:40 +04:00
private Dictionary<int, ICookModel>? _productCooks = null;
2023-04-07 23:35:33 +04:00
2023-04-07 10:55:40 +04:00
[NotMapped]
public Dictionary<int, ICookModel> ProductCooks
{
get
{
if (_productCooks == null)
{
2023-04-07 23:35:33 +04:00
_productCooks = Cooks
.ToDictionary(recPC => recPC.CookId, recPC => (recPC.Cook as ICookModel));
2023-04-07 10:55:40 +04:00
}
return _productCooks;
}
}
2023-04-07 23:35:33 +04:00
2023-04-07 10:55:40 +04:00
[ForeignKey("ProductId")]
public virtual List<ProductCook> Cooks { get; set; } = new();
2023-04-09 00:34:25 +04:00
[ForeignKey("ProductId")]
public virtual List<LunchProduct> Lunches { get; set; } = new();
[ForeignKey("ProductId")]
public virtual List<DishProduct> Dishes { get; set; } = new();
public virtual Manager Manager { get; set; }
2023-04-07 23:35:33 +04:00
public static Product Create(CanteenDatabase context, ProductBindingModel model)
2023-04-07 10:55:40 +04:00
{
2023-04-07 23:35:33 +04:00
return new Product
2023-04-07 10:55:40 +04:00
{
Id = model.Id,
ProductName = model.ProductName,
2023-04-07 23:35:33 +04:00
Price = model.Price,
ManagerId = model.ManagerId,
2023-04-07 10:55:40 +04:00
Cooks = model.ProductCooks.Select(x => new ProductCook
{
2023-05-17 17:02:26 +04:00
Cook = context.Cooks.First(y => y.Id == x.Key)
2023-04-07 10:55:40 +04:00
}).ToList()
};
}
public void Update(ProductBindingModel model)
{
ProductName = model.ProductName;
2023-04-07 23:35:33 +04:00
Price = model.Price;
ManagerId = model.ManagerId;
2023-04-07 10:55:40 +04:00
}
public ProductViewModel GetViewModel => new()
{
Id = Id,
ProductName = ProductName,
2023-04-07 23:35:33 +04:00
Price = Price,
ManagerId = ManagerId,
ProductCooks = ProductCooks
2023-04-07 10:55:40 +04:00
};
2023-04-07 23:35:33 +04:00
public void UpdateProductCooks(CanteenDatabase context, ProductBindingModel model)
2023-04-07 10:55:40 +04:00
{
2023-04-07 23:35:33 +04:00
var productCookers = context.ProductCook.Where(rec => rec.ProductId == model.Id).ToList();
2023-05-17 17:02:26 +04:00
if (productCookers != null)
2023-04-07 10:55:40 +04:00
{
2023-04-07 23:35:33 +04:00
context.ProductCook.RemoveRange(productCookers.Where(rec => !model.ProductCooks.ContainsKey(rec.CookId)));
2023-04-07 10:55:40 +04:00
context.SaveChanges();
}
2023-04-09 00:34:25 +04:00
foreach (var updateCook in productCookers)
{
model.ProductCooks.Remove(updateCook.CookId);
}
context.SaveChanges();
2023-04-07 10:55:40 +04:00
var product = context.Products.First(x => x.Id == Id);
2023-04-07 23:35:33 +04:00
2023-04-07 10:55:40 +04:00
foreach (var pc in model.ProductCooks)
{
2023-04-07 23:35:33 +04:00
context.ProductCook.Add(new ProductCook
2023-04-07 10:55:40 +04:00
{
Product = product,
2023-04-07 23:35:33 +04:00
Cook = context.Cooks.First(x => x.Id == pc.Key)
2023-04-07 10:55:40 +04:00
});
context.SaveChanges();
}
_productCooks = null;
}
}
}