SUBD_Aleikin/Restaurant/RestaurantDatabaseImplement/Models/Component.cs

116 lines
4.1 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 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; }
private List<(IProviderModel, int, DateTime)>? _componentProviders = null;
[NotMapped]
public List<(IProviderModel, int, DateTime)> ComponentProviders
{
get
{
if (_componentProviders == null)
{
_componentProviders = Providers.Select(recPC => (recPC.Provider as IProviderModel, recPC.Count, recPC.Date)).ToList();
}
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
{
Provider = context.Providers.First(y => y.ComponentProviders == x.Item1),
Count = x.Item2,
Date = x.Item3
}).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)
{ // удалили те, которых нет в модели
context.ComponentProviders.RemoveRange(ComponentProviders.Where(rec => !model.ComponentProviders.Select(x => x.Item1.Id).Contains(rec.ProviderId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateComponent in ComponentProviders)
{
updateComponent.Count = model.ComponentProviders.Find(x => x.Item1.Id == updateComponent.ProviderId).Item2;
model.ComponentProviders.Remove(model.ComponentProviders.Find(x => x.Item1.Id == updateComponent.ProviderId));
}
context.SaveChanges();
}
var Component = context.Components.First(x => x.Id == Id);
foreach (var pc in model.ComponentProviders)
{
context.ComponentProviders.Add(new ComponentProvider
{
Component = Component,
Provider = context.Providers.First(x => x.ComponentProviders == pc.Item1),
Count = pc.Item2,
Date = pc.Item3 // ??????
});
context.SaveChanges();
}
_componentProviders = null;
}
}
}