112 lines
3.7 KiB
C#
112 lines
3.7 KiB
C#
|
using CarRepairShopContracts.BindingModels;
|
|||
|
using CarRepairShopContracts.ViewModels;
|
|||
|
using CarRepairShopDataModels;
|
|||
|
using System.ComponentModel.DataAnnotations.Schema;
|
|||
|
using System.ComponentModel.DataAnnotations;
|
|||
|
using CarRepairShopDataModels.Models;
|
|||
|
|
|||
|
namespace CarRepairShopDatabaseImplement.Models
|
|||
|
{
|
|||
|
public class Shop : IShopModel
|
|||
|
{
|
|||
|
public int Id { get; private set; }
|
|||
|
[Required]
|
|||
|
public string ShopName { get; private set; }
|
|||
|
[Required]
|
|||
|
public string Address { get; private set; }
|
|||
|
[Required]
|
|||
|
public DateTime DateOpen { get; private set; }
|
|||
|
[Required]
|
|||
|
public int MaxCapacity { get; private 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(RepairsShopDatabase context, ShopBindingModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
return null;
|
|||
|
return new Shop()
|
|||
|
{
|
|||
|
Id = model.Id,
|
|||
|
ShopName = model.ShopName,
|
|||
|
Address = model.Address,
|
|||
|
DateOpen = model.DateOpen,
|
|||
|
MaxCapacity = model.MaxCapacity,
|
|||
|
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)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
ShopName = model.ShopName;
|
|||
|
Address = model.Address;
|
|||
|
DateOpen = model.DateOpen;
|
|||
|
MaxCapacity = model.MaxCapacity;
|
|||
|
}
|
|||
|
|
|||
|
public ShopViewModel GetViewModel => new()
|
|||
|
{
|
|||
|
Id = Id,
|
|||
|
ShopName = ShopName,
|
|||
|
Address = Address,
|
|||
|
DateOpen = DateOpen,
|
|||
|
ShopRepairs = ShopRepairs,
|
|||
|
MaxCapacity = MaxCapacity
|
|||
|
};
|
|||
|
|
|||
|
public void UpdateRepairs(RepairsShopDatabase 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.ShopId)));
|
|||
|
|
|||
|
context.SaveChanges();
|
|||
|
foreach (var updateRepair in shopRepairs)
|
|||
|
{
|
|||
|
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 pc in model.ShopRepairs)
|
|||
|
{
|
|||
|
context.ShopRepairs.Add(new ShopRepair
|
|||
|
{
|
|||
|
Shop = shop,
|
|||
|
Repair = context.Repairs.First(x => x.Id == pc.Key),
|
|||
|
Count = pc.Value.Item2
|
|||
|
});
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
_shopRepairs = null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|