autobus-api_old/AutobusApi.Application/RouteSearch/RouteWithTransfersDto.cs

144 lines
4.9 KiB
C#

using AutobusApi.Application.Common.Mappings;
using AutoMapper;
namespace AutobusApi.Application.RouteSearch;
public class RouteWithTransfersDto : IMapFrom<List<EnrollmentAddressVertex>>
{
public List<SubRouteDto> Routes { get; set; } = null!;
public TimeSpan TotalTravelTime { get; set; }
public double TotalCost { get; set; }
// public int NumberOfTransfers { get; set; }
public DateTime DeparuteDateTime { get; set; }
public DateTime ArrivalDateTime { get; set; }
public void Mapping(Profile profile)
{
profile.CreateMap<List<EnrollmentAddressVertex>, RouteWithTransfersDto>()
.ForMember(
d => d.TotalCost,
opt => opt.MapFrom(
s => s
.SkipLast(1)
.Select(eav => eav.VehicleEnrollment)
.SelectMany(ve => ve.RouteAddressDetails)
.Select(rad => rad.CostToNextAddress)
.Aggregate(0.0, (sum, val) => sum + val)
)
)
.ForMember(
d => d.TotalTravelTime,
opt => opt.MapFrom(
s =>
s.Last().VehicleEnrollment.GetArrivalDateTimeUtc(s.Last().Address.Id) -
s.First().VehicleEnrollment.GetDepartureDateTimeUtc(s.First().Address.Id)
)
)
.ForMember(d => d.DeparuteDateTime, opt => opt.MapFrom(s => s.First().VehicleEnrollment.GetDepartureDateTimeUtc(s.First().Address.Id)))
.ForMember(d => d.ArrivalDateTime, opt => opt.MapFrom(s => s.Last().VehicleEnrollment.GetArrivalDateTimeUtc(s.Last().Address.Id)))
.ForMember(d => d.Routes, opt => opt.MapFrom(s => CreateRoutes(s)));
}
List<SubRouteDto> CreateRoutes(List<EnrollmentAddressVertex> vertices)
{
var result = new List<SubRouteDto>();
var groupedByVehicle = vertices.GroupBy(ev => ev.VehicleEnrollment);
int order = 1;
foreach (var group in groupedByVehicle)
{
var subRouteDto = new SubRouteDto
{
Stations = new List<StationDto>(),
VehicleType = "Unknown",//group.Key.Vehicle.Type, // Replace with the actual property to fetch VehicleType
CompanyName = group.Key.Vehicle.Company.Name, // Replace with the actual property to fetch CompanyName
CompanyId = group.Key.Vehicle.Company.Id, // Replace with the actual property to fetch CompanyId
Order = order
// Other properties initialization
};
order++;
foreach (var enrollmentVertex in group)
{
var stationDto = new StationDto
{
AddressId = enrollmentVertex.Address.Id,
AddressName = enrollmentVertex.Address.Name,
CityId = enrollmentVertex.Address.CityId,
CityName = enrollmentVertex.Address.City.Name,
RegionId = enrollmentVertex.Address.City.RegionId,
RegionName = enrollmentVertex.Address.City.Region.Name,
CountryId = enrollmentVertex.Address.City.Region.CountryId,
CountryName = enrollmentVertex.Address.City.Region.Country.Name,
DeparureTime = enrollmentVertex.GetDepartureDateTimeUtc(),
ArrivalTime = enrollmentVertex.GetArrivalDateTimeUtc(),
StopTime = enrollmentVertex.GetDepartureDateTimeUtc() - enrollmentVertex.GetArrivalDateTimeUtc(),
Latitude = enrollmentVertex.Address.Location.Latitude,
Longitude = enrollmentVertex.Address.Location.Longitude
};
subRouteDto.Stations.Add(stationDto);
}
result.Add(subRouteDto);
}
return result;
}
}
public class SubRouteDto
{
public List<StationDto> Stations { get; set; } = null!;
public string VehicleType { get; set; } = null!;
public string CompanyName { get; set; } = null!;
public int CompanyId { get; set; }
public int TicketsAvailable { get; set; }
public int TicketsBought { get; set; }
public int Order { get; set; }
}
public class StationDto : IMapFrom<EnrollmentAddressVertex>
{
public int AddressId { get; set; }
public string AddressName { get; set; } = null!;
public int CityId { get; set; }
public string CityName { get; set; } = null!;
public int RegionId { get; set; }
public string RegionName { get; set; } = null!;
public int CountryId { get; set; }
public string CountryName { get; set; } = null!;
public DateTime DeparureTime { get; set; }
public DateTime ArrivalTime { get; set; }
public TimeSpan StopTime { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
}