From 70c9a6347ec45e6f7ef99ffd9da2c477096cdca4 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Fri, 16 Feb 2024 22:34:41 +0300 Subject: [PATCH] feat: add queries for group --- .../GetGroupDetails/GetGroupInfoQuery.cs | 8 +++++ .../GetGroupInfoQueryHandler.cs | 28 +++++++++++++++++ .../Queries/GetGroupDetails/GroupInfoVm.cs | 27 ++++++++++++++++ .../Queries/GetGroupList/GetGroupListQuery.cs | 9 ++++++ .../GetGroupList/GetGroupListQueryHandler.cs | 31 +++++++++++++++++++ .../Group/Queries/GetGroupList/GroupListVm.cs | 14 +++++++++ .../Queries/GetGroupList/GroupLookupDto.cs | 22 +++++++++++++ 7 files changed, 139 insertions(+) create mode 100644 Application/Cqrs/Group/Queries/GetGroupDetails/GetGroupInfoQuery.cs create mode 100644 Application/Cqrs/Group/Queries/GetGroupDetails/GetGroupInfoQueryHandler.cs create mode 100644 Application/Cqrs/Group/Queries/GetGroupDetails/GroupInfoVm.cs create mode 100644 Application/Cqrs/Group/Queries/GetGroupList/GetGroupListQuery.cs create mode 100644 Application/Cqrs/Group/Queries/GetGroupList/GetGroupListQueryHandler.cs create mode 100644 Application/Cqrs/Group/Queries/GetGroupList/GroupListVm.cs create mode 100644 Application/Cqrs/Group/Queries/GetGroupList/GroupLookupDto.cs diff --git a/Application/Cqrs/Group/Queries/GetGroupDetails/GetGroupInfoQuery.cs b/Application/Cqrs/Group/Queries/GetGroupDetails/GetGroupInfoQuery.cs new file mode 100644 index 0000000..de0b87f --- /dev/null +++ b/Application/Cqrs/Group/Queries/GetGroupDetails/GetGroupInfoQuery.cs @@ -0,0 +1,8 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Group.Queries.GetGroupDetails; + +public class GetGroupInfoQuery : IRequest +{ + public required int Id { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Group/Queries/GetGroupDetails/GetGroupInfoQueryHandler.cs b/Application/Cqrs/Group/Queries/GetGroupDetails/GetGroupInfoQueryHandler.cs new file mode 100644 index 0000000..4d1a092 --- /dev/null +++ b/Application/Cqrs/Group/Queries/GetGroupDetails/GetGroupInfoQueryHandler.cs @@ -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 +{ + public async Task 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 + }; + } +} \ No newline at end of file diff --git a/Application/Cqrs/Group/Queries/GetGroupDetails/GroupInfoVm.cs b/Application/Cqrs/Group/Queries/GetGroupDetails/GroupInfoVm.cs new file mode 100644 index 0000000..8a4999c --- /dev/null +++ b/Application/Cqrs/Group/Queries/GetGroupDetails/GroupInfoVm.cs @@ -0,0 +1,27 @@ +namespace Mirea.Api.DataAccess.Application.Cqrs.Group.Queries.GetGroupDetails; + +/// +/// Represents groups. +/// +public class GroupInfoVm +{ + /// + /// The unique identifier for the group. + /// + public int Id { get; set; } + + /// + /// The name of the group. + /// + public required string Name { get; set; } + + /// + /// The faculty name for the group. + /// + public string? Faculty { get; set; } + + /// + /// The faculty identifier for the group. + /// + public int? FacultyId { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Group/Queries/GetGroupList/GetGroupListQuery.cs b/Application/Cqrs/Group/Queries/GetGroupList/GetGroupListQuery.cs new file mode 100644 index 0000000..7bcc6c9 --- /dev/null +++ b/Application/Cqrs/Group/Queries/GetGroupList/GetGroupListQuery.cs @@ -0,0 +1,9 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Group.Queries.GetGroupList; + +public class GetGroupListQuery : IRequest +{ + public int? Page { get; set; } + public int? PageSize { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Group/Queries/GetGroupList/GetGroupListQueryHandler.cs b/Application/Cqrs/Group/Queries/GetGroupList/GetGroupListQueryHandler.cs new file mode 100644 index 0000000..b0d63b1 --- /dev/null +++ b/Application/Cqrs/Group/Queries/GetGroupList/GetGroupListQueryHandler.cs @@ -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 +{ + public async Task 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 + }; + } +} \ No newline at end of file diff --git a/Application/Cqrs/Group/Queries/GetGroupList/GroupListVm.cs b/Application/Cqrs/Group/Queries/GetGroupList/GroupListVm.cs new file mode 100644 index 0000000..f4a777c --- /dev/null +++ b/Application/Cqrs/Group/Queries/GetGroupList/GroupListVm.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Group.Queries.GetGroupList; + +/// +/// Represents a view model containing multiple groups name. +/// +public class GroupListVm +{ + /// + /// The list of groups. + /// + public IList Groups { get; set; } = new List(); +} \ No newline at end of file diff --git a/Application/Cqrs/Group/Queries/GetGroupList/GroupLookupDto.cs b/Application/Cqrs/Group/Queries/GetGroupList/GroupLookupDto.cs new file mode 100644 index 0000000..306f8d2 --- /dev/null +++ b/Application/Cqrs/Group/Queries/GetGroupList/GroupLookupDto.cs @@ -0,0 +1,22 @@ +namespace Mirea.Api.DataAccess.Application.Cqrs.Group.Queries.GetGroupList; + +/// +/// Represents groups. +/// +public class GroupLookupDto +{ + /// + /// The unique identifier for the group. + /// + public int Id { get; set; } + + /// + /// The name of the group. + /// + public required string Name { get; set; } + + /// + /// The faculty identifier for the group. + /// + public int? FacultyId { get; set; } +} \ No newline at end of file