2024-03-14 18:53:30 +04:00
|
|
|
|
using CarRepairShopContracts.BindingModels;
|
|
|
|
|
using CarRepairShopContracts.ViewModels;
|
|
|
|
|
using CarRepairShopDataModels.Models;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
|
|
|
|
|
namespace CarRepairShopDatabaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Repair : IRepairModel
|
|
|
|
|
{
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
[Required]
|
|
|
|
|
public string RepairName { get; set; } = string.Empty;
|
|
|
|
|
[Required]
|
|
|
|
|
public double Price { get; set; }
|
|
|
|
|
private Dictionary<int, (IComponentModel, int)>? _repairComponents = null;
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, (IComponentModel, int)> RepairComponents
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_repairComponents == null)
|
|
|
|
|
{
|
|
|
|
|
_repairComponents = Components
|
|
|
|
|
.ToDictionary(recPC => recPC.ComponentId, recPC =>
|
|
|
|
|
(recPC.Component as IComponentModel, recPC.Count));
|
|
|
|
|
}
|
|
|
|
|
return _repairComponents;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[ForeignKey("RepairId")]
|
|
|
|
|
public virtual List<RepairComponent> Components { get; set; } = new();
|
|
|
|
|
[ForeignKey("RepairId")]
|
|
|
|
|
public virtual List<Order> Orders { get; set; } = new();
|
2024-03-25 21:16:58 +04:00
|
|
|
|
[ForeignKey("RepairId")]
|
|
|
|
|
public virtual List<ShopRepair> ShopRepairs { get; set; } = new();
|
2024-03-14 18:53:30 +04:00
|
|
|
|
public static Repair Create(RepairsShopDatabase context, RepairBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Repair()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
RepairName = model.RepairName,
|
|
|
|
|
Price = model.Price,
|
|
|
|
|
Components = model.RepairComponents.Select(x => new RepairComponent
|
|
|
|
|
{
|
|
|
|
|
Component = context.Components.First(y => y.Id == x.Key),
|
|
|
|
|
Count = x.Value.Item2
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(RepairBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
RepairName = model.RepairName;
|
|
|
|
|
Price = model.Price;
|
|
|
|
|
}
|
|
|
|
|
public RepairViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
RepairName = RepairName,
|
|
|
|
|
Price = Price,
|
|
|
|
|
RepairComponents = RepairComponents
|
|
|
|
|
};
|
|
|
|
|
public void UpdateComponents(RepairsShopDatabase context, RepairBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var RepairComponents = context.RepairComponents.Where(rec => rec.RepairId == model.Id).ToList();
|
|
|
|
|
if (RepairComponents != null && RepairComponents.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
context.RepairComponents.RemoveRange(RepairComponents.Where(rec => !model.RepairComponents.ContainsKey(rec.ComponentId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
foreach (var updateComponent in RepairComponents)
|
|
|
|
|
{
|
|
|
|
|
updateComponent.Count = model.RepairComponents[updateComponent.ComponentId].Item2;
|
|
|
|
|
model.RepairComponents.Remove(updateComponent.ComponentId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
var Repair = context.Repairs.First(x => x.Id == Id);
|
|
|
|
|
foreach (var pc in model.RepairComponents)
|
|
|
|
|
{
|
|
|
|
|
context.RepairComponents.Add(new RepairComponent
|
|
|
|
|
{
|
|
|
|
|
Repair = Repair,
|
|
|
|
|
Component = context.Components.First(x => x.Id == pc.Key),
|
|
|
|
|
Count = pc.Value.Item2
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_repairComponents = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|