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

This commit is contained in:
Polianin Nikita 2024-02-16 22:40:21 +03:00
parent 69a554ce76
commit ce3bd23673
7 changed files with 143 additions and 0 deletions

View File

@ -0,0 +1,32 @@
namespace Mirea.Api.DataAccess.Application.Cqrs.Faculty.Queries.GetFacultyDetails;
/// <summary>
/// Represents faculties.
/// </summary>
public class FacultyInfoVm
{
/// <summary>
/// The unique identifier for the faculty.
/// </summary>
public int Id { get; set; }
/// <summary>
/// The name of the faculty.
/// </summary>
public required string Name { get; set; }
/// <summary>
/// ID indicating the faculty's affiliation to the campus.
/// </summary>
public int? CampusId { get; set; }
/// <summary>
/// Campus name indicating the faculty's affiliation to the campus.
/// </summary>
public string? CampusName { get; set; }
/// <summary>
/// Campus code indicating the faculty's affiliation to the campus.
/// </summary>
public string? CampusCode { get; set; }
}

View File

@ -0,0 +1,8 @@
using MediatR;
namespace Mirea.Api.DataAccess.Application.Cqrs.Faculty.Queries.GetFacultyDetails;
public class GetFacultyInfoQuery : IRequest<FacultyInfoVm>
{
public required int Id { get; set; }
}

View File

@ -0,0 +1,27 @@
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.Faculty.Queries.GetFacultyDetails;
public class GetFacultyInfoQueryHandler(IFacultyDbContext dbContext) : IRequestHandler<GetFacultyInfoQuery, FacultyInfoVm>
{
public async Task<FacultyInfoVm> Handle(GetFacultyInfoQuery request, CancellationToken cancellationToken)
{
var faculty = await dbContext.Faculties
.Include(f => f.Campus)
.FirstOrDefaultAsync(f => f.Id == request.Id, cancellationToken) ?? throw new NotFoundException(typeof(Domain.Schedule.Faculty), request.Id);
return new FacultyInfoVm()
{
Id = faculty.Id,
Name = faculty.Name,
CampusId = faculty.CampusId,
CampusName = faculty.Campus?.FullName,
CampusCode = faculty.Campus?.CodeName
};
}
}

View File

@ -0,0 +1,14 @@
using System.Collections.Generic;
namespace Mirea.Api.DataAccess.Application.Cqrs.Faculty.Queries.GetFacultyList;
/// <summary>
/// Represents a view model containing multiple faculties.
/// </summary>
public class FacultyListVm
{
/// <summary>
/// The list of faculties.
/// </summary>
public IList<FacultyLookupDto> Faculties { get; set; } = new List<FacultyLookupDto>();
}

View File

@ -0,0 +1,22 @@
namespace Mirea.Api.DataAccess.Application.Cqrs.Faculty.Queries.GetFacultyList;
/// <summary>
/// Represents faculties.
/// </summary>
public class FacultyLookupDto
{
/// <summary>
/// The unique identifier for the faculty.
/// </summary>
public int Id { get; set; }
/// <summary>
/// The name of the faculty.
/// </summary>
public required string Name { get; set; }
/// <summary>
/// ID indicating the faculty's affiliation to the campus.
/// </summary>
public int? CampusId { get; set; }
}

View File

@ -0,0 +1,9 @@
using MediatR;
namespace Mirea.Api.DataAccess.Application.Cqrs.Faculty.Queries.GetFacultyList;
public class GetFacultyListQuery : IRequest<FacultyListVm>
{
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.Faculty.Queries.GetFacultyList;
public class GetFacultyListQueryHandler(IFacultyDbContext dbContext) : IRequestHandler<GetFacultyListQuery, FacultyListVm>
{
public async Task<FacultyListVm> Handle(GetFacultyListQuery request, CancellationToken cancellationToken)
{
var dtos = dbContext.Faculties.OrderBy(f => f.Id).Select(f => new FacultyLookupDto()
{
Id = f.Id,
Name = f.Name,
CampusId = f.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 FacultyListVm
{
Faculties = result
};
}
}