MireaBackend/SqlData/Application/Cqrs/Discipline/Queries/GetDisciplineList/GetDisciplineListQueryHandler.cs

30 lines
1.0 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.Discipline.Queries.GetDisciplineList;
public class GetDisciplineListQueryHandler(IDisciplineDbContext dbContext) : IRequestHandler<GetDisciplineListQuery, DisciplineListVm>
{
public async Task<DisciplineListVm> Handle(GetDisciplineListQuery request, CancellationToken cancellationToken)
{
var dtos = dbContext.Disciplines.OrderBy(d => d.Id).Select(d => new DisciplineLookupDto()
{
Id = d.Id,
Name = d.Name,
});
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 DisciplineListVm
{
Disciplines = result
};
}
}