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
{
///
/// Сущность "Расписание"
///
public class Schedule : IScheduleModel
{
///
/// Идентификатор
///
public int Id { get; private set; }
///
/// Дата записи расписания
///
[Required]
public DateTime Date { get; private set; } = DateTime.Now;
///
/// Время отправления
///
[Required]
public DateTime DepartureTime { get; private set; } = DateTime.MinValue;
///
/// Время прибытия
///
[Required]
public DateTime ArrivalTime { get; private set; } = DateTime.MinValue;
///
/// Периодичность движения транспорта
///
[Required]
public DateTime Frequency { get; private set; } = DateTime.MinValue;
///
/// Идентификатор маршрута
///
[ForeignKey("RouteId")]
public int RouteId { get; private set; }
///
/// Сущность "Маршрут"
///
public virtual Route Route { get; private set; } = new();
///
/// Создание модели
///
///
///
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)
};
}
///
/// Изменение модели
///
///
public void Update(ScheduleBindingModel model)
{
if (model == null)
{
return;
}
Date = model.Date;
DepartureTime = model.DepartureTime;
ArrivalTime = model.ArrivalTime;
Frequency = model.Frequency;
}
///
/// Получение модели
///
public ScheduleViewModel GetViewModel => new()
{
Id = Id,
Date = Date,
DepartureTime = DepartureTime,
ArrivalTime = ArrivalTime,
Frequency = Frequency,
RouteId = RouteId,
RouteName = Route.Name
};
}
}