Skip to content

Commit

Permalink
created endpoint to get all stops related to trip
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriyBorkovich committed May 6, 2024
1 parent 1bf7c40 commit d8cb481
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/BSMS.API/BackgroundJobs/TripStartOrStopPeriodicJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ private async Task TryRestartAsync(BusStationContext dbContext, DateTime current
var delayedTrips = await dbContext.Trips
.Where(t => t.Status == TripStatus.Delayed
|| t.Status == TripStatus.Scheduled
|| t.Status == TripStatus.InTransit
&& t.DepartureTime != null
&& t.DepartureTime.Value.Date == currentTime.Date)
.ToListAsync(stoppingToken);
Expand Down
19 changes: 18 additions & 1 deletion src/BSMS.API/Controllers/TripController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using BSMS.API.Filters;
using BSMS.API.Extensions;
using BSMS.API.Filters;
using BSMS.Application.Features.Common;
using BSMS.Application.Features.Trip.Queries.GetAll;
using BSMS.Application.Features.Trip.Queries.GetAllStops;
using BSMS.Core.Enums;
using MediatR;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -25,4 +27,19 @@ [FromQuery] GetAllTripsQuery query
{
return await sender.Send(query);
}

/// <summary>
/// Get all route stops related to trip
/// </summary>
/// <param name="query">Contains trip ID</param>
/// <returns>List of stop IDs and names</returns>
[HttpGet("GetAllStops")]
public async Task<ActionResult<List<GetAllRouteStopsResponse>>> GetAllStops(
[FromQuery] GetAllRouteStopsQuery query
)
{
var result = await sender.Send(query);

return result.DecideWhatToReturn();
}
}
5 changes: 5 additions & 0 deletions src/BSMS.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.Configure<HostOptions>(hostOptions =>
{
hostOptions.BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.Ignore;
});

builder.Host.UseSerilog((context, loggerConfig) =>
{
loggerConfig.ReadFrom.Configuration(context.Configuration);
Expand Down
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;
}
}

0 comments on commit d8cb481

Please sign in to comment.