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