119 lines
4.2 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
{
[Required]
public string Name { get; private set; } = string.Empty;
[Required]
public string Address { get; private set; } = string.Empty;
[Required]
public int ReinforcedMaxCount { get; private set; }
public DateTime DateOpening { get; private set; }
private Dictionary<int, (IReinforcedModel, int)>? _cachedReinforcedies = null;
[NotMapped]
public Dictionary<int, (IReinforcedModel, int)> Reinforcedies
{
get
{
if (_cachedReinforcedies == null)
{
using var context = new PrecastConcretePlantDataBase();
_cachedReinforcedies = ShopReinforcedies
.ToDictionary(x => x.ReinforcedId, x => (context.Reinforcedies
.FirstOrDefault(y => y.Id == x.ReinforcedId)! as IReinforcedModel, x.Count));
}
return _cachedReinforcedies;
}
}
public int Id { get; private set; }
[ForeignKey("ShopId")]
public virtual List<ShopReinforced> ShopReinforcedies { get; set; } = new();
public static Shop? Create(PrecastConcretePlantDataBase context, ShopBindingModel? model)
{
if (model == null)
{
return null;
}
return new Shop()
{
Id = model.Id,
Name = model.Name,
Address = model.Address,
DateOpening = model.DateOpening,
ReinforcedMaxCount = model.ReinforcedMaxCount,
ShopReinforcedies = model.Reinforcedies.Select(x => new ShopReinforced
{
Reinforced = context.Reinforcedies.FirstOrDefault(y => y.Id == x.Key)!,
Count = x.Value.Item2,
}).ToList()
};
}
public void Update(ShopBindingModel? model)
{
if (model == null)
{
return;
}
Name = model.Name;
Address = model.Address;
DateOpening = model.DateOpening;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Address = Address,
Reinforcedies = Reinforcedies,
DateOpening = DateOpening,
ReinforcedMaxCount = ReinforcedMaxCount,
};
public void UpdateReinforcedies(PrecastConcretePlantDataBase context, ShopBindingModel model)
{
var shopReinforcedies = context.ShopReinforcedies
.Where(rec => rec.ShopId == model.Id)
.ToList();
if (shopReinforcedies != null && shopReinforcedies.Count > 0)
{
context.ShopReinforcedies
.RemoveRange(shopReinforcedies
.Where(rec => !model.Reinforcedies
.ContainsKey(rec.ReinforcedId)));
foreach (var updateReinforced in shopReinforcedies.Where(x => model.Reinforcedies.ContainsKey(x.ReinforcedId)))
{
updateReinforced.Count = model.Reinforcedies[updateReinforced.ReinforcedId].Item2;
model.Reinforcedies.Remove(updateReinforced.ReinforcedId);
}
}
var shop = context.Shops.First(x => x.Id == model.Id);
shop.ShopReinforcedies.AddRange(model.Reinforcedies.Select(x => new ShopReinforced
{
Reinforced = context.Reinforcedies.First(y => y.Id == x.Key),
Count = x.Value.Item2,
}).Except(shopReinforcedies ?? new()));
context.SaveChanges();
_cachedReinforcedies = null;
}
}
}