107 lines
3.9 KiB
C#
Raw Normal View History

2023-03-28 10:53:40 +04:00
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.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 PrecastConcretePlantDatabaseImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; set; }
2023-03-28 10:53:40 +04:00
[Required]
public string Name { get; set; } = string.Empty;
2023-03-28 10:53:40 +04:00
[Required]
public string Address { get; set; } = string.Empty;
2023-03-28 10:53:40 +04:00
[Required]
public DateTime DateOpening { get; set; }
[Required]
public int ReinforcedMaxCount { get; set; }
2023-03-28 10:53:40 +04:00
private Dictionary<int, (IReinforcedModel, int)>? _shopReinforcedies = null;
2023-03-28 10:53:40 +04:00
[NotMapped]
public Dictionary<int, (IReinforcedModel, int)> ShopReinforcedies
2023-03-28 10:53:40 +04:00
{
get
{
if (_shopReinforcedies == null)
2023-03-28 10:53:40 +04:00
{
_shopReinforcedies = Reinforcedies.ToDictionary(recST => recST.ReinforcedId, recST => (recST.Reinforced as IReinforcedModel, recST.Count));
2023-03-28 10:53:40 +04:00
}
return _shopReinforcedies;
2023-03-28 10:53:40 +04:00
}
}
[ForeignKey("ShopId")]
public virtual List<ShopReinforced> Reinforcedies { get; set; } = new();
public static Shop Create(PrecastConcretePlantDataBase context, ShopBindingModel model)
2023-03-28 10:53:40 +04:00
{
return new Shop()
{
Id = model.Id,
Name = model.Name,
2023-03-28 10:53:40 +04:00
Address = model.Address,
DateOpening = model.DateOpening,
ReinforcedMaxCount = model.ReinforcedMaxCount,
Reinforcedies = model.ShopReinforcedies.Select(x => new ShopReinforced
2023-03-28 10:53:40 +04:00
{
Reinforced = context.Reinforcedies.First(y => y.Id == x.Key),
Count = x.Value.Item2
2023-03-28 10:53:40 +04:00
}).ToList()
};
}
public void Update(ShopBindingModel model)
2023-03-28 10:53:40 +04:00
{
Name = model.Name;
2023-03-28 10:53:40 +04:00
Address = model.Address;
DateOpening = model.DateOpening;
ReinforcedMaxCount = model.ReinforcedMaxCount;
2023-03-28 10:53:40 +04:00
}
2023-03-28 10:53:40 +04:00
public ShopViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
2023-03-28 10:53:40 +04:00
Address = Address,
DateOpening = DateOpening,
ReinforcedMaxCount = ReinforcedMaxCount,
ShopReinforcedies = ShopReinforcedies
2023-03-28 10:53:40 +04:00
};
public void UpdateReinforcedies(PrecastConcretePlantDataBase context, ShopBindingModel model)
{
var shopReinforcedies = context.ShopReinforcedies.Where(rec => rec.ShopId == model.Id).ToList();
2023-03-28 10:53:40 +04:00
if (shopReinforcedies != null && shopReinforcedies.Count > 0)
{
context.ShopReinforcedies.RemoveRange(shopReinforcedies.Where(rec => !model.ShopReinforcedies.ContainsKey(rec.ReinforcedId)));
context.SaveChanges();
2023-03-28 10:53:40 +04:00
foreach (var updateReinforced in shopReinforcedies)
2023-03-28 10:53:40 +04:00
{
updateReinforced.Count = model.ShopReinforcedies[updateReinforced.ReinforcedId].Item2;
model.ShopReinforcedies.Remove(updateReinforced.ReinforcedId);
2023-03-28 10:53:40 +04:00
}
context.SaveChanges();
2023-03-28 10:53:40 +04:00
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var st in model.ShopReinforcedies)
2023-03-28 10:53:40 +04:00
{
context.ShopReinforcedies.Add(new ShopReinforced
{
Shop = shop,
Reinforced = context.Reinforcedies.First(x => x.Id == st.Key),
Count = st.Value.Item2
});
context.SaveChanges();
}
_shopReinforcedies = null;
2023-03-28 10:53:40 +04:00
}
}
}