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

59 lines
1.5 KiB
C#
Raw Normal View History

using CarServiceContracts.BindingModels;
using CarServiceContracts.Models;
using CarServiceContracts.ViewModels;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace CarServiceDatabase.Models
{
public class Vehicle : IVehicleModel
{
public int Id { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
public string? Plate { get; private set; }
public string? VIN { get; private set; }
[Required]
public int CustomerId { get; private set; }
/// <summary>
/// Заявки на ремонт
/// </summary>
[ForeignKey("VehicleId")]
public virtual List<RepairRequest> RepairRequests { get; set; } = new();
public virtual Customer Customer { get; set; } = new();
public static Vehicle? Create(CarServiceDbContext context, VehicleBindingModel? model)
{
if (model == null)
{
return null;
}
return new()
{
Name = model.Name,
Plate = model.Plate,
VIN = model.VIN,
Customer = context.Customers.First(x => x.Id == model.CustomerId)
};
}
public void Update(CarServiceDbContext context, VehicleBindingModel? model)
{
if (model == null)
{
return;
}
Name = model.Name;
Plate = model.Plate;
VIN = model.VIN;
Customer = context.Customers.First(x => x.Id == model.CustomerId);
}
public VehicleViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Plate = Plate,
VIN = VIN,
CustomerId = CustomerId
};
}
}