Compare commits

..

4 Commits

Author SHA1 Message Date
2ccc476686 feat: add search professors by name
All checks were successful
Build and Deploy Docker Container / build-and-deploy (push) Successful in 2m14s
.NET Test Pipeline / build-and-test (push) Successful in 2m41s
2024-10-25 04:44:18 +03:00
84d7b095f0 fix: return altName 2024-10-25 04:43:30 +03:00
4605c81895 docs: add some specification 2024-10-25 04:43:18 +03:00
0788c36bd2 style: remove "id" from text 2024-10-25 04:42:27 +03:00
8 changed files with 120 additions and 5 deletions

View File

@ -2,6 +2,7 @@
using MediatR; using MediatR;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorDetails; using Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorDetails;
using Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorDetailsBySearch;
using Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorList; using Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorList;
using Mirea.Api.Dto.Responses; using Mirea.Api.Dto.Responses;
using Mirea.Api.Endpoint.Common.Attributes; using Mirea.Api.Endpoint.Common.Attributes;
@ -63,4 +64,35 @@ public class ProfessorController(IMediator mediator) : BaseController
AltName = result.AltName AltName = result.AltName
}); });
} }
/// <summary>
/// Retrieves detailed information about professors based on their name.
/// </summary>
/// <remarks>
/// This method searches for professors whose name matches the provided search term.
/// </remarks>
/// <param name="name">The name of the professor to search for. Must be at least 4 characters long.</param>
/// <returns>
/// A list of <see cref="ProfessorResponse"/> objects containing the details of the matching professors.
/// </returns>
[HttpGet("{name:required}")]
[BadRequestResponse]
[NotFoundResponse]
public async Task<ActionResult<List<ProfessorResponse>>> GetDetails(string name)
{
if (string.IsNullOrEmpty(name) || name.Length < 4)
return BadRequest($"The minimum number of characters is 4 (current: {name.Length}).");
var result = await mediator.Send(new GetProfessorInfoSearchQuery()
{
Name = name
});
return Ok(result.Details.Select(x => new ProfessorResponse()
{
Id = x.Id,
Name = x.Name,
AltName = x.AltName
}));
}
} }

View File

@ -21,10 +21,18 @@ namespace Mirea.Api.Endpoint.Controllers.V1;
[CacheMaxAge(true)] [CacheMaxAge(true)]
public class ScheduleController(IMediator mediator, IOptionsSnapshot<GeneralConfig> config) : BaseController public class ScheduleController(IMediator mediator, IOptionsSnapshot<GeneralConfig> config) : BaseController
{ {
/// <summary>
/// Retrieves the start term for the schedule.
/// </summary>
/// <returns>The start term as a <see cref="DateOnly"/> value.</returns>
[CacheMaxAge(1, 0)] [CacheMaxAge(1, 0)]
[HttpGet("StartTerm")] [HttpGet("StartTerm")]
public ActionResult<DateOnly> GetStartTerm() => config.Value.ScheduleSettings!.StartTerm; public ActionResult<DateOnly> GetStartTerm() => config.Value.ScheduleSettings!.StartTerm;
/// <summary>
/// Retrieves the pair periods.
/// </summary>
/// <returns>A dictionary of pair periods, where the key is an integer identifier and the value is a <see cref="PairPeriodTime"/> object.</returns>
[CacheMaxAge(1, 0)] [CacheMaxAge(1, 0)]
[HttpGet("PairPeriod")] [HttpGet("PairPeriod")]
public ActionResult<Dictionary<int, PairPeriodTime>> GetPairPeriod() => config.Value.ScheduleSettings!.PairPeriod.ConvertToDto(); public ActionResult<Dictionary<int, PairPeriodTime>> GetPairPeriod() => config.Value.ScheduleSettings!.PairPeriod.ConvertToDto();

View File

@ -5,6 +5,6 @@ namespace Mirea.Api.DataAccess.Application.Common.Exceptions;
public class NotFoundException : Exception public class NotFoundException : Exception
{ {
public NotFoundException(MemberInfo entity, string name, object id) : base($"The entity \"{entity.Name}\" property \"{name}\" was not found by id \"{id}\".") { } public NotFoundException(MemberInfo entity, string name, object id) : base($"The entity \"{entity.Name}\" property \"{name}\" was not found by \"{id}\".") { }
public NotFoundException(MemberInfo entity, object id) : base($"The entity \"{entity.Name}\" was not found by id \"{id}\".") { } public NotFoundException(MemberInfo entity, object id) : base($"The entity \"{entity.Name}\" was not found by \"{id}\".") { }
} }

View File

@ -2,7 +2,16 @@
namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorDetails; namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorDetails;
/// <summary>
/// Represents a query to retrieve information about a professor.
/// </summary>
/// <remarks>
/// This query can be used to fetch details of a professor by either their unique identifier.
/// </remarks>
public class GetProfessorInfoQuery : IRequest<ProfessorInfoVm> public class GetProfessorInfoQuery : IRequest<ProfessorInfoVm>
{ {
/// <summary>
/// The unique identifier for the professor.
/// </summary>
public required int Id { get; set; } public required int Id { get; set; }
} }

View File

@ -11,12 +11,13 @@ public class GetProfessorInfoQueryHandler(IProfessorDbContext dbContext) : IRequ
{ {
public async Task<ProfessorInfoVm> Handle(GetProfessorInfoQuery request, CancellationToken cancellationToken) public async Task<ProfessorInfoVm> Handle(GetProfessorInfoQuery request, CancellationToken cancellationToken)
{ {
var discipline = await dbContext.Professors.FirstOrDefaultAsync(p => p.Id == request.Id, cancellationToken) ?? throw new NotFoundException(typeof(Domain.Schedule.Professor), request.Id); var professor = await dbContext.Professors.FirstOrDefaultAsync(p => p.Id == request.Id, cancellationToken) ?? throw new NotFoundException(typeof(Domain.Schedule.Professor), request.Id);
return new ProfessorInfoVm() return new ProfessorInfoVm()
{ {
Id = discipline.Id, Id = professor.Id,
Name = discipline.Name, Name = professor.Name,
AltName = professor.AltName
}; };
} }
} }

View File

@ -0,0 +1,17 @@
using MediatR;
namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorDetailsBySearch;
/// <summary>
/// Represents a query to retrieve information about a professor.
/// </summary>
/// <remarks>
/// This query can be used to fetch details of a professor by either their name.
/// </remarks>
public class GetProfessorInfoSearchQuery : IRequest<ProfessorInfoListVm>
{
/// <summary>
/// The name of the professor.
/// </summary>
public required string Name { get; set; }
}

View File

@ -0,0 +1,33 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using Mirea.Api.DataAccess.Application.Common.Exceptions;
using Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorDetails;
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.GetProfessorDetailsBySearch;
public class GetProfessorInfoSearchQueryHandler(IProfessorDbContext dbContext) : IRequestHandler<GetProfessorInfoSearchQuery, ProfessorInfoListVm>
{
public async Task<ProfessorInfoListVm> Handle(GetProfessorInfoSearchQuery request, CancellationToken cancellationToken)
{
var name = request.Name.ToLower();
var professor = await dbContext.Professors
.Where(p => p.Name.ToLower().Contains(name) ||
(p.AltName != null && p.AltName.ToLower().Contains(name)))
.ToListAsync(cancellationToken)
?? throw new NotFoundException(typeof(Domain.Schedule.Professor), request.Name);
return new ProfessorInfoListVm()
{
Details = professor.Select(x => new ProfessorInfoVm()
{
Id = x.Id,
Name = x.Name,
AltName = x.AltName
}).ToList()
};
}
}

View File

@ -0,0 +1,15 @@
using Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorDetails;
using System.Collections.Generic;
namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorDetailsBySearch;
/// <summary>
/// Represents professors.
/// </summary>
public class ProfessorInfoListVm
{
/// <summary>
/// List of <see cref="ProfessorInfoVm"/>
/// </summary>
public required IList<ProfessorInfoVm> Details { get; set; }
}