113 lines
3.9 KiB
C#
113 lines
3.9 KiB
C#
using SushiBarContracts.BindingModel;
|
||
using SushiBarContracts.ViewModels;
|
||
using SushiBarDataModels;
|
||
using SushiBarDataModels.Models;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using System.ComponentModel.DataAnnotations.Schema;
|
||
using System.Diagnostics;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Xml.Linq;
|
||
|
||
namespace SushiBarDatabaseImplement.Models
|
||
{
|
||
public class Shop : IShopModel
|
||
{
|
||
public int Id { get; private set; }
|
||
|
||
[Required]
|
||
public string ShopName { get; private set; }
|
||
|
||
[Required]
|
||
public string Address { get; private set; }
|
||
|
||
[Required]
|
||
public DateTime DateOpening { get; private set; }
|
||
|
||
[Required]
|
||
public int MaxCountSushis { get; private set; }
|
||
|
||
[ForeignKey("ShopId")]
|
||
public List<ShopSushi> Sushis { get; private set; } = new();
|
||
|
||
private Dictionary<int, (ISushiModel, int)>? _shopSushis = null;
|
||
|
||
[NotMapped]
|
||
public Dictionary<int, (ISushiModel, int)> ShopSushis
|
||
{
|
||
get
|
||
{
|
||
if (_shopSushis == null)
|
||
{
|
||
_shopSushis = Sushis.ToDictionary(recPC => recPC.SushiId, recPC => (recPC.Sushi as ISushiModel, recPC.Count));
|
||
}
|
||
return _shopSushis;
|
||
}
|
||
}
|
||
|
||
public static Shop Create(SushiBarDatabase context, ShopBindingModel model)
|
||
{
|
||
return new Shop()
|
||
{
|
||
Id = model.Id,
|
||
ShopName = model.ShopName,
|
||
Address = model.Address,
|
||
MaxCountSushis = model.MaxCountSushis,
|
||
DateOpening = model.DateOpening,
|
||
Sushis = model.ShopSushis.Select(x => new ShopSushi
|
||
{
|
||
Sushi = context.Sushis.First(y => y.Id == x.Key), Count = x.Value.Item2
|
||
}).ToList()
|
||
};
|
||
}
|
||
public void Update(ShopBindingModel model)
|
||
{
|
||
ShopName = model.ShopName;
|
||
Address = model.Address;
|
||
DateOpening = model.DateOpening;
|
||
MaxCountSushis = model.MaxCountSushis;
|
||
}
|
||
public ShopViewModel GetViewModel => new()
|
||
{
|
||
Id = Id,
|
||
ShopName = ShopName,
|
||
Address = Address,
|
||
DateOpening = DateOpening,
|
||
MaxCountSushis = MaxCountSushis,
|
||
ShopSushis = ShopSushis
|
||
};
|
||
public void UpdateSushis(SushiBarDatabase context, ShopBindingModel model)
|
||
{
|
||
var ShopSushis = context.ShopSushis.Where(rec => rec.ShopId == model.Id).ToList();
|
||
if (ShopSushis != null && ShopSushis.Count > 0)
|
||
{
|
||
// удалили те, которых нет в модели
|
||
context.ShopSushis.RemoveRange(ShopSushis.Where(rec => !model.ShopSushis.ContainsKey(rec.SushiId)));
|
||
context.SaveChanges();
|
||
ShopSushis = context.ShopSushis.Where(rec => rec.ShopId == model.Id).ToList();
|
||
// обновили количество у существующих записей
|
||
foreach (var updateSushi in ShopSushis)
|
||
{
|
||
updateSushi.Count = model.ShopSushis[updateSushi.SushiId].Item2;
|
||
model.ShopSushis.Remove(updateSushi.SushiId);
|
||
}
|
||
context.SaveChanges();
|
||
}
|
||
var shop = context.Shops.First(x => x.Id == Id);
|
||
foreach (var elem in model.ShopSushis)
|
||
{
|
||
context.ShopSushis.Add(new ShopSushi
|
||
{
|
||
Shop = shop,
|
||
Sushi = context.Sushis.First(x => x.Id == elem.Key),
|
||
Count = elem.Value.Item2
|
||
});
|
||
context.SaveChanges();
|
||
}
|
||
_shopSushis = null;
|
||
}
|
||
}
|
||
} |