feat: add search professors by name
This commit is contained in:
@ -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; }
|
||||
}
|
@ -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()
|
||||
};
|
||||
}
|
||||
}
|
@ -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; }
|
||||
}
|
Reference in New Issue
Block a user