-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created endpoint to get all stops related to trip
- Loading branch information
1 parent
1bf7c40
commit d8cb481
Showing
4 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/BSMS.Application/Features/Trip/Queries/GetAllStops/GetAllRouteStopsQueryHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using BSMS.Application.Contracts.Persistence; | ||
using BSMS.Application.Helpers; | ||
using BSMS.Core.Entities; | ||
using MapsterMapper; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace BSMS.Application.Features.Trip.Queries.GetAllStops; | ||
|
||
public record GetAllRouteStopsQuery(int TripId) : IRequest<MethodResult<List<GetAllRouteStopsResponse>>>; | ||
|
||
public record GetAllRouteStopsResponse( | ||
int StopId, | ||
string Name | ||
); | ||
|
||
public class GetAllRouteStopsQueryHandler( | ||
ITripRepository tripRepository, | ||
IMapper mapper, | ||
MethodResultFactory methodResultFactory | ||
) : IRequestHandler<GetAllRouteStopsQuery, MethodResult<List<GetAllRouteStopsResponse>>> | ||
{ | ||
public async Task<MethodResult<List<GetAllRouteStopsResponse>>> Handle(GetAllRouteStopsQuery request, CancellationToken cancellationToken) | ||
{ | ||
var result = methodResultFactory.Create<List<GetAllRouteStopsResponse>>(); | ||
|
||
var tripExists = await tripRepository.AnyAsync(t => t.TripId == request.TripId); | ||
if (!tripExists) | ||
{ | ||
result.SetError("Trip not found", System.Net.HttpStatusCode.NotFound); | ||
return result; | ||
} | ||
|
||
var orderedStops = await tripRepository.GetAll() | ||
.Where(t => t.TripId == request.TripId) | ||
.SelectMany(t => t.BusScheduleEntry.Route.Stops) | ||
.OrderBy(s => s.DistanceToPrevious) | ||
.Select(s => new Stop { StopId = s.StopId, Name = s.Name }) | ||
.ToListAsync(cancellationToken); | ||
|
||
result.Data = mapper.Map<List<GetAllRouteStopsResponse>>(orderedStops); | ||
|
||
return result; | ||
} | ||
} |