diff --git a/Endpoint/Controllers/V1/ProfessorController.cs b/Endpoint/Controllers/V1/ProfessorController.cs index 8b1a58d..722fa8c 100644 --- a/Endpoint/Controllers/V1/ProfessorController.cs +++ b/Endpoint/Controllers/V1/ProfessorController.cs @@ -2,6 +2,7 @@ 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; @@ -63,4 +64,35 @@ public class ProfessorController(IMediator mediator) : BaseController AltName = result.AltName }); } + + /// + /// Retrieves detailed information about professors based on their name. + /// + /// + /// This method searches for professors whose name matches the provided search term. + /// + /// The name of the professor to search for. Must be at least 4 characters long. + /// + /// A list of objects containing the details of the matching professors. + /// + [HttpGet("{name:required}")] + [BadRequestResponse] + [NotFoundResponse] + public async Task>> 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 + })); + } } \ No newline at end of file diff --git a/SqlData/Application/Cqrs/Professor/Queries/GetProfessorDetailsBySearch/GetProfessorInfoSearchQuery.cs b/SqlData/Application/Cqrs/Professor/Queries/GetProfessorDetailsBySearch/GetProfessorInfoSearchQuery.cs new file mode 100644 index 0000000..afd52d1 --- /dev/null +++ b/SqlData/Application/Cqrs/Professor/Queries/GetProfessorDetailsBySearch/GetProfessorInfoSearchQuery.cs @@ -0,0 +1,17 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Queries.GetProfessorDetailsBySearch; + +/// +/// Represents a query to retrieve information about a professor. +/// +/// +/// This query can be used to fetch details of a professor by either their name. +/// +public class GetProfessorInfoSearchQuery : IRequest +{ + /// + /// The name of the professor. + /// + public required string Name { get; set; } +} \ No newline at end of file diff --git a/SqlData/Application/Cqrs/Professor/Queries/GetProfessorDetailsBySearch/GetProfessorInfoSearchQueryHandler.cs b/SqlData/Application/Cqrs/Professor/Queries/GetProfessorDetailsBySearch/GetProfessorInfoSearchQueryHandler.cs new file mode 100644 index 0000000..c466adc --- /dev/null +++ b/SqlData/Application/Cqrs/Professor/Queries/GetProfessorDetailsBySearch/GetProfessorInfoSearchQueryHandler.cs @@ -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 +{ + public async Task 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() + }; + } +} \ No newline at end of file diff --git a/SqlData/Application/Cqrs/Professor/Queries/GetProfessorDetailsBySearch/ProfessorInfoListVm.cs b/SqlData/Application/Cqrs/Professor/Queries/GetProfessorDetailsBySearch/ProfessorInfoListVm.cs new file mode 100644 index 0000000..9b2637f --- /dev/null +++ b/SqlData/Application/Cqrs/Professor/Queries/GetProfessorDetailsBySearch/ProfessorInfoListVm.cs @@ -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; + +/// +/// Represents professors. +/// +public class ProfessorInfoListVm +{ + /// + /// List of + /// + public required IList Details { get; set; } +} \ No newline at end of file