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

This commit is contained in:
Polianin Nikita 2024-10-25 04:44:18 +03:00
parent 84d7b095f0
commit 2ccc476686
4 changed files with 97 additions and 0 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

@ -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; }
}