From c8c9b7f4560994f2f6129211674b8d2729ddaba9 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Fri, 16 Feb 2024 18:06:22 +0300 Subject: [PATCH] feat: add queries for professor --- .../GetProfessorInfoQuery.cs | 8 +++++ .../GetProfessorInfoQueryHandler.cs | 22 +++++++++++++ .../GetProfessorDetails/ProfessorInfoVm.cs | 22 +++++++++++++ .../GetProfessorList/GetProfessorListQuery.cs | 9 ++++++ .../GetProfessorListQueryHandler.cs | 31 +++++++++++++++++++ .../GetProfessorList/ProfessorListVm.cs | 14 +++++++++ .../GetProfessorList/ProfessorLookupDto.cs | 22 +++++++++++++ 7 files changed, 128 insertions(+) create mode 100644 Application/Cqrs/Professor/Queries/GetProfessorDetails/GetProfessorInfoQuery.cs create mode 100644 Application/Cqrs/Professor/Queries/GetProfessorDetails/GetProfessorInfoQueryHandler.cs create mode 100644 Application/Cqrs/Professor/Queries/GetProfessorDetails/ProfessorInfoVm.cs create mode 100644 Application/Cqrs/Professor/Queries/GetProfessorList/GetProfessorListQuery.cs create mode 100644 Application/Cqrs/Professor/Queries/GetProfessorList/GetProfessorListQueryHandler.cs create mode 100644 Application/Cqrs/Professor/Queries/GetProfessorList/ProfessorListVm.cs create mode 100644 Application/Cqrs/Professor/Queries/GetProfessorList/ProfessorLookupDto.cs diff --git a/Application/Cqrs/Professor/Queries/GetProfessorDetails/GetProfessorInfoQuery.cs b/Application/Cqrs/Professor/Queries/GetProfessorDetails/GetProfessorInfoQuery.cs new file mode 100644 index 0000000..93f1aa8 --- /dev/null +++ b/Application/Cqrs/Professor/Queries/GetProfessorDetails/GetProfessorInfoQuery.cs @@ -0,0 +1,8 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorDetails; + +public class GetProfessorInfoQuery : IRequest +{ + public required int Id { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Professor/Queries/GetProfessorDetails/GetProfessorInfoQueryHandler.cs b/Application/Cqrs/Professor/Queries/GetProfessorDetails/GetProfessorInfoQueryHandler.cs new file mode 100644 index 0000000..411a68d --- /dev/null +++ b/Application/Cqrs/Professor/Queries/GetProfessorDetails/GetProfessorInfoQueryHandler.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.Professor.Queries.GetProfessorDetails; + +public class GetProfessorInfoQueryHandler(IProfessorDbContext dbContext) : IRequestHandler +{ + public async Task Handle(GetProfessorInfoQuery request, CancellationToken cancellationToken) + { + var discipline = await dbContext.Professors.FirstOrDefaultAsync(p => p.Id == request.Id, cancellationToken) ?? throw new NotFoundException(typeof(Domain.Schedule.Group), request.Id); + + return new ProfessorInfoVm() + { + Id = discipline.Id, + Name = discipline.Name, + }; + } +} \ No newline at end of file diff --git a/Application/Cqrs/Professor/Queries/GetProfessorDetails/ProfessorInfoVm.cs b/Application/Cqrs/Professor/Queries/GetProfessorDetails/ProfessorInfoVm.cs new file mode 100644 index 0000000..691ef9b --- /dev/null +++ b/Application/Cqrs/Professor/Queries/GetProfessorDetails/ProfessorInfoVm.cs @@ -0,0 +1,22 @@ +namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorDetails; + +/// +/// Represents professors. +/// +public class ProfessorInfoVm +{ + /// + /// The unique identifier for the professor. + /// + public int Id { get; set; } + + /// + /// The name of the professor. + /// + public required string Name { get; set; } + + /// + /// The alt name of the professor. Usually is full name. + /// + public string? AltName { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Professor/Queries/GetProfessorList/GetProfessorListQuery.cs b/Application/Cqrs/Professor/Queries/GetProfessorList/GetProfessorListQuery.cs new file mode 100644 index 0000000..1a2e14b --- /dev/null +++ b/Application/Cqrs/Professor/Queries/GetProfessorList/GetProfessorListQuery.cs @@ -0,0 +1,9 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorList; + +public class GetProfessorListQuery : IRequest +{ + public int? Page { get; set; } + public int? PageSize { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Professor/Queries/GetProfessorList/GetProfessorListQueryHandler.cs b/Application/Cqrs/Professor/Queries/GetProfessorList/GetProfessorListQueryHandler.cs new file mode 100644 index 0000000..adcff57 --- /dev/null +++ b/Application/Cqrs/Professor/Queries/GetProfessorList/GetProfessorListQueryHandler.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.Professor.Queries.GetProfessorList; + +public class GetProfessorListQueryHandler(IProfessorDbContext dbContext) : IRequestHandler +{ + public async Task Handle(GetProfessorListQuery request, CancellationToken cancellationToken) + { + var dtos = dbContext.Professors.OrderBy(p => p.Id).Select(p => new ProfessorLookupDto() + { + Id = p.Id, + Name = p.Name, + AltName = p.AltName + }); + + 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 ProfessorListVm + { + Professors = result + }; + } +} \ No newline at end of file diff --git a/Application/Cqrs/Professor/Queries/GetProfessorList/ProfessorListVm.cs b/Application/Cqrs/Professor/Queries/GetProfessorList/ProfessorListVm.cs new file mode 100644 index 0000000..f3bf0f2 --- /dev/null +++ b/Application/Cqrs/Professor/Queries/GetProfessorList/ProfessorListVm.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorList; + +/// +/// Represents a view model containing multiple professors name. +/// +public class ProfessorListVm +{ + /// + /// The list of professors. + /// + public IList Professors { get; set; } = new List(); +} \ No newline at end of file diff --git a/Application/Cqrs/Professor/Queries/GetProfessorList/ProfessorLookupDto.cs b/Application/Cqrs/Professor/Queries/GetProfessorList/ProfessorLookupDto.cs new file mode 100644 index 0000000..4e32f26 --- /dev/null +++ b/Application/Cqrs/Professor/Queries/GetProfessorList/ProfessorLookupDto.cs @@ -0,0 +1,22 @@ +namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorList; + +/// +/// Represents professor. +/// +public class ProfessorLookupDto +{ + /// + /// The unique identifier for the professor. + /// + public int Id { get; set; } + + /// + /// The name of the professor. + /// + public required string Name { get; set; } + + /// + /// The alt name of the professor. Usually is full name. + /// + public string? AltName { get; set; } +} \ No newline at end of file