This commit is contained in:
shadowik 2023-04-06 02:15:40 +04:00
parent 351921e1f7
commit 5c96df7985
4 changed files with 153 additions and 2 deletions

View File

@ -14,5 +14,6 @@ namespace PizzeriaDataModels.Models
string Adress { get; }
DateTime OpeningDate { get; }
Dictionary<int, (IPizzaModel, int)> ShopPizzas { get; }
public int PizzaMaxCount { get; }
}
}

View File

@ -0,0 +1,118 @@
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,
});
}
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PizzeriaDatabaseImplement.Models
{
public class ShopPizzas
{
public int Id { get; set; }
[Required]
public int PizzaId { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int Count { get; set; }
public virtual Shop Shop { get; set; } = new();
public virtual Pizza Pizza { get; set; } = new();
}
}

View File

@ -16,6 +16,7 @@ namespace PizzeriaListImplement.Models
public string Adress { get; private set; } = string.Empty;
public DateTime OpeningDate { get; private set; }
public Dictionary<int, (IPizzaModel, int)> ShopPizzas { get; private set; } = new();
public int PizzaMaxCount { get; private set; }
public static Shop? Create(ShopBindingModel? model)
{
if (model == null)
@ -27,7 +28,8 @@ namespace PizzeriaListImplement.Models
Id = model.Id,
ShopName = model.ShopName,
Adress = model.Adress,
OpeningDate = model.OpeningDate
OpeningDate = model.OpeningDate,
PizzaMaxCount = model.PizzaMaxCount,
};
}
public void Update(ShopBindingModel? model)
@ -39,6 +41,7 @@ namespace PizzeriaListImplement.Models
ShopName = model.ShopName;
Adress = model.Adress;
OpeningDate = model.OpeningDate;
PizzaMaxCount = model.PizzaMaxCount;
}
public ShopViewModel GetViewModel => new()
{
@ -46,8 +49,10 @@ namespace PizzeriaListImplement.Models
ShopName = ShopName,
Adress = Adress,
OpeningDate = OpeningDate,
ShopPizzas = ShopPizzas
ShopPizzas = ShopPizzas,
PizzaMaxCount = PizzaMaxCount,
};
}
}