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
{
///
/// Сущность "Транспорт"
///
public class Transport : ITransportModel
{
///
/// Идентификатор
///
[BsonId]
[BsonElement("_id")]
[BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; private set; }
///
/// Номерной знак
///
[BsonRequired]
public string License { get; private set; } = string.Empty;
///
/// Тип транспортного средства
///
[BsonRequired]
public TransportType Type { get; private set; } = TransportType.Автобус;
///
/// Вместимость (количество пассажиров)
///
[BsonRequired]
public int Capacity { get; private set; }
///
/// Идентификатор водителя
///
[BsonRequired]
[BsonElement("driver_id")]
public string? DriverId { get; private set; }
///
/// Сущность "Водитель"
///
[BsonIgnoreIfNull]
[BsonIgnoreIfDefault]
public virtual Driver Driver { get; private set; } = new();
///
/// Создание модели
///
///
///
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.Filter.Eq(x => x.Id, model.DriverId))
.FirstOrDefault()
};
}
///
/// Изменение модели
///
///
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
};
}
}