PIbd-21_MasenkinMS_SUBD_Rou.../RouteGuide/RouteGuideDatabaseImplement/Models/Transport.cs

108 lines
3.0 KiB
C#
Raw Permalink Normal View History

2024-04-02 02:15:17 +04:00
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
{
/// <summary>
/// Сущность "Транспорт"
/// </summary>
public class Transport : ITransportModel
{
/// <summary>
/// Идентификатор
/// </summary>
public int Id { get; private set; }
/// <summary>
/// Номерной знак
/// </summary>
[Required]
public string License { get; private set; } = string.Empty;
/// <summary>
/// Тип транспортного средства
/// </summary>
[Required]
public TransportType Type { get; private set; } = TransportType.Автобус;
/// <summary>
/// Вместимость (количество пассажиров)
/// </summary>
[Required]
public int Capacity { get; private set; }
/// <summary>
/// Идентификатор водителя
/// </summary>
[ForeignKey("DriverId")]
public int DriverId { get; private set; }
/// <summary>
/// Сущность "Водитель"
/// </summary>
public virtual Driver Driver { get; private set; } = new();
/// <summary>
/// Создание модели
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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)
};
}
/// <summary>
/// Изменение модели
/// </summary>
/// <param name="model"></param>
public void Update(TransportBindingModel model)
{
if (model == null)
{
return;
}
License = model.License;
Capacity = model.Capacity;
}
/// <summary>
/// Получение модели
/// </summary>
public TransportViewModel GetViewModel => new()
{
Id = Id,
License = License,
Type = Type,
2024-04-08 21:05:52 +04:00
Capacity= Capacity,
2024-04-05 02:48:52 +04:00
DriverId = DriverId,
DriverName = Driver.FullName
2024-04-02 02:15:17 +04:00
};
}
}