111 lines
3.6 KiB
C#
111 lines
3.6 KiB
C#
using CarRepairShopContracts.BindingModels;
|
|
using CarRepairShopContracts.ViewModels;
|
|
using CarRepairShopDataModels.Models;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
namespace CarRepairShopDatabaseImplement.Models
|
|
{
|
|
public class Shop : IShopModel
|
|
{
|
|
public int Id { get; set; }
|
|
|
|
[Required]
|
|
public string ShopName { get; set; } = string.Empty;
|
|
|
|
[Required]
|
|
public string Address { get; set; } = string.Empty;
|
|
|
|
[Required]
|
|
public DateTime DateOpening { get; set; }
|
|
|
|
[Required]
|
|
public int RepairMaxAmount { get; set; }
|
|
|
|
private Dictionary<int, (IRepairModel, int)>? _shopRepairs = null;
|
|
|
|
[NotMapped]
|
|
public Dictionary<int, (IRepairModel, int)> ShopRepairs
|
|
{
|
|
get
|
|
{
|
|
if (_shopRepairs == null)
|
|
{
|
|
_shopRepairs = Repairs
|
|
.ToDictionary(x => x.RepairId, x => (x.Repair as IRepairModel, x.Count));
|
|
}
|
|
return _shopRepairs;
|
|
}
|
|
}
|
|
|
|
[ForeignKey("ShopId")]
|
|
public virtual List<ShopRepair> Repairs { get; set; } = new();
|
|
|
|
public static Shop Create(CarRepairShopDatabase context, ShopBindingModel model)
|
|
{
|
|
return new Shop()
|
|
{
|
|
Id = model.Id,
|
|
ShopName = model.ShopName,
|
|
Address = model.Address,
|
|
DateOpening = model.DateOpening,
|
|
RepairMaxAmount = model.RepairMaxAmount,
|
|
Repairs = model.ShopRepairs.Select(x => new ShopRepair
|
|
{
|
|
Repair = context.Repairs.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;
|
|
RepairMaxAmount = model.RepairMaxAmount;
|
|
}
|
|
|
|
public ShopViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
ShopName = ShopName,
|
|
Address = Address,
|
|
DateOpening = DateOpening,
|
|
RepairMaxAmount = RepairMaxAmount,
|
|
ShopRepairs = ShopRepairs
|
|
};
|
|
|
|
public void UpdateRepairs(CarRepairShopDatabase context, ShopBindingModel model)
|
|
{
|
|
var shopRepairs = context.ShopRepairs.Where(rec => rec.ShopId == model.Id).ToList();
|
|
if (shopRepairs != null && shopRepairs.Count > 0)
|
|
{
|
|
context.ShopRepairs.RemoveRange(shopRepairs.Where(rec => !model.ShopRepairs.ContainsKey(rec.RepairId)));
|
|
context.SaveChanges();
|
|
foreach (var updateRepair in shopRepairs)
|
|
{
|
|
if (model.ShopRepairs.ContainsKey(updateRepair.RepairId))
|
|
{
|
|
updateRepair.Count = model.ShopRepairs[updateRepair.RepairId].Item2;
|
|
model.ShopRepairs.Remove(updateRepair.RepairId);
|
|
}
|
|
}
|
|
context.SaveChanges();
|
|
}
|
|
var shop = context.Shops.First(x => x.Id == Id);
|
|
foreach (var ic in model.ShopRepairs)
|
|
{
|
|
context.ShopRepairs.Add(new ShopRepair
|
|
{
|
|
Shop = shop,
|
|
Repair = context.Repairs.First(x => x.Id == ic.Key),
|
|
Count = ic.Value.Item2
|
|
});
|
|
context.SaveChanges();
|
|
}
|
|
_shopRepairs = null;
|
|
}
|
|
}
|
|
}
|