ISEbd-21_Melnikov_I.O._CarS.../CarService/CarServiceDatabase/Models/RepairRequest.cs

60 lines
1.8 KiB
C#

using CarServiceContracts.BindingModels;
using CarServiceContracts.Models;
using CarServiceContracts.ViewModels;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace CarServiceDatabase.Models
{
public class RepairRequest : IRepairRequestModel
{
public int Id { get; private set; }
[Required, Column(TypeName = "date")]
public DateTime DateCreated { get; private set; } = DateTime.Now;
[Required]
public int VehicleId { get; private set; }
/// <summary>
/// Работы в заявке
/// </summary>
[ForeignKey("RepairRequestId")]
public virtual List<WorkInRequest> WorksInRequest { get; set; } = new();
/// <summary>
/// Затраты на ремонт
/// </summary>
[ForeignKey("RepairRequestId")]
public virtual List<ItemForRepair> ItemsForRepair { get; set; } = new();
public virtual Vehicle Vehicle { get; set; } = new();
public static RepairRequest? Create(CarServiceDbContext context, RepairRequestBindingModel? model)
{
if (model == null)
{
return null;
}
return new()
{
DateCreated = model.DateCreated,
Vehicle = context.Vehicles.First(x => x.Id == model.VehicleId)
};
}
public void Update(CarServiceDbContext context, RepairRequestBindingModel? model)
{
if (model == null)
{
return;
}
DateCreated = model.DateCreated;
Vehicle = context.Vehicles.First(x => x.Id == model.VehicleId);
}
public RepairRequestViewModel GetViewModel => new()
{
Id = Id,
DateCreated = DateCreated,
VehicleId = VehicleId,
VehicleName = Vehicle.Name,
VehiclePlate = Vehicle.Plate ?? "Отсутствует",
VehicleVIN = Vehicle.VIN ?? "Отсутствует",
CustomerName = Vehicle.Customer.Name + " " + Vehicle.Customer.Surname
};
}
}