DB/TransportGuideDatabaseImplements/Models/Route.cs

80 lines
1.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TransportGuideContracts.BindingModels;
using TransportGuideContracts.ViewModels;
using TransportGuideDataModels.Models;
namespace TransportGuideDatabaseImplements.Models
{
public class Route : IRouteModel
{
public int Id { get; private set; }
[Required]
public int TransportTypeId { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
[Required]
public string IP { get; private set; } = string.Empty;
public virtual TransportType transportType { get; set; }
[ForeignKey("RouteId")]
public virtual List<StopRoute> StopRoutes { get; set; } = new();
public static Route? Create(RouteBindingModel? model)
{
if (model == null)
{
return null;
}
return new Route()
{
Id = model.Id,
TransportTypeId = model.TransportTypeId,
IP = model.IP,
Name= model.Name
};
}
public void Update(RouteBindingModel? model)
{
if (model == null)
{
return;
}
TransportTypeId = model.TransportTypeId;
IP = model.IP;
Name = model.Name;
}
public RouteViewModel GetViewModel
{
get
{
using var context = new TransportGuideDB();
return new RouteViewModel
{
Id = Id,
TransportTypeId = TransportTypeId,
IP = IP,
Name = Name,
TransportTypeName = context.TransportTypes.FirstOrDefault(x => x.Id == TransportTypeId)?.Name ?? string.Empty,
};
}
}
}
}