114 lines
2.3 KiB
C#
114 lines
2.3 KiB
C#
|
using MongoDB.Bson.Serialization.Attributes;
|
|||
|
using MongoDB.Bson;
|
|||
|
using RouteGuideDataModels.Models;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using RouteGuideContracts.BindingModels;
|
|||
|
using RouteGuideContracts.ViewModels;
|
|||
|
|
|||
|
namespace RouteGuideMongoDBImplement.Models
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Сущность "Водитель"
|
|||
|
/// </summary>
|
|||
|
public class Driver : IDriverModel<string>
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Идентификатор
|
|||
|
/// </summary>
|
|||
|
[BsonId]
|
|||
|
[BsonElement("_id")]
|
|||
|
[BsonRepresentation(BsonType.ObjectId)]
|
|||
|
public string? Id { get; private set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// ФИО водителя
|
|||
|
/// </summary>
|
|||
|
[BsonRequired]
|
|||
|
public string FullName { get; private set; } = string.Empty;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Номер телефона водителя
|
|||
|
/// </summary>
|
|||
|
[BsonRequired]
|
|||
|
public string Phone { get; private set; } = string.Empty;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Опыт работы водителя
|
|||
|
/// </summary>
|
|||
|
[BsonIgnoreIfNull]
|
|||
|
[BsonIgnoreIfDefault]
|
|||
|
public int? Experience { get; private set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Создание модели
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static Driver? Create(DriverBindingModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
return new Driver()
|
|||
|
{
|
|||
|
FullName = model.FullName,
|
|||
|
Phone = model.Phone,
|
|||
|
Experience = model.Experience
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Создание модели
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static Driver? Create(DriverViewModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
return new Driver()
|
|||
|
{
|
|||
|
FullName = model.FullName,
|
|||
|
Phone = model.Phone,
|
|||
|
Experience = model.Experience
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Изменение модели
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
public void Update(DriverBindingModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
FullName = model.FullName;
|
|||
|
Phone = model.Phone;
|
|||
|
Experience = model.Experience;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Получение модели
|
|||
|
/// </summary>
|
|||
|
public DriverViewModel GetViewModel => new()
|
|||
|
{
|
|||
|
Id = Id!,
|
|||
|
FullName = FullName,
|
|||
|
Phone = Phone,
|
|||
|
Experience = Experience
|
|||
|
};
|
|||
|
}
|
|||
|
}
|