116 lines
3.6 KiB
C#
116 lines
3.6 KiB
C#
using GiftShopContracts.BindingModels;
|
|
using GiftShopContracts.ViewModels;
|
|
using GiftShopDataModels.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GiftShopDatabaseImplement.Models
|
|
{
|
|
public class Shop : IShopModel
|
|
{
|
|
public int Id { get; set; }
|
|
[Required]
|
|
public string ShopName { get; private set; }
|
|
[Required]
|
|
public string ShopAdress { get; private set; }
|
|
[Required]
|
|
public DateTime OpeningDate { get; private set; }
|
|
[Required]
|
|
public int MaxCapacity { get; private set; }
|
|
private Dictionary<int, (IGiftModel, int)>? _shopGifts =
|
|
null;
|
|
[NotMapped]
|
|
public Dictionary<int, (IGiftModel, int)> ShopGifts
|
|
{
|
|
get
|
|
{
|
|
if (_shopGifts == null)
|
|
{
|
|
_shopGifts = Gifts
|
|
.ToDictionary(x => x.GiftId, x =>
|
|
(x.Gift as IGiftModel, x.Count));
|
|
}
|
|
return _shopGifts;
|
|
}
|
|
}
|
|
[ForeignKey("ShopId")]
|
|
public virtual List<ShopGift> Gifts { get; set; } = new();
|
|
public static Shop? Create(GiftShopDatabase context, ShopBindingModel model)
|
|
{
|
|
if (model == null)
|
|
return null;
|
|
return new Shop()
|
|
{
|
|
Id = model.Id,
|
|
ShopName = model.ShopName,
|
|
ShopAdress = model.ShopAdress,
|
|
OpeningDate = model.OpeningDate,
|
|
MaxCapacity = model.MaxCapacity,
|
|
Gifts = model.ShopGifts.Select(x => new ShopGift
|
|
{
|
|
Gift = context.Gifts.First(y => y.Id == x.Key),
|
|
Count = x.Value.Item2
|
|
}).ToList()
|
|
};
|
|
}
|
|
|
|
public void Update(ShopBindingModel? model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return;
|
|
}
|
|
ShopName = model.ShopName;
|
|
ShopAdress = model.ShopAdress;
|
|
OpeningDate = model.OpeningDate;
|
|
MaxCapacity = model.MaxCapacity;
|
|
}
|
|
|
|
public ShopViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
ShopName = ShopName,
|
|
ShopAdress = ShopAdress,
|
|
OpeningDate = OpeningDate,
|
|
ShopGifts = ShopGifts,
|
|
MaxCapacity = MaxCapacity
|
|
};
|
|
|
|
public void UpdateGifts(GiftShopDatabase context, ShopBindingModel model)
|
|
{
|
|
var shopGifts = context.ShopGifts.Where(rec =>
|
|
rec.ShopId == model.Id).ToList();
|
|
if (shopGifts != null && shopGifts.Count > 0)
|
|
{
|
|
context.ShopGifts.RemoveRange(shopGifts.Where(rec => !model.ShopGifts.ContainsKey(rec.GiftId)));
|
|
|
|
context.SaveChanges();
|
|
foreach (var updateGift in shopGifts)
|
|
{
|
|
updateGift.Count =
|
|
model.ShopGifts[updateGift.GiftId].Item2;
|
|
model.ShopGifts.Remove(updateGift.GiftId);
|
|
}
|
|
context.SaveChanges();
|
|
}
|
|
var shop = context.Shops.First(x => x.Id == Id);
|
|
foreach (var pc in model.ShopGifts)
|
|
{
|
|
context.ShopGifts.Add(new ShopGift
|
|
{
|
|
Shop = shop,
|
|
Gift = context.Gifts.First(x => x.Id == pc.Key),
|
|
Count = pc.Value.Item2
|
|
});
|
|
context.SaveChanges();
|
|
}
|
|
_shopGifts = null;
|
|
}
|
|
}
|
|
}
|