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 139 additions and 0 deletions
Showing only changes of commit 70c9a6347e - Show all commits

View File

@ -0,0 +1,8 @@
using MediatR;
namespace Mirea.Api.DataAccess.Application.Cqrs.Group.Queries.GetGroupDetails;
public class GetGroupInfoQuery : IRequest<GroupInfoVm>
{
public required int Id { get; set; }
}

View File

@ -0,0 +1,28 @@
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.Group
.Queries.GetGroupDetails;
public class GetGroupInfoQueryHandler(IGroupDbContext dbContext) : IRequestHandler<GetGroupInfoQuery, GroupInfoVm>
{
public async Task<GroupInfoVm> Handle(GetGroupInfoQuery request, CancellationToken cancellationToken)
{
var discipline = await dbContext.Groups
.Include(g => g.Faculty)
.FirstOrDefaultAsync(g => g.Id == request.Id, cancellationToken)
?? throw new NotFoundException(typeof(Domain.Schedule.Group), request.Id);
return new GroupInfoVm()
{
Id = discipline.Id,
Name = discipline.Name,
Faculty = discipline.Faculty?.Name,
FacultyId = discipline.FacultyId
};
}
}

View File

@ -0,0 +1,27 @@
namespace Mirea.Api.DataAccess.Application.Cqrs.Group.Queries.GetGroupDetails;
/// <summary>
/// Represents groups.
/// </summary>
public class GroupInfoVm
{
/// <summary>
/// The unique identifier for the group.
/// </summary>
public int Id { get; set; }
/// <summary>
/// The name of the group.
/// </summary>
public required string Name { get; set; }
/// <summary>
/// The faculty name for the group.
/// </summary>
public string? Faculty { get; set; }
/// <summary>
/// The faculty identifier for the group.
/// </summary>
public int? FacultyId { get; set; }
}

View File

@ -0,0 +1,9 @@
using MediatR;
namespace Mirea.Api.DataAccess.Application.Cqrs.Group.Queries.GetGroupList;
public class GetGroupListQuery : IRequest<GroupListVm>
{
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.Group.Queries.GetGroupList;
public class GetGroupListQueryHandler(IGroupDbContext dbContext) : IRequestHandler<GetGroupListQuery, GroupListVm>
{
public async Task<GroupListVm> Handle(GetGroupListQuery request, CancellationToken cancellationToken)
{
var dtos = dbContext.Groups.OrderBy(g => g.Id).Select(g => new GroupLookupDto()
{
Id = g.Id,
Name = g.Name,
FacultyId = g.FacultyId
});
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 GroupListVm
{
Groups = result
};
}
}

View File

@ -0,0 +1,14 @@
using System.Collections.Generic;
namespace Mirea.Api.DataAccess.Application.Cqrs.Group.Queries.GetGroupList;
/// <summary>
/// Represents a view model containing multiple groups name.
/// </summary>
public class GroupListVm
{
/// <summary>
/// The list of groups.
/// </summary>
public IList<GroupLookupDto> Groups { get; set; } = new List<GroupLookupDto>();
}

View File

@ -0,0 +1,22 @@
namespace Mirea.Api.DataAccess.Application.Cqrs.Group.Queries.GetGroupList;
/// <summary>
/// Represents groups.
/// </summary>
public class GroupLookupDto
{
/// <summary>
/// The unique identifier for the group.
/// </summary>
public int Id { get; set; }
/// <summary>
/// The name of the group.
/// </summary>
public required string Name { get; set; }
/// <summary>
/// The faculty identifier for the group.
/// </summary>
public int? FacultyId { get; set; }
}