2023-05-08 13:51:25 +04:00
|
|
|
|
using RestaurantContracts.BindingModels;
|
|
|
|
|
using RestaurantContracts.ViewModels;
|
|
|
|
|
using RestaurantDataModels.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 RestaurantDatabaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Component : IComponentModel
|
|
|
|
|
{
|
|
|
|
|
[Required]
|
|
|
|
|
public string Name { get; private set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public int Price { get; private set; }
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public int Count { get; private set; }
|
|
|
|
|
|
2023-05-16 23:26:45 +04:00
|
|
|
|
private List<(IProviderModel, int, DateTime)>? _componentProviders = null;
|
2023-05-08 13:51:25 +04:00
|
|
|
|
|
|
|
|
|
[NotMapped]
|
2023-05-16 23:26:45 +04:00
|
|
|
|
public List<(IProviderModel, int, DateTime)> ComponentProviders
|
2023-05-08 13:51:25 +04:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_componentProviders == null)
|
|
|
|
|
{
|
2023-05-16 23:26:45 +04:00
|
|
|
|
_componentProviders = Providers.Select(recPC => (recPC.Provider as IProviderModel, recPC.Count, recPC.Date)).ToList();
|
2023-05-08 13:51:25 +04:00
|
|
|
|
}
|
|
|
|
|
return _componentProviders;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ForeignKey("ComponentId")]
|
|
|
|
|
public virtual List<ComponentProvider> Providers { get; set; } = new();
|
|
|
|
|
|
|
|
|
|
public int Id { get; private set; }
|
|
|
|
|
|
|
|
|
|
[ForeignKey("ComponentId")]
|
|
|
|
|
public virtual List<ProductComponent> ProductComponents { get; set; } = new();
|
|
|
|
|
|
|
|
|
|
public static Component? Create(RestaurantDatabase context, ComponentBindingModel? model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return new Component()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Name = model.Name,
|
|
|
|
|
Price = model.Price,
|
|
|
|
|
Count = model.Count,
|
|
|
|
|
Providers = model.ComponentProviders.Select(x => new ComponentProvider
|
|
|
|
|
{
|
2023-05-16 23:26:45 +04:00
|
|
|
|
Provider = context.Providers.First(y => y.ComponentProviders == x.Item1),
|
|
|
|
|
Count = x.Item2,
|
|
|
|
|
Date = x.Item3
|
2023-05-08 13:51:25 +04:00
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(ComponentBindingModel? model)
|
|
|
|
|
{
|
|
|
|
|
Name = model.Name;
|
|
|
|
|
Price = model.Price;
|
|
|
|
|
Count = model.Count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ComponentViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
Name = Name,
|
|
|
|
|
Price = Price,
|
|
|
|
|
Count = Count,
|
|
|
|
|
ComponentProviders = ComponentProviders,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public void UpdateComponents(RestaurantDatabase context, ComponentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var ComponentProviders = context.ComponentProviders.Where(rec => rec.ComponentId == model.Id).ToList();
|
|
|
|
|
if (ComponentProviders != null && ComponentProviders.Count > 0)
|
|
|
|
|
{ // удалили те, которых нет в модели
|
2023-05-16 23:26:45 +04:00
|
|
|
|
context.ComponentProviders.RemoveRange(ComponentProviders.Where(rec => !model.ComponentProviders.Select(x => x.Item1.Id).Contains(rec.ProviderId)));
|
2023-05-08 13:51:25 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
// обновили количество у существующих записей
|
|
|
|
|
foreach (var updateComponent in ComponentProviders)
|
|
|
|
|
{
|
2023-05-16 23:26:45 +04:00
|
|
|
|
updateComponent.Count = model.ComponentProviders.Find(x => x.Item1.Id == updateComponent.ProviderId).Item2;
|
|
|
|
|
model.ComponentProviders.Remove(model.ComponentProviders.Find(x => x.Item1.Id == updateComponent.ProviderId));
|
2023-05-08 13:51:25 +04:00
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
var Component = context.Components.First(x => x.Id == Id);
|
|
|
|
|
foreach (var pc in model.ComponentProviders)
|
|
|
|
|
{
|
|
|
|
|
context.ComponentProviders.Add(new ComponentProvider
|
|
|
|
|
{
|
|
|
|
|
Component = Component,
|
2023-05-16 23:26:45 +04:00
|
|
|
|
Provider = context.Providers.First(x => x.ComponentProviders == pc.Item1),
|
|
|
|
|
Count = pc.Item2,
|
|
|
|
|
Date = pc.Item3 // ??????
|
2023-05-08 13:51:25 +04:00
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_componentProviders = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|