PIbd-21_MasenkinMS_SUBD_Rou.../RouteGuide/RouteGuideMongoDBImplement/Models/Stop.cs
2024-04-23 19:38:25 +04:00

113 lines
2.2 KiB
C#

using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson;
using RouteGuideContracts.BindingModels;
using RouteGuideContracts.ViewModels;
using RouteGuideDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RouteGuideMongoDBImplement.Models
{
/// <summary>
/// Сущность "Остановка"
/// </summary>
public class Stop : IStopModel<string>
{
/// <summary>
/// Идентификатор
/// </summary>
[BsonId]
[BsonElement("_id")]
[BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; private set; }
/// <summary>
/// Название остановки
/// </summary>
[BsonRequired]
public string Name { get; private set; } = string.Empty;
/// <summary>
/// Название улицы
/// </summary>
[BsonRequired]
public string Street { get; private set; } = string.Empty;
/// <summary>
/// Номер дома
/// </summary>
[BsonRequired]
public int Number { get; private set; }
/// <summary>
/// Создание модели
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public static Stop? Create(StopBindingModel model)
{
if (model == null)
{
return null;
}
return new Stop()
{
Name = model.Name,
Street = model.Street,
Number = model.Number
};
}
/// <summary>
/// Создание модели
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public static Stop? Create(StopViewModel model)
{
if (model == null)
{
return null;
}
return new Stop()
{
Name = model.Name,
Street = model.Street,
Number = model.Number
};
}
/// <summary>
/// Изменение модели
/// </summary>
/// <param name="model"></param>
public void Update(StopBindingModel model)
{
if (model == null)
{
return;
}
Name = model.Name;
Street = model.Street;
Number = model.Number;
}
/// <summary>
/// Получение модели
/// </summary>
public StopViewModel GetViewModel => new()
{
Id = Id!,
Name = Name,
Street = Street,
Number = Number
};
}
}