PIbd-21_MasenkinMS_SUBD_Rou.../RouteGuide/RouteGuideDatabaseImplement/Models/Transport.cs
2024-04-05 02:48:52 +04:00

107 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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,
DriverId = DriverId,
DriverName = Driver.FullName
};
}
}