From a00dc8ccd6fc1db6ab84fc4ab3b22d02eb2a4f37 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Fri, 16 Feb 2024 22:51:26 +0300 Subject: [PATCH] feat: add queries for lecture hall --- .../GetLectureHallInfoQuery.cs | 8 +++++ .../GetLectureHallInfoQueryHandler.cs | 28 ++++++++++++++++ .../LectureHallInfoVm.cs | 32 +++++++++++++++++++ .../GetLectureHallListQuery.cs | 9 ++++++ .../GetLectureHallListQueryHandler.cs | 31 ++++++++++++++++++ .../GetLectureHallList/LectureHallListVm.cs | 14 ++++++++ .../LectureHallLookupDto.cs | 22 +++++++++++++ 7 files changed, 144 insertions(+) create mode 100644 Application/Cqrs/LectureHall/Queries/GetLectureHallDetails/GetLectureHallInfoQuery.cs create mode 100644 Application/Cqrs/LectureHall/Queries/GetLectureHallDetails/GetLectureHallInfoQueryHandler.cs create mode 100644 Application/Cqrs/LectureHall/Queries/GetLectureHallDetails/LectureHallInfoVm.cs create mode 100644 Application/Cqrs/LectureHall/Queries/GetLectureHallList/GetLectureHallListQuery.cs create mode 100644 Application/Cqrs/LectureHall/Queries/GetLectureHallList/GetLectureHallListQueryHandler.cs create mode 100644 Application/Cqrs/LectureHall/Queries/GetLectureHallList/LectureHallListVm.cs create mode 100644 Application/Cqrs/LectureHall/Queries/GetLectureHallList/LectureHallLookupDto.cs diff --git a/Application/Cqrs/LectureHall/Queries/GetLectureHallDetails/GetLectureHallInfoQuery.cs b/Application/Cqrs/LectureHall/Queries/GetLectureHallDetails/GetLectureHallInfoQuery.cs new file mode 100644 index 0000000..284f218 --- /dev/null +++ b/Application/Cqrs/LectureHall/Queries/GetLectureHallDetails/GetLectureHallInfoQuery.cs @@ -0,0 +1,8 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.LectureHall.Queries.GetLectureHallDetails; + +public class GetLectureHallInfoQuery : IRequest +{ + public required int Id { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/LectureHall/Queries/GetLectureHallDetails/GetLectureHallInfoQueryHandler.cs b/Application/Cqrs/LectureHall/Queries/GetLectureHallDetails/GetLectureHallInfoQueryHandler.cs new file mode 100644 index 0000000..17027a6 --- /dev/null +++ b/Application/Cqrs/LectureHall/Queries/GetLectureHallDetails/GetLectureHallInfoQueryHandler.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.LectureHall.Queries.GetLectureHallDetails; + +public class GetLectureHallInfoQueryHandler(ILectureHallDbContext dbContext) : IRequestHandler +{ + public async Task Handle(GetLectureHallInfoQuery request, CancellationToken cancellationToken) + { + var lectureHall = await dbContext.LectureHalls + .Include(l => l.Campus) + .FirstOrDefaultAsync(l => l.Id == request.Id, cancellationToken) + ?? throw new NotFoundException(typeof(Domain.Schedule.LectureHall), request.Id); + + return new LectureHallInfoVm() + { + Id = lectureHall.Id, + Name = lectureHall.Name, + CampusId = lectureHall.CampusId, + CampusCode = lectureHall.Campus?.CodeName, + CampusName = lectureHall.Campus?.FullName + }; + } +} \ No newline at end of file diff --git a/Application/Cqrs/LectureHall/Queries/GetLectureHallDetails/LectureHallInfoVm.cs b/Application/Cqrs/LectureHall/Queries/GetLectureHallDetails/LectureHallInfoVm.cs new file mode 100644 index 0000000..9b9c42f --- /dev/null +++ b/Application/Cqrs/LectureHall/Queries/GetLectureHallDetails/LectureHallInfoVm.cs @@ -0,0 +1,32 @@ +namespace Mirea.Api.DataAccess.Application.Cqrs.LectureHall.Queries.GetLectureHallDetails; + +/// +/// Represents disciplines. +/// +public class LectureHallInfoVm +{ + /// + /// The unique identifier for the lecture hall. + /// + public int Id { get; set; } + + /// + /// The name of the lecture hall. + /// + public required string Name { get; set; } + + /// + /// ID indicating the lecture hall's affiliation to the campus. + /// + public int CampusId { get; set; } + + /// + /// Campus name indicating the lecture hall's affiliation to the campus. + /// + public string? CampusName { get; set; } + + /// + /// Campus code indicating the lecture hall's affiliation to the campus. + /// + public string? CampusCode { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/LectureHall/Queries/GetLectureHallList/GetLectureHallListQuery.cs b/Application/Cqrs/LectureHall/Queries/GetLectureHallList/GetLectureHallListQuery.cs new file mode 100644 index 0000000..cb8ea83 --- /dev/null +++ b/Application/Cqrs/LectureHall/Queries/GetLectureHallList/GetLectureHallListQuery.cs @@ -0,0 +1,9 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.LectureHall.Queries.GetLectureHallList; + +public class GetLectureHallListQuery : IRequest +{ + public int? Page { get; set; } + public int? PageSize { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/LectureHall/Queries/GetLectureHallList/GetLectureHallListQueryHandler.cs b/Application/Cqrs/LectureHall/Queries/GetLectureHallList/GetLectureHallListQueryHandler.cs new file mode 100644 index 0000000..ac7b041 --- /dev/null +++ b/Application/Cqrs/LectureHall/Queries/GetLectureHallList/GetLectureHallListQueryHandler.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.LectureHall.Queries.GetLectureHallList; + +public class GetLectureHallListQueryHandler(ILectureHallDbContext dbContext) : IRequestHandler +{ + public async Task Handle(GetLectureHallListQuery request, CancellationToken cancellationToken) + { + var dtos = dbContext.LectureHalls.OrderBy(l => l.Id).Select(l => new LectureHallLookupDto() + { + Id = l.Id, + Name = l.Name, + CampusId = l.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 LectureHallListVm + { + LectureHalls = result + }; + } +} \ No newline at end of file diff --git a/Application/Cqrs/LectureHall/Queries/GetLectureHallList/LectureHallListVm.cs b/Application/Cqrs/LectureHall/Queries/GetLectureHallList/LectureHallListVm.cs new file mode 100644 index 0000000..8338f99 --- /dev/null +++ b/Application/Cqrs/LectureHall/Queries/GetLectureHallList/LectureHallListVm.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; + +namespace Mirea.Api.DataAccess.Application.Cqrs.LectureHall.Queries.GetLectureHallList; + +/// +/// Represents a view model containing multiple disciplines name. +/// +public class LectureHallListVm +{ + /// + /// The list of lecture hall. + /// + public IList LectureHalls { get; set; } = new List(); +} \ No newline at end of file diff --git a/Application/Cqrs/LectureHall/Queries/GetLectureHallList/LectureHallLookupDto.cs b/Application/Cqrs/LectureHall/Queries/GetLectureHallList/LectureHallLookupDto.cs new file mode 100644 index 0000000..f00083c --- /dev/null +++ b/Application/Cqrs/LectureHall/Queries/GetLectureHallList/LectureHallLookupDto.cs @@ -0,0 +1,22 @@ +namespace Mirea.Api.DataAccess.Application.Cqrs.LectureHall.Queries.GetLectureHallList; + +/// +/// Represents lecture halls. +/// +public class LectureHallLookupDto +{ + /// + /// The unique identifier for the lecture hall. + /// + public int Id { get; set; } + + /// + /// The name of the lecture hall. + /// + public required string Name { get; set; } + + /// + /// ID indicating the lecture hall's affiliation to the campus. + /// + public int CampusId { get; set; } +} \ No newline at end of file