PIbd-21_MasenkinMS_SUBD_Rou.../RouteGuide/RouteGuideDatabaseImplement/Models/Schedule.cs

116 lines
3.3 KiB
C#

using RouteGuideContracts.BindingModels;
using RouteGuideContracts.ViewModels;
using RouteGuideDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RouteGuideDatabaseImplement.Models
{
/// <summary>
/// Сущность "Расписание"
/// </summary>
public class Schedule : IScheduleModel
{
/// <summary>
/// Идентификатор
/// </summary>
public int Id { get; private set; }
/// <summary>
/// Дата записи расписания
/// </summary>
[Required]
public DateTime Date { get; private set; } = DateTime.Now;
/// <summary>
/// Время отправления
/// </summary>
[Required]
public DateTime DepartureTime { get; private set; } = DateTime.MinValue;
/// <summary>
/// Время прибытия
/// </summary>
[Required]
public DateTime ArrivalTime { get; private set; } = DateTime.MinValue;
/// <summary>
/// Периодичность движения транспорта
/// </summary>
[Required]
public DateTime Frequency { get; private set; } = DateTime.MinValue;
/// <summary>
/// Идентификатор маршрута
/// </summary>
[ForeignKey("RouteId")]
public int RouteId { get; private set; }
/// <summary>
/// Сущность "Маршрут"
/// </summary>
public virtual Route Route { get; private set; } = new();
/// <summary>
/// Создание модели
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public static Schedule? Create(RouteGuideDatabase context, ScheduleBindingModel model)
{
if (model == null)
{
return null;
}
return new Schedule()
{
Id = model.Id,
Date = model.Date,
DepartureTime = model.DepartureTime,
ArrivalTime = model.ArrivalTime,
Frequency = model.Frequency,
RouteId = model.RouteId,
Route = context.Routes
.FirstOrDefault(x => x.Id == model.RouteId)
};
}
/// <summary>
/// Изменение модели
/// </summary>
/// <param name="model"></param>
public void Update(ScheduleBindingModel model)
{
if (model == null)
{
return;
}
Date = model.Date;
DepartureTime = model.DepartureTime;
ArrivalTime = model.ArrivalTime;
Frequency = model.Frequency;
}
/// <summary>
/// Получение модели
/// </summary>
public ScheduleViewModel GetViewModel => new()
{
Id = Id,
Date = Date,
DepartureTime = DepartureTime,
ArrivalTime = ArrivalTime,
Frequency = Frequency,
RouteId = RouteId
};
}
}