114 lines
2.6 KiB
C#
114 lines
2.6 KiB
C#
|
using RouteGuideContracts.BindingModels;
|
|||
|
using RouteGuideContracts.ViewModels;
|
|||
|
using RouteGuideDataModels.Enums;
|
|||
|
using RouteGuideDataModels.Models;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using MongoDB.Bson.Serialization.Attributes;
|
|||
|
using MongoDB.Bson;
|
|||
|
using MongoDB.Driver;
|
|||
|
|
|||
|
namespace RouteGuideMongoDBImplement.Models
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Сущность "Транспорт"
|
|||
|
/// </summary>
|
|||
|
public class Transport : ITransportModel<string>
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Идентификатор
|
|||
|
/// </summary>
|
|||
|
[BsonId]
|
|||
|
[BsonElement("_id")]
|
|||
|
[BsonRepresentation(BsonType.ObjectId)]
|
|||
|
public string? Id { get; private set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Номерной знак
|
|||
|
/// </summary>
|
|||
|
[BsonRequired]
|
|||
|
public string License { get; private set; } = string.Empty;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Тип транспортного средства
|
|||
|
/// </summary>
|
|||
|
[BsonRequired]
|
|||
|
public TransportType Type { get; private set; } = TransportType.Автобус;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Вместимость (количество пассажиров)
|
|||
|
/// </summary>
|
|||
|
[BsonRequired]
|
|||
|
public int Capacity { get; private set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Идентификатор водителя
|
|||
|
/// </summary>
|
|||
|
[BsonRequired]
|
|||
|
[BsonElement("driver_id")]
|
|||
|
public string? DriverId { get; private set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Сущность "Водитель"
|
|||
|
/// </summary>
|
|||
|
[BsonIgnoreIfNull]
|
|||
|
[BsonIgnoreIfDefault]
|
|||
|
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()
|
|||
|
{
|
|||
|
License = model.License,
|
|||
|
Type = model.Type,
|
|||
|
Capacity = model.Capacity,
|
|||
|
DriverId = model.DriverId.ToString(),
|
|||
|
Driver = context.Drivers
|
|||
|
.Find(Builders<Driver>.Filter.Eq(x => x.Id, model.DriverId))
|
|||
|
.FirstOrDefault()
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
/// <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,
|
|||
|
Capacity = Capacity,
|
|||
|
DriverId = DriverId!,
|
|||
|
DriverName = Driver.FullName
|
|||
|
};
|
|||
|
}
|
|||
|
}
|