96 lines
3.2 KiB
C#
96 lines
3.2 KiB
C#
using System.ComponentModel.DataAnnotations;
|
||
using TransportCompanyContracts.BindingModels;
|
||
using TransportCompanyContracts.ViewModels;
|
||
using TransportCompanyDataModels.Enums;
|
||
using TransportCompanyDataModels.Models;
|
||
|
||
namespace TransportCompanyDatabaseImplement.Models
|
||
{
|
||
public class Transportation : ITransportationModel
|
||
{
|
||
public int Id { get; private set; }
|
||
|
||
[Required]
|
||
public int DriverId { get; private set; }
|
||
public virtual Driver Driver { get; set; } = new();
|
||
|
||
[Required]
|
||
public int TransportId { get; private set; }
|
||
public virtual Transport Transport { get; set; } = new();
|
||
|
||
[Required]
|
||
public int CargoId { get; private set; }
|
||
public virtual Cargo Cargo { get; set; } = new();
|
||
|
||
[Required]
|
||
public int Count { get; private set; }
|
||
|
||
[Required]
|
||
public int PointToId { get; private set; }
|
||
public virtual Point PointTo { get; set; } = new();
|
||
|
||
[Required]
|
||
public int PointFromId { get; private set; }
|
||
public virtual Point PointFrom { get; set; } = new();
|
||
|
||
[Required]
|
||
public TransportationStatus Status { get; private set; } = TransportationStatus.Неизвестен;
|
||
|
||
[Required]
|
||
public DateTime DepartureDate { get; private set; } = DateTime.Now;
|
||
|
||
public DateTime? ArrivalDate { get; private set; }
|
||
|
||
public static Transportation? Create(TransportCompanyDatabase context, TransportationBindingModel model)
|
||
{
|
||
return new Transportation()
|
||
{
|
||
Id = model.Id,
|
||
DriverId = model.DriverId,
|
||
Driver = context.Drivers.First(x => x.Id == model.DriverId),
|
||
TransportId = model.TransportId,
|
||
Transport = context.Transports.First(x => x.Id == model.TransportId),
|
||
CargoId = model.CargoId,
|
||
Cargo = context.Cargos.First(x => x.Id == model.CargoId),
|
||
Count = model.Count,
|
||
PointToId = model.PointToId,
|
||
PointTo = context.Points.First(x => x.Id == model.PointToId),
|
||
PointFromId = model.PointFromId,
|
||
PointFrom = context.Points.First(x => x.Id == model.PointFromId),
|
||
Status = model.Status,
|
||
DepartureDate = model.DepartureDate,
|
||
ArrivalDate = model.ArrivalDate,
|
||
};
|
||
}
|
||
|
||
public void Update(TransportationBindingModel? model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
return;
|
||
}
|
||
Status = model.Status;
|
||
ArrivalDate = model.ArrivalDate;
|
||
}
|
||
|
||
public TransportationViewModel GetViewModel => new()
|
||
{
|
||
Id = Id,
|
||
DriverId = DriverId,
|
||
DriverFio = Driver.DriverFio,
|
||
TransportId = TransportId,
|
||
Model = Transport.Model,
|
||
CargoId = CargoId,
|
||
CargoName = Cargo.CargoName,
|
||
Count = Count,
|
||
PointToId = PointToId,
|
||
PointNameTo = PointTo.PointName,
|
||
PointFromId = PointFromId,
|
||
PointNameFrom = PointFrom.PointName,
|
||
Status = Status,
|
||
DepartureDate = DepartureDate,
|
||
ArrivalDate = ArrivalDate,
|
||
};
|
||
}
|
||
}
|