feat: add queries for group
This commit is contained in:
parent
c8c9b7f456
commit
70c9a6347e
@ -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; }
|
||||
}
|
@ -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
|
||||
};
|
||||
}
|
||||
}
|
@ -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; }
|
||||
}
|
@ -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; }
|
||||
}
|
@ -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
|
||||
};
|
||||
}
|
||||
}
|
14
Application/Cqrs/Group/Queries/GetGroupList/GroupListVm.cs
Normal file
14
Application/Cqrs/Group/Queries/GetGroupList/GroupListVm.cs
Normal 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>();
|
||||
}
|
@ -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; }
|
||||
}
|
Loading…
Reference in New Issue
Block a user