feat: add queries for professor
All checks were successful
.NET Test Pipeline / build-and-test (pull_request) Successful in 1m44s

This commit is contained in:
Polianin Nikita 2024-02-16 18:06:22 +03:00
parent a1a83ce2e6
commit c8c9b7f456
7 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,8 @@
using MediatR;
namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorDetails;
public class GetProfessorInfoQuery : IRequest<ProfessorInfoVm>
{
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.Professor.Queries.GetProfessorDetails;
public class GetProfessorInfoQueryHandler(IProfessorDbContext dbContext) : IRequestHandler<GetProfessorInfoQuery, ProfessorInfoVm>
{
public async Task<ProfessorInfoVm> Handle(GetProfessorInfoQuery request, CancellationToken cancellationToken)
{
var discipline = await dbContext.Professors.FirstOrDefaultAsync(p => p.Id == request.Id, cancellationToken) ?? throw new NotFoundException(typeof(Domain.Schedule.Group), request.Id);
return new ProfessorInfoVm()
{
Id = discipline.Id,
Name = discipline.Name,
};
}
}

View File

@ -0,0 +1,22 @@
namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorDetails;
/// <summary>
/// Represents professors.
/// </summary>
public class ProfessorInfoVm
{
/// <summary>
/// The unique identifier for the professor.
/// </summary>
public int Id { get; set; }
/// <summary>
/// The name of the professor.
/// </summary>
public required string Name { get; set; }
/// <summary>
/// The alt name of the professor. Usually is full name.
/// </summary>
public string? AltName { get; set; }
}

View File

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

View File

@ -0,0 +1,31 @@
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.Professor.Queries.GetProfessorList;
public class GetProfessorListQueryHandler(IProfessorDbContext dbContext) : IRequestHandler<GetProfessorListQuery, ProfessorListVm>
{
public async Task<ProfessorListVm> Handle(GetProfessorListQuery request, CancellationToken cancellationToken)
{
var dtos = dbContext.Professors.OrderBy(p => p.Id).Select(p => new ProfessorLookupDto()
{
Id = p.Id,
Name = p.Name,
AltName = p.AltName
});
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 ProfessorListVm
{
Professors = result
};
}
}

View File

@ -0,0 +1,14 @@
using System.Collections.Generic;
namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorList;
/// <summary>
/// Represents a view model containing multiple professors name.
/// </summary>
public class ProfessorListVm
{
/// <summary>
/// The list of professors.
/// </summary>
public IList<ProfessorLookupDto> Professors { get; set; } = new List<ProfessorLookupDto>();
}

View File

@ -0,0 +1,22 @@
namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorList;
/// <summary>
/// Represents professor.
/// </summary>
public class ProfessorLookupDto
{
/// <summary>
/// The unique identifier for the professor.
/// </summary>
public int Id { get; set; }
/// <summary>
/// The name of the professor.
/// </summary>
public required string Name { get; set; }
/// <summary>
/// The alt name of the professor. Usually is full name.
/// </summary>
public string? AltName { get; set; }
}