MireaBackend/SqlData/Application/Cqrs/LectureHall/Queries/GetLectureHallList/GetLectureHallListQueryHandler.cs

31 lines
1.1 KiB
C#

using MediatR;
using Microsoft.EntityFrameworkCore;
using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Mirea.Api.DataAccess.Application.Cqrs.LectureHall.Queries.GetLectureHallList;
public class GetLectureHallListQueryHandler(ILectureHallDbContext dbContext) : IRequestHandler<GetLectureHallListQuery, LectureHallListVm>
{
public async Task<LectureHallListVm> Handle(GetLectureHallListQuery request, CancellationToken cancellationToken)
{
var dtos = dbContext.LectureHalls.OrderBy(l => l.Id).Select(l => new LectureHallLookupDto()
{
Id = l.Id,
Name = l.Name,
CampusId = l.CampusId
});
if (request is { PageSize: not null, Page: not null })
dtos = dtos.Skip(request.Page.Value * request.PageSize.Value).Take(request.PageSize.Value);
var result = await dtos.ToListAsync(cancellationToken);
return new LectureHallListVm
{
LectureHalls = result
};
}
}