MireaBackend/Endpoint/Controllers/V1/ProfessorController.cs
Polianin Nikita 2ccc476686
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
feat: add search professors by name
2024-10-25 04:44:18 +03:00

98 lines
3.2 KiB
C#

using Asp.Versioning;
using MediatR;
using Microsoft.AspNetCore.Mvc;
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.Dto.Responses;
using Mirea.Api.Endpoint.Common.Attributes;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Mirea.Api.Endpoint.Controllers.V1;
[ApiVersion("1.0")]
[CacheMaxAge(true)]
public class ProfessorController(IMediator mediator) : BaseController
{
/// <summary>
/// Retrieves a list of professors.
/// </summary>
/// <param name="page">The page number for pagination (optional).</param>
/// <param name="pageSize">The page size for pagination (optional).</param>
/// <returns>A list of professors.</returns>
[HttpGet]
[BadRequestResponse]
public async Task<ActionResult<List<ProfessorResponse>>> Get([FromQuery] int? page, [FromQuery] int? pageSize)
{
var result = await mediator.Send(new GetProfessorListQuery()
{
Page = page,
PageSize = pageSize
});
return Ok(result.Professors
.Select(p => new ProfessorResponse()
{
Id = p.Id,
Name = p.Name,
AltName = p.AltName
})
);
}
/// <summary>
/// Retrieves detailed information about a specific professor.
/// </summary>
/// <param name="id">The ID of the professor to retrieve.</param>
/// <returns>Detailed information about the professor.</returns>
[HttpGet("{id:int}")]
[BadRequestResponse]
[NotFoundResponse]
public async Task<ActionResult<ProfessorResponse>> GetDetails(int id)
{
var result = await mediator.Send(new GetProfessorInfoQuery()
{
Id = id
});
return Ok(new ProfessorResponse()
{
Id = result.Id,
Name = result.Name,
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
}));
}
}