Add queries #8

Merged
Wesser merged 9 commits from feat/application-query into release/v1.0.0 2024-02-16 22:56:02 +03:00
7 changed files with 117 additions and 0 deletions
Showing only changes of commit a1a83ce2e6 - Show all commits

View File

@ -0,0 +1,17 @@
namespace Mirea.Api.DataAccess.Application.Cqrs.Discipline.Queries.GetDisciplineDetails;
/// <summary>
/// Represents disciplines.
/// </summary>
public class DisciplineInfoVm
{
/// <summary>
/// The unique identifier for the discipline.
/// </summary>
public int Id { get; set; }
/// <summary>
/// The name of the discipline.
/// </summary>
public required string Name { get; set; }
}

View File

@ -0,0 +1,8 @@
using MediatR;
namespace Mirea.Api.DataAccess.Application.Cqrs.Discipline.Queries.GetDisciplineDetails;
public class GetDisciplineInfoQuery : IRequest<DisciplineInfoVm>
{
public required int Id { get; set; }
}

View File

@ -0,0 +1,22 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using Mirea.Api.DataAccess.Application.Common.Exceptions;
using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule;
using System.Threading;
using System.Threading.Tasks;
namespace Mirea.Api.DataAccess.Application.Cqrs.Discipline.Queries.GetDisciplineDetails;
public class GetDisciplineInfoQueryHandler(IDisciplineDbContext dbContext) : IRequestHandler<GetDisciplineInfoQuery, DisciplineInfoVm>
{
public async Task<DisciplineInfoVm> Handle(GetDisciplineInfoQuery request, CancellationToken cancellationToken)
{
var discipline = await dbContext.Disciplines.FirstOrDefaultAsync(d => d.Id == request.Id, cancellationToken) ?? throw new NotFoundException(typeof(Domain.Schedule.Discipline), request.Id);
return new DisciplineInfoVm()
{
Id = discipline.Id,
Name = discipline.Name,
};
}
}

View File

@ -0,0 +1,14 @@
using System.Collections.Generic;
namespace Mirea.Api.DataAccess.Application.Cqrs.Discipline.Queries.GetDisciplineList;
/// <summary>
/// Represents a view model containing multiple disciplines name.
/// </summary>
public class DisciplineListVm
{
/// <summary>
/// The list of disciplines.
/// </summary>
public IList<DisciplineLookupDto> Disciplines { get; set; } = new List<DisciplineLookupDto>();
}

View File

@ -0,0 +1,17 @@
namespace Mirea.Api.DataAccess.Application.Cqrs.Discipline.Queries.GetDisciplineList;
/// <summary>
/// Represents disciplines.
/// </summary>
public class DisciplineLookupDto
{
/// <summary>
/// The unique identifier for the discipline.
/// </summary>
public int Id { get; set; }
/// <summary>
/// The name of the discipline.
/// </summary>
public required string Name { get; set; }
}

View File

@ -0,0 +1,9 @@
using MediatR;
namespace Mirea.Api.DataAccess.Application.Cqrs.Discipline.Queries.GetDisciplineList;
public class GetDisciplineListQuery : IRequest<DisciplineListVm>
{
public int? Page { get; set; }
public int? PageSize { get; set; }
}

View File

@ -0,0 +1,30 @@
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
};
}
}