From a1a83ce2e6cf398922891e700c0df72312454e10 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sun, 28 Jan 2024 05:59:48 +0300 Subject: [PATCH] feat: add queries for the discipline --- .../GetDisciplineDetails/DisciplineInfoVm.cs | 17 +++++++++++ .../GetDisciplineInfoQuery.cs | 8 +++++ .../GetDisciplineInfoQueryHandler.cs | 22 ++++++++++++++ .../GetDisciplineList/DisciplineListVm.cs | 14 +++++++++ .../GetDisciplineList/DisciplineLookupDto.cs | 17 +++++++++++ .../GetDisciplineListQuery.cs | 9 ++++++ .../GetDisciplineListQueryHandler.cs | 30 +++++++++++++++++++ 7 files changed, 117 insertions(+) create mode 100644 Application/Cqrs/Discipline/Queries/GetDisciplineDetails/DisciplineInfoVm.cs create mode 100644 Application/Cqrs/Discipline/Queries/GetDisciplineDetails/GetDisciplineInfoQuery.cs create mode 100644 Application/Cqrs/Discipline/Queries/GetDisciplineDetails/GetDisciplineInfoQueryHandler.cs create mode 100644 Application/Cqrs/Discipline/Queries/GetDisciplineList/DisciplineListVm.cs create mode 100644 Application/Cqrs/Discipline/Queries/GetDisciplineList/DisciplineLookupDto.cs create mode 100644 Application/Cqrs/Discipline/Queries/GetDisciplineList/GetDisciplineListQuery.cs create mode 100644 Application/Cqrs/Discipline/Queries/GetDisciplineList/GetDisciplineListQueryHandler.cs diff --git a/Application/Cqrs/Discipline/Queries/GetDisciplineDetails/DisciplineInfoVm.cs b/Application/Cqrs/Discipline/Queries/GetDisciplineDetails/DisciplineInfoVm.cs new file mode 100644 index 0000000..c6ac1b1 --- /dev/null +++ b/Application/Cqrs/Discipline/Queries/GetDisciplineDetails/DisciplineInfoVm.cs @@ -0,0 +1,17 @@ +namespace Mirea.Api.DataAccess.Application.Cqrs.Discipline.Queries.GetDisciplineDetails; + +/// +/// Represents disciplines. +/// +public class DisciplineInfoVm +{ + /// + /// The unique identifier for the discipline. + /// + public int Id { get; set; } + + /// + /// The name of the discipline. + /// + public required string Name { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Discipline/Queries/GetDisciplineDetails/GetDisciplineInfoQuery.cs b/Application/Cqrs/Discipline/Queries/GetDisciplineDetails/GetDisciplineInfoQuery.cs new file mode 100644 index 0000000..b6c0621 --- /dev/null +++ b/Application/Cqrs/Discipline/Queries/GetDisciplineDetails/GetDisciplineInfoQuery.cs @@ -0,0 +1,8 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Discipline.Queries.GetDisciplineDetails; + +public class GetDisciplineInfoQuery : IRequest +{ + public required int Id { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Discipline/Queries/GetDisciplineDetails/GetDisciplineInfoQueryHandler.cs b/Application/Cqrs/Discipline/Queries/GetDisciplineDetails/GetDisciplineInfoQueryHandler.cs new file mode 100644 index 0000000..007e74f --- /dev/null +++ b/Application/Cqrs/Discipline/Queries/GetDisciplineDetails/GetDisciplineInfoQueryHandler.cs @@ -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.Discipline.Queries.GetDisciplineDetails; + +public class GetDisciplineInfoQueryHandler(IDisciplineDbContext dbContext) : IRequestHandler +{ + public async Task Handle(GetDisciplineInfoQuery request, CancellationToken cancellationToken) + { + var discipline = await dbContext.Disciplines.FirstOrDefaultAsync(d => d.Id == request.Id, cancellationToken) ?? throw new NotFoundException(typeof(Domain.Schedule.Discipline), request.Id); + + return new DisciplineInfoVm() + { + Id = discipline.Id, + Name = discipline.Name, + }; + } +} \ No newline at end of file diff --git a/Application/Cqrs/Discipline/Queries/GetDisciplineList/DisciplineListVm.cs b/Application/Cqrs/Discipline/Queries/GetDisciplineList/DisciplineListVm.cs new file mode 100644 index 0000000..3f5c835 --- /dev/null +++ b/Application/Cqrs/Discipline/Queries/GetDisciplineList/DisciplineListVm.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Discipline.Queries.GetDisciplineList; + +/// +/// Represents a view model containing multiple disciplines name. +/// +public class DisciplineListVm +{ + /// + /// The list of disciplines. + /// + public IList Disciplines { get; set; } = new List(); +} \ No newline at end of file diff --git a/Application/Cqrs/Discipline/Queries/GetDisciplineList/DisciplineLookupDto.cs b/Application/Cqrs/Discipline/Queries/GetDisciplineList/DisciplineLookupDto.cs new file mode 100644 index 0000000..0a090df --- /dev/null +++ b/Application/Cqrs/Discipline/Queries/GetDisciplineList/DisciplineLookupDto.cs @@ -0,0 +1,17 @@ +namespace Mirea.Api.DataAccess.Application.Cqrs.Discipline.Queries.GetDisciplineList; + +/// +/// Represents disciplines. +/// +public class DisciplineLookupDto +{ + /// + /// The unique identifier for the discipline. + /// + public int Id { get; set; } + + /// + /// The name of the discipline. + /// + public required string Name { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Discipline/Queries/GetDisciplineList/GetDisciplineListQuery.cs b/Application/Cqrs/Discipline/Queries/GetDisciplineList/GetDisciplineListQuery.cs new file mode 100644 index 0000000..f3b770b --- /dev/null +++ b/Application/Cqrs/Discipline/Queries/GetDisciplineList/GetDisciplineListQuery.cs @@ -0,0 +1,9 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Discipline.Queries.GetDisciplineList; + +public class GetDisciplineListQuery : IRequest +{ + public int? Page { get; set; } + public int? PageSize { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Discipline/Queries/GetDisciplineList/GetDisciplineListQueryHandler.cs b/Application/Cqrs/Discipline/Queries/GetDisciplineList/GetDisciplineListQueryHandler.cs new file mode 100644 index 0000000..c76ead2 --- /dev/null +++ b/Application/Cqrs/Discipline/Queries/GetDisciplineList/GetDisciplineListQueryHandler.cs @@ -0,0 +1,30 @@ +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.Discipline.Queries.GetDisciplineList; + +public class GetDisciplineListQueryHandler(IDisciplineDbContext dbContext) : IRequestHandler +{ + public async Task Handle(GetDisciplineListQuery request, CancellationToken cancellationToken) + { + var dtos = dbContext.Disciplines.OrderBy(d => d.Id).Select(d => new DisciplineLookupDto() + { + Id = d.Id, + Name = d.Name, + }); + + 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 DisciplineListVm + { + Disciplines = result + }; + } +} \ No newline at end of file