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; } /// /// Работы в заявке /// [ForeignKey("RepairRequestId")] public virtual List WorksInRequest { get; set; } = new(); /// /// Затраты на ремонт /// [ForeignKey("RepairRequestId")] public virtual List 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 }; } }