PIbd-21_Zaharchenko_M.I._Pi.../Pizzeria/PizzeriaDatabaseImplement/Models/Shop.cs

121 lines
3.9 KiB
C#
Raw Normal View History

2023-04-06 02:15:40 +04:00
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; }
2023-04-06 04:11:31 +04:00
public int PizzaMaxCount { get; set; }
2023-04-06 02:15:40 +04:00
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(),
2023-04-06 04:11:31 +04:00
PizzaMaxCount = model.PizzaMaxCount
2023-04-06 02:15:40 +04:00
};
}
public void Update(ShopBindingModel model)
{
ShopName = model.ShopName;
Adress = model.Adress;
OpeningDate = model.OpeningDate;
2023-04-06 04:11:31 +04:00
PizzaMaxCount = model.PizzaMaxCount;
2023-04-06 02:15:40 +04:00
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Adress = Adress,
OpeningDate = OpeningDate,
2023-04-06 04:11:31 +04:00
ShopPizzas = ShopPizzas,
PizzaMaxCount = PizzaMaxCount
2023-04-06 02:15:40 +04:00
};
2023-04-06 04:11:31 +04:00
public void UpdatePizzas(PizzeriaDatabase context, ShopBindingModel model)
2023-04-06 02:15:40 +04:00
{
2023-04-06 04:11:31 +04:00
var ShopPizzas = context.ShopPizzas.Where(rec => rec.ShopId == model.Id).ToList();
if (ShopPizzas != null && ShopPizzas.Count > 0)
2023-04-06 02:15:40 +04:00
{
2023-04-06 04:11:31 +04:00
context.ShopPizzas.RemoveRange(ShopPizzas.Where(rec => !model.ShopPizzas.ContainsKey(rec.PizzaId)));
2023-04-06 02:15:40 +04:00
context.SaveChanges();
2023-04-06 04:11:31 +04:00
ShopPizzas = context.ShopPizzas.Where(rec => rec.ShopId == model.Id).ToList();
foreach (var updatePizza in ShopPizzas)
2023-04-06 02:15:40 +04:00
{
2023-04-06 04:11:31 +04:00
updatePizza.Count = model.ShopPizzas[updatePizza.PizzaId].Item2;
model.ShopPizzas.Remove(updatePizza.PizzaId);
2023-04-06 02:15:40 +04:00
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
2023-04-06 04:11:31 +04:00
foreach (var ar in model.ShopPizzas)
2023-04-06 02:15:40 +04:00
{
2023-04-06 04:11:31 +04:00
context.ShopPizzas.Add(new ShopPizzas
2023-04-06 02:15:40 +04:00
{
Shop = shop,
2023-04-06 04:11:31 +04:00
Pizza = context.Pizzas.First(x => x.Id == ar.Key),
2023-04-06 02:15:40 +04:00
Count = ar.Value.Item2
});
context.SaveChanges();
}
2023-04-06 04:11:31 +04:00
_shopPizzas = null;
2023-04-06 02:15:40 +04:00
}
2023-04-06 04:11:31 +04:00
public void PizzasDictionatyUpdate(PizzeriaDatabase context)
2023-04-06 02:15:40 +04:00
{
2023-04-06 04:11:31 +04:00
UpdatePizzas(context, new ShopBindingModel
2023-04-06 02:15:40 +04:00
{
Id = Id,
2023-04-06 04:11:31 +04:00
ShopPizzas = ShopPizzas,
2023-04-06 02:15:40 +04:00
});
}
}
}