using RouteGuideContracts.BindingModels;
using RouteGuideContracts.ViewModels;
using RouteGuideDataModels.Enums;
using RouteGuideDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace RouteGuideDatabaseImplement.Models
{
///
/// Сущность "Транспорт"
///
public class Transport : ITransportModel
{
///
/// Идентификатор
///
public int Id { get; private set; }
///
/// Номерной знак
///
[Required]
public string License { get; private set; } = string.Empty;
///
/// Тип транспортного средства
///
[Required]
public TransportType Type { get; private set; } = TransportType.Автобус;
///
/// Вместимость (количество пассажиров)
///
[Required]
public int Capacity { get; private set; }
///
/// Идентификатор водителя
///
[ForeignKey("DriverId")]
public int DriverId { get; private set; }
///
/// Сущность "Водитель"
///
public virtual Driver Driver { get; private set; } = new();
///
/// Создание модели
///
///
///
public static Transport? Create(RouteGuideDatabase context, TransportBindingModel model)
{
if (model == null)
{
return null;
}
return new Transport()
{
Id = model.Id,
License = model.License,
Type = model.Type,
Capacity = model.Capacity,
DriverId = model.DriverId,
Driver = context.Drivers
.FirstOrDefault(x => x.Id == model.DriverId)
};
}
///
/// Изменение модели
///
///
public void Update(TransportBindingModel model)
{
if (model == null)
{
return;
}
License = model.License;
Capacity = model.Capacity;
}
///
/// Получение модели
///
public TransportViewModel GetViewModel => new()
{
Id = Id,
License = License,
Type = Type,
Capacity= Capacity,
DriverId = DriverId,
DriverName = Driver.FullName
};
}
}