Skip to content

Commit

Permalink
added get all trips query and endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriyBorkovich committed May 2, 2024
1 parent 360f7f9 commit 724fb06
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
27 changes: 27 additions & 0 deletions src/BSMS.API/Controllers/TripController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using BSMS.API.Filters;
using BSMS.Application.Features.Common;
using BSMS.Application.Features.Trip.Queries.GetAll;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace BSMS.API.Controllers;

/// <inheritdoc/>
[ApiController]
[Route("/api/[controller]")]
[Authorization]
public class TripController(ISender sender) : ControllerBase
{
/// <summary>
/// Get all available trips based on current moment
/// </summary>
/// <param name="query">Contains route search field and pagination data</param>
/// <returns></returns>
[HttpGet("GetAll")]
public async Task<ActionResult<ListResponse<GetAllTripsQueryRespone>>> GetAll(
[FromQuery] GetAllTripsQuery query
)
{
return await sender.Send(query);
}
}
6 changes: 3 additions & 3 deletions src/BSMS.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
.AddCustomIdentityServices();

builder.Services.AddHostedService<CacheCleaningJob>();
// builder.Services.AddHostedService<DatabaseSeedJob>(); // comment if you have already filled DB
builder.Services.AddHostedService<ScheduleTripsJob>();
builder.Services.AddHostedService<TripStartOrStopPeriodicJob>();
builder.Services.AddHostedService<DatabaseSeedJob>(); // comment if you have already filled DB
// builder.Services.AddHostedService<ScheduleTripsJob>();
// builder.Services.AddHostedService<TripStartOrStopPeriodicJob>();

builder.Services.AddCors(options =>
{
Expand Down
1 change: 0 additions & 1 deletion src/BSMS.Application/BSMS.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<Folder Include="Features\Bus\Queries\GetById\" />
<Folder Include="Features\Route\Queries\" />
<Folder Include="Features\TicketPayment\" />
<Folder Include="Features\Trip\" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using BSMS.Application.Contracts.Persistence;
using BSMS.Application.Extensions;
using BSMS.Application.Features.Common;
using BSMS.Core.Views;
using LinqKit;
using Mapster;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace BSMS.Application.Features.Trip.Queries.GetAll;

public record GetAllTripsQuery(
string? SearchedRoute,
Pagination Pagination
) : IRequest<ListResponse<GetAllTripsQueryRespone>>;

public record GetAllTripsQueryRespone(
int TripId,
DateTime DepartureTime,
DateTime ArrivalTime,
string RouteName,
string BusBrand,
string CompanyName,
int BusRating,
string TripStatus,
int FreeSeatsCount
);

public class GetAllTripsQueryHandler(ITripRepository repository)
: IRequestHandler<GetAllTripsQuery, ListResponse<GetAllTripsQueryRespone>>
{
public async Task<ListResponse<GetAllTripsQueryRespone>> Handle(
GetAllTripsQuery request, CancellationToken cancellationToken)
{
var todayDateTime = DateTime.Now;
var filters = PredicateBuilder.New<TripView>(true);
if (!string.IsNullOrWhiteSpace(request.SearchedRoute))
{
filters = filters.And(t => t.RouteName.Contains(request.SearchedRoute));
}

var (trips, count) = await repository
.GetDetails()
.AsNoTracking()
.Where(filters)
.Where(t => t.DepartureTime.Date == todayDateTime.Date
&& t.DepartureTime >= todayDateTime)
.ProjectToType<GetAllTripsQueryRespone>()
.Page(request.Pagination);

return new ListResponse<GetAllTripsQueryRespone>(trips, count);
}
}

0 comments on commit 724fb06

Please sign in to comment.