2023-04-06 02:15:40 +04:00

119 lines
4.2 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 PizzeriaContracts.BindingModels;
using PizzeriaContracts.ViewModels;
using PizzeriaDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PizzeriaDatabaseImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; set; }
public string ShopName { get; set; } = String.Empty;
public string Adress { get; set; } = String.Empty;
public DateTime OpeningDate { get; set; }
private Dictionary<int, (IPizzaModel, int)>? _shopPizzas = null;
public Dictionary<int, (IPizzaModel, int)> ShopPizzas
{
get
{
if (_shopPizzas == null) {
if (_shopPizzas == null)
{
_shopPizzas = Pizzas
.ToDictionary(recSP => recSP.PizzaId, recSP => (recSP.Pizza as IPizzaModel, recSP.Count));
}
return _shopPizzas;
}
return _shopPizzas;
}
}
[ForeignKey("ShopId")]
public List<ShopPizzas> Pizzas { get; set; } = new();
public static Shop Create(PizzeriaDatabase context, ShopBindingModel model)
{
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
Adress = model.Adress,
OpeningDate = model.OpeningDate,
Pizzas = model.ShopPizzas.Select(x => new ShopPizzas
{
Pizza = context.Pizzas.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList(),
Piz = model.ReinforcedMaxCount
};
}
public void Update(ShopBindingModel model)
{
ShopName = model.ShopName;
Adress = model.Adress;
OpeningDate = model.OpeningDate;
ReinforcedMaxCount = model.ReinforcedMaxCount;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Adress = Adress,
OpeningDate = OpeningDate,
ShopReinforcedes = ShopReinforcedes,
ReinforcedMaxCount = ReinforcedMaxCount
};
public void UpdateReinforcedes(PrecastConcretePlantDatabase context, ShopBindingModel model)
{
var ShopReinforcedes = context.ShopReinforcedes.Where(rec => rec.ShopId == model.Id).ToList();
if (ShopReinforcedes != null && ShopReinforcedes.Count > 0)
{
// удалили те, которых нет в модели
context.ShopReinforcedes.RemoveRange(ShopReinforcedes.Where(rec => !model.ShopReinforcedes.ContainsKey(rec.ReinforcedId)));
context.SaveChanges();
ShopReinforcedes = context.ShopReinforcedes.Where(rec => rec.ShopId == model.Id).ToList();
// обновили количество у существующих записей
foreach (var updateReinforced in ShopReinforcedes)
{
updateReinforced.Count = model.ShopReinforcedes[updateReinforced.ReinforcedId].Item2;
model.ShopReinforcedes.Remove(updateReinforced.ReinforcedId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var ar in model.ShopReinforcedes)
{
context.ShopReinforcedes.Add(new ShopReinforced
{
Shop = shop,
Reinforced = context.Reinforceds.First(x => x.Id == ar.Key),
Count = ar.Value.Item2
});
context.SaveChanges();
}
_shopReinforcedes = null;
}
public void ReinforcedDictionatyUpdate(PrecastConcretePlantDatabase context)
{
UpdateReinforcedes(context, new ShopBindingModel
{
Id = Id,
ShopReinforcedes = ShopReinforcedes,
});
}
}
}