From ce3bd2367379d98e8c329eb0185a9f68bb82679a Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Fri, 16 Feb 2024 22:40:21 +0300 Subject: [PATCH] feat: add queries for faculty --- .../GetFacultyDetails/FacultyInfoVm.cs | 32 +++++++++++++++++++ .../GetFacultyDetails/GetFacultyInfoQuery.cs | 8 +++++ .../GetFacultyInfoQueryHandler.cs | 27 ++++++++++++++++ .../Queries/GetFacultyList/FacultyListVm.cs | 14 ++++++++ .../GetFacultyList/FacultyLookupDto.cs | 22 +++++++++++++ .../GetFacultyList/GetFacultyListQuery.cs | 9 ++++++ .../GetFacultyListQueryHandler.cs | 31 ++++++++++++++++++ 7 files changed, 143 insertions(+) create mode 100644 Application/Cqrs/Faculty/Queries/GetFacultyDetails/FacultyInfoVm.cs create mode 100644 Application/Cqrs/Faculty/Queries/GetFacultyDetails/GetFacultyInfoQuery.cs create mode 100644 Application/Cqrs/Faculty/Queries/GetFacultyDetails/GetFacultyInfoQueryHandler.cs create mode 100644 Application/Cqrs/Faculty/Queries/GetFacultyList/FacultyListVm.cs create mode 100644 Application/Cqrs/Faculty/Queries/GetFacultyList/FacultyLookupDto.cs create mode 100644 Application/Cqrs/Faculty/Queries/GetFacultyList/GetFacultyListQuery.cs create mode 100644 Application/Cqrs/Faculty/Queries/GetFacultyList/GetFacultyListQueryHandler.cs diff --git a/Application/Cqrs/Faculty/Queries/GetFacultyDetails/FacultyInfoVm.cs b/Application/Cqrs/Faculty/Queries/GetFacultyDetails/FacultyInfoVm.cs new file mode 100644 index 0000000..f7fb805 --- /dev/null +++ b/Application/Cqrs/Faculty/Queries/GetFacultyDetails/FacultyInfoVm.cs @@ -0,0 +1,32 @@ +namespace Mirea.Api.DataAccess.Application.Cqrs.Faculty.Queries.GetFacultyDetails; + +/// +/// Represents faculties. +/// +public class FacultyInfoVm +{ + /// + /// The unique identifier for the faculty. + /// + public int Id { get; set; } + + /// + /// The name of the faculty. + /// + public required string Name { get; set; } + + /// + /// ID indicating the faculty's affiliation to the campus. + /// + public int? CampusId { get; set; } + + /// + /// Campus name indicating the faculty's affiliation to the campus. + /// + public string? CampusName { get; set; } + + /// + /// Campus code indicating the faculty's affiliation to the campus. + /// + public string? CampusCode { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Faculty/Queries/GetFacultyDetails/GetFacultyInfoQuery.cs b/Application/Cqrs/Faculty/Queries/GetFacultyDetails/GetFacultyInfoQuery.cs new file mode 100644 index 0000000..2517b14 --- /dev/null +++ b/Application/Cqrs/Faculty/Queries/GetFacultyDetails/GetFacultyInfoQuery.cs @@ -0,0 +1,8 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Faculty.Queries.GetFacultyDetails; + +public class GetFacultyInfoQuery : IRequest +{ + public required int Id { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Faculty/Queries/GetFacultyDetails/GetFacultyInfoQueryHandler.cs b/Application/Cqrs/Faculty/Queries/GetFacultyDetails/GetFacultyInfoQueryHandler.cs new file mode 100644 index 0000000..a2f6486 --- /dev/null +++ b/Application/Cqrs/Faculty/Queries/GetFacultyDetails/GetFacultyInfoQueryHandler.cs @@ -0,0 +1,27 @@ +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.Faculty.Queries.GetFacultyDetails; + +public class GetFacultyInfoQueryHandler(IFacultyDbContext dbContext) : IRequestHandler +{ + public async Task Handle(GetFacultyInfoQuery request, CancellationToken cancellationToken) + { + var faculty = await dbContext.Faculties + .Include(f => f.Campus) + .FirstOrDefaultAsync(f => f.Id == request.Id, cancellationToken) ?? throw new NotFoundException(typeof(Domain.Schedule.Faculty), request.Id); + + return new FacultyInfoVm() + { + Id = faculty.Id, + Name = faculty.Name, + CampusId = faculty.CampusId, + CampusName = faculty.Campus?.FullName, + CampusCode = faculty.Campus?.CodeName + }; + } +} \ No newline at end of file diff --git a/Application/Cqrs/Faculty/Queries/GetFacultyList/FacultyListVm.cs b/Application/Cqrs/Faculty/Queries/GetFacultyList/FacultyListVm.cs new file mode 100644 index 0000000..8ad7d4b --- /dev/null +++ b/Application/Cqrs/Faculty/Queries/GetFacultyList/FacultyListVm.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Faculty.Queries.GetFacultyList; + +/// +/// Represents a view model containing multiple faculties. +/// +public class FacultyListVm +{ + /// + /// The list of faculties. + /// + public IList Faculties { get; set; } = new List(); +} \ No newline at end of file diff --git a/Application/Cqrs/Faculty/Queries/GetFacultyList/FacultyLookupDto.cs b/Application/Cqrs/Faculty/Queries/GetFacultyList/FacultyLookupDto.cs new file mode 100644 index 0000000..7a8c615 --- /dev/null +++ b/Application/Cqrs/Faculty/Queries/GetFacultyList/FacultyLookupDto.cs @@ -0,0 +1,22 @@ +namespace Mirea.Api.DataAccess.Application.Cqrs.Faculty.Queries.GetFacultyList; + +/// +/// Represents faculties. +/// +public class FacultyLookupDto +{ + /// + /// The unique identifier for the faculty. + /// + public int Id { get; set; } + + /// + /// The name of the faculty. + /// + public required string Name { get; set; } + + /// + /// ID indicating the faculty's affiliation to the campus. + /// + public int? CampusId { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Faculty/Queries/GetFacultyList/GetFacultyListQuery.cs b/Application/Cqrs/Faculty/Queries/GetFacultyList/GetFacultyListQuery.cs new file mode 100644 index 0000000..9abe675 --- /dev/null +++ b/Application/Cqrs/Faculty/Queries/GetFacultyList/GetFacultyListQuery.cs @@ -0,0 +1,9 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Faculty.Queries.GetFacultyList; + +public class GetFacultyListQuery : IRequest +{ + public int? Page { get; set; } + public int? PageSize { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Faculty/Queries/GetFacultyList/GetFacultyListQueryHandler.cs b/Application/Cqrs/Faculty/Queries/GetFacultyList/GetFacultyListQueryHandler.cs new file mode 100644 index 0000000..1249fe7 --- /dev/null +++ b/Application/Cqrs/Faculty/Queries/GetFacultyList/GetFacultyListQueryHandler.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.Faculty.Queries.GetFacultyList; + +public class GetFacultyListQueryHandler(IFacultyDbContext dbContext) : IRequestHandler +{ + public async Task Handle(GetFacultyListQuery request, CancellationToken cancellationToken) + { + var dtos = dbContext.Faculties.OrderBy(f => f.Id).Select(f => new FacultyLookupDto() + { + Id = f.Id, + Name = f.Name, + CampusId = f.CampusId + }); + + 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 FacultyListVm + { + Faculties = result + }; + } +} \ No newline at end of file