From bc99007d9487a614a97d5c99d52fa3d76f1437b8 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sun, 28 Jan 2024 04:31:18 +0300 Subject: [PATCH] feat: add queries for the campus --- .../CampusBasicInfoDto.cs | 22 +++++++++++++++ .../CampusBasicInfoVm.cs | 14 ++++++++++ .../GetCampusBasicInfoListQuery.cs | 5 ++++ .../GetCampusBasicInfoListQueryHandler.cs | 26 ++++++++++++++++++ .../GetCampusDetails/CampusDetailsVm.cs | 27 +++++++++++++++++++ .../GetCampusDetails/GetCampusDetailsQuery.cs | 9 +++++++ .../GetCampusDetailsQueryHandler.cs | 27 +++++++++++++++++++ 7 files changed, 130 insertions(+) create mode 100644 Application/Cqrs/Campus/Queries/GetCampusBasicInfoList/CampusBasicInfoDto.cs create mode 100644 Application/Cqrs/Campus/Queries/GetCampusBasicInfoList/CampusBasicInfoVm.cs create mode 100644 Application/Cqrs/Campus/Queries/GetCampusBasicInfoList/GetCampusBasicInfoListQuery.cs create mode 100644 Application/Cqrs/Campus/Queries/GetCampusBasicInfoList/GetCampusBasicInfoListQueryHandler.cs create mode 100644 Application/Cqrs/Campus/Queries/GetCampusDetails/CampusDetailsVm.cs create mode 100644 Application/Cqrs/Campus/Queries/GetCampusDetails/GetCampusDetailsQuery.cs create mode 100644 Application/Cqrs/Campus/Queries/GetCampusDetails/GetCampusDetailsQueryHandler.cs diff --git a/Application/Cqrs/Campus/Queries/GetCampusBasicInfoList/CampusBasicInfoDto.cs b/Application/Cqrs/Campus/Queries/GetCampusBasicInfoList/CampusBasicInfoDto.cs new file mode 100644 index 0000000..e0c9918 --- /dev/null +++ b/Application/Cqrs/Campus/Queries/GetCampusBasicInfoList/CampusBasicInfoDto.cs @@ -0,0 +1,22 @@ +namespace Mirea.Api.DataAccess.Application.Cqrs.Campus.Queries.GetCampusBasicInfoList; + +/// +/// Represents basic information about a campus. +/// +public class CampusBasicInfoDto +{ + /// + /// The unique identifier for the campus. + /// + public int Id { get; set; } + + /// + /// The code name associated with the campus. + /// + public required string CodeName { get; set; } + + /// + /// The full name of the campus. + /// + public string? FullName { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Campus/Queries/GetCampusBasicInfoList/CampusBasicInfoVm.cs b/Application/Cqrs/Campus/Queries/GetCampusBasicInfoList/CampusBasicInfoVm.cs new file mode 100644 index 0000000..a3a5f2d --- /dev/null +++ b/Application/Cqrs/Campus/Queries/GetCampusBasicInfoList/CampusBasicInfoVm.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Campus.Queries.GetCampusBasicInfoList; + +/// +/// Represents a view model containing basic information about multiple campuses. +/// +public class CampusBasicInfoVm +{ + /// + /// The list of campus basic information. + /// + public IList Campuses { get; set; } = new List(); +} \ No newline at end of file diff --git a/Application/Cqrs/Campus/Queries/GetCampusBasicInfoList/GetCampusBasicInfoListQuery.cs b/Application/Cqrs/Campus/Queries/GetCampusBasicInfoList/GetCampusBasicInfoListQuery.cs new file mode 100644 index 0000000..2059735 --- /dev/null +++ b/Application/Cqrs/Campus/Queries/GetCampusBasicInfoList/GetCampusBasicInfoListQuery.cs @@ -0,0 +1,5 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Campus.Queries.GetCampusBasicInfoList; + +public class GetCampusBasicInfoListQuery : IRequest; \ No newline at end of file diff --git a/Application/Cqrs/Campus/Queries/GetCampusBasicInfoList/GetCampusBasicInfoListQueryHandler.cs b/Application/Cqrs/Campus/Queries/GetCampusBasicInfoList/GetCampusBasicInfoListQueryHandler.cs new file mode 100644 index 0000000..3cd6259 --- /dev/null +++ b/Application/Cqrs/Campus/Queries/GetCampusBasicInfoList/GetCampusBasicInfoListQueryHandler.cs @@ -0,0 +1,26 @@ +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.Campus.Queries.GetCampusBasicInfoList; + +public class GetCampusBasicInfoListQueryHandler(ICampusDbContext dbContext) : IRequestHandler +{ + public async Task Handle(GetCampusBasicInfoListQuery request, CancellationToken cancellationToken) + { + var dtos = await dbContext.Campuses.Select(c => new CampusBasicInfoDto() + { + Id = c.Id, + CodeName = c.CodeName, + FullName = c.FullName + }).ToListAsync(cancellationToken); + + return new CampusBasicInfoVm + { + Campuses = dtos + }; + } +} \ No newline at end of file diff --git a/Application/Cqrs/Campus/Queries/GetCampusDetails/CampusDetailsVm.cs b/Application/Cqrs/Campus/Queries/GetCampusDetails/CampusDetailsVm.cs new file mode 100644 index 0000000..35246c1 --- /dev/null +++ b/Application/Cqrs/Campus/Queries/GetCampusDetails/CampusDetailsVm.cs @@ -0,0 +1,27 @@ +namespace Mirea.Api.DataAccess.Application.Cqrs.Campus.Queries.GetCampusDetails; + +/// +/// Represents a view model containing detailed information about a campus. +/// +public class CampusDetailsVm +{ + /// + /// The unique identifier for the campus. + /// + public int Id { get; set; } + + /// + /// The code name associated with the campus. + /// + public required string CodeName { get; set; } + + /// + /// The full name of the campus. + /// + public string? FullName { get; set; } + + /// + /// The address of the campus. + /// + public string? Address { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Campus/Queries/GetCampusDetails/GetCampusDetailsQuery.cs b/Application/Cqrs/Campus/Queries/GetCampusDetails/GetCampusDetailsQuery.cs new file mode 100644 index 0000000..f64bf8b --- /dev/null +++ b/Application/Cqrs/Campus/Queries/GetCampusDetails/GetCampusDetailsQuery.cs @@ -0,0 +1,9 @@ +using MediatR; +using Mirea.Api.DataAccess.Application.Cqrs.Campus.Queries.GetCampusBasicInfoList; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Campus.Queries.GetCampusDetails; + +public class GetCampusDetailsQuery : IRequest +{ + public required int Id { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Campus/Queries/GetCampusDetails/GetCampusDetailsQueryHandler.cs b/Application/Cqrs/Campus/Queries/GetCampusDetails/GetCampusDetailsQueryHandler.cs new file mode 100644 index 0000000..2cb791c --- /dev/null +++ b/Application/Cqrs/Campus/Queries/GetCampusDetails/GetCampusDetailsQueryHandler.cs @@ -0,0 +1,27 @@ +using System.Globalization; +using System.Linq; +using MediatR; +using Mirea.Api.DataAccess.Application.Cqrs.Campus.Queries.GetCampusBasicInfoList; +using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; +using System.Threading.Tasks; +using System.Threading; +using Microsoft.EntityFrameworkCore; +using Mirea.Api.DataAccess.Application.Common.Exceptions; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Campus.Queries.GetCampusDetails; + +public class GetCampusDetailsQueryHandler(ICampusDbContext dbContext) : IRequestHandler +{ + public async Task Handle(GetCampusDetailsQuery request, CancellationToken cancellationToken) + { + var campus = await dbContext.Campuses.FirstOrDefaultAsync(c => c.Id == request.Id, cancellationToken) ?? throw new NotFoundException(typeof(Domain.Schedule.Campus), request.Id); + + return new CampusDetailsVm() + { + Id = campus.Id, + CodeName = campus.CodeName, + FullName = campus.FullName, + Address = campus.Address + }; + } +} \ No newline at end of file