PIbd-22_Tsukanova_I.V._IceC.../IceCreamShop/IceCreamShopDatabaseImplement/Models/Shop.cs

114 lines
3.9 KiB
C#
Raw Normal View History

2023-04-09 19:24:19 +04:00
using AbstractIceCreamShopDataModels.Models;
using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.ViewModels;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Net;
namespace IceCreamShopDatabaseImplement.Models
{
public class Shop : IShopModel
{
[Required]
public string Name { get; private set; } = string.Empty;
[Required]
public string Adress { get; private set; } = string.Empty;
public DateTime OpeningDate { get; private set; }
private Dictionary<int, (IIceCreamModel, int)>? _cashedIcecreams = null;
[NotMapped]
public Dictionary<int, (IIceCreamModel, int)> ShopIceCreams
{
get
{
2023-04-25 08:54:06 +04:00
if (_cashedIcecreams == null)
2023-04-09 19:24:19 +04:00
{
_cashedIcecreams = IceCreams.ToDictionary(recPC => recPC.IceCreamId, recPC => (recPC.IceCream as IIceCreamModel, recPC.Count));
}
return _cashedIcecreams;
}
}
[Required]
public int IceCreamMaxCount { get; private set; }
public int Id { get; private set; }
[ForeignKey("ShopId")]
public virtual List<ShopIcecream> IceCreams { get; set; } = new();
public static Shop? Create(IceCreamShopDatabase context, ShopBindingModel? model)
{
if (model == null)
{
return null;
}
return new Shop()
{
Id = model.Id,
Name = model.Name,
Adress = model.Adress,
OpeningDate = model.OpeningDate,
IceCreamMaxCount = model.IceCreamMaxCount,
IceCreams = model.ShopIceCreams.Select(x => new ShopIcecream
{
2023-04-25 08:54:06 +04:00
IceCream = context.IceCreams.FirstOrDefault(y => y.Id == x.Key)!,
2023-04-09 19:24:19 +04:00
Count = x.Value.Item2,
}).ToList()
};
}
public void Update(ShopBindingModel? model)
{
if (model == null)
{
return;
}
Name = model.Name;
Adress = model.Adress;
OpeningDate = model.OpeningDate;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Adress = Adress,
ShopIceCreams = ShopIceCreams,
OpeningDate = OpeningDate,
IceCreamMaxCount = IceCreamMaxCount,
};
2023-04-19 19:52:11 +04:00
public void UpdateIceCreams(IceCreamShopDatabase context, ShopBindingModel model)
2023-04-09 19:24:19 +04:00
{
2023-04-25 08:54:06 +04:00
var shopIceCreams = context.ShopIcecreams
.Where(rec => rec.ShopId == model.Id)
.ToList();
// удалили те, которых нет в модели
if (shopIceCreams != null && shopIceCreams.Count > 0)
{
context.ShopIcecreams
2023-04-25 08:54:06 +04:00
.RemoveRange(shopIceCreams
.Where(rec => !model.ShopIceCreams
.ContainsKey(rec.IceCreamId)));
2023-04-09 19:24:19 +04:00
// обновили количество у существующих записей
2023-04-25 08:54:06 +04:00
foreach (var updateIcecream in shopIceCreams.Where(x => model.ShopIceCreams.ContainsKey(x.IceCreamId)))
2023-04-09 19:24:19 +04:00
{
2023-04-25 08:54:06 +04:00
updateIcecream.Count = model.ShopIceCreams[updateIcecream.IceCreamId].Item2;
model.ShopIceCreams.Remove(updateIcecream.IceCreamId);
2023-04-09 19:24:19 +04:00
}
}
2023-04-25 08:54:06 +04:00
var shop = context.Shops.First(x => x.Id == model.Id);
shop.IceCreams.AddRange(model.ShopIceCreams.Select(x => new ShopIcecream
2023-04-09 19:24:19 +04:00
{
2023-04-25 08:54:06 +04:00
IceCream = context.IceCreams.First(y => y.Id == x.Key),
Count = x.Value.Item2,
}).Except(shopIceCreams ?? new()));
context.SaveChanges();
2023-04-09 19:24:19 +04:00
_cashedIcecreams = null;
}
}
}