-
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.
added get all trips query and endpoint
- Loading branch information
1 parent
360f7f9
commit 724fb06
Showing
4 changed files
with
83 additions
and
4 deletions.
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
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); | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
src/BSMS.Application/Features/Trip/Queries/GetAll/GetAllTripsQueryHandler.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,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); | ||
} | ||
} |