2024-03-23 14:59:30 +04:00
|
|
|
|
using CarRepairShopContracts.BindingModels;
|
|
|
|
|
using CarRepairShopContracts.ViewModels;
|
|
|
|
|
using CarRepairShopDataModels.Models;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
2024-05-17 00:53:12 +04:00
|
|
|
|
using System.Runtime.Serialization;
|
2024-03-23 14:59:30 +04:00
|
|
|
|
|
|
|
|
|
namespace CarRepairShopDatabaseImplement.Models
|
|
|
|
|
{
|
2024-05-17 00:53:12 +04:00
|
|
|
|
[DataContract]
|
2024-03-23 14:59:30 +04:00
|
|
|
|
public class Repair : IRepairModel
|
|
|
|
|
{
|
2024-05-17 00:53:12 +04:00
|
|
|
|
[DataMember]
|
2024-03-23 14:59:30 +04:00
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
|
2024-05-17 00:53:12 +04:00
|
|
|
|
[DataMember]
|
2024-03-23 14:59:30 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public string RepairName { get; set; } = string.Empty;
|
|
|
|
|
|
2024-05-17 00:53:12 +04:00
|
|
|
|
[DataMember]
|
2024-03-23 14:59:30 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public double Price { get; set; }
|
|
|
|
|
|
2024-03-23 15:13:31 +04:00
|
|
|
|
private Dictionary<int, (IComponentModel, int)>? _repairComponents = null;
|
2024-03-23 14:59:30 +04:00
|
|
|
|
|
2024-05-17 00:53:12 +04:00
|
|
|
|
[DataMember]
|
2024-03-23 14:59:30 +04:00
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, (IComponentModel, int)> RepairComponents
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2024-03-23 15:13:31 +04:00
|
|
|
|
if (_repairComponents == null)
|
2024-03-23 14:59:30 +04:00
|
|
|
|
{
|
2024-03-23 15:13:31 +04:00
|
|
|
|
_repairComponents = Components
|
2024-03-23 14:59:30 +04:00
|
|
|
|
.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
|
|
|
|
|
}
|
2024-03-23 15:13:31 +04:00
|
|
|
|
return _repairComponents;
|
2024-03-23 14:59:30 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ForeignKey("RepairId")]
|
|
|
|
|
public virtual List<RepairComponent> Components { get; set; } = new();
|
|
|
|
|
|
|
|
|
|
[ForeignKey("RepairId")]
|
|
|
|
|
public virtual List<Order> Orders { get; set; } = new();
|
|
|
|
|
|
|
|
|
|
public static Repair Create(CarRepairShopDatabase 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(CarRepairShopDatabase context, RepairBindingModel model)
|
|
|
|
|
{
|
2024-03-23 15:13:31 +04:00
|
|
|
|
var repairComponents = context.RepairComponents.Where(rec => rec.RepairId == model.Id).ToList();
|
|
|
|
|
if (repairComponents != null && repairComponents.Count > 0)
|
2024-03-23 14:59:30 +04:00
|
|
|
|
{ // удалили те, которых нет в модели
|
2024-03-23 15:13:31 +04:00
|
|
|
|
context.RepairComponents.RemoveRange(repairComponents.Where(rec => !model.RepairComponents.ContainsKey(rec.ComponentId)));
|
2024-03-23 14:59:30 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
// обновили количество у существующих записей
|
2024-03-23 15:13:31 +04:00
|
|
|
|
foreach (var updateComponent in repairComponents)
|
2024-03-23 14:59:30 +04:00
|
|
|
|
{
|
|
|
|
|
updateComponent.Count = model.RepairComponents[updateComponent.ComponentId].Item2;
|
|
|
|
|
model.RepairComponents.Remove(updateComponent.ComponentId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
2024-03-23 15:13:31 +04:00
|
|
|
|
var repair = context.Repairs.First(x => x.Id == Id);
|
2024-03-23 14:59:30 +04:00
|
|
|
|
foreach (var pc in model.RepairComponents)
|
|
|
|
|
{
|
|
|
|
|
context.RepairComponents.Add(new RepairComponent
|
|
|
|
|
{
|
2024-03-23 15:13:31 +04:00
|
|
|
|
Repair = repair,
|
2024-03-23 14:59:30 +04:00
|
|
|
|
Component = context.Components.First(x => x.Id == pc.Key),
|
|
|
|
|
Count = pc.Value.Item2
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
2024-03-23 15:13:31 +04:00
|
|
|
|
_repairComponents = null;
|
2024-03-23 14:59:30 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|