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

72 lines
1.6 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(VehicleBindingModel? model)
{
if (model == null)
{
return null;
}
return new()
{
Id = model.Id,
Name = model.Name,
Plate = model.Plate,
VIN = model.VIN,
CustomerId = model.CustomerId
};
}
public static Vehicle Create(VehicleViewModel model)
{
return new()
{
Id = model.Id,
Name = model.Name,
Plate = model.Plate,
VIN = model.VIN,
CustomerId = model.CustomerId
};
}
public void Update(VehicleBindingModel? model)
{
if (model == null)
{
return;
}
Id = model.Id;
Name = model.Name;
Plate = model.Plate;
VIN = model.VIN;
CustomerId = model.CustomerId;
}
public VehicleViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Plate = Plate,
VIN = VIN,
CustomerId = CustomerId
};
}
}