MireaBackend/Application/Cqrs/LectureHall/Queries/GetLectureHallDetails/GetLectureHallInfoQueryHandler.cs

28 lines
1.1 KiB
C#
Raw Normal View History

2024-02-16 22:51:26 +03:00
using MediatR;
using Microsoft.EntityFrameworkCore;
using Mirea.Api.DataAccess.Application.Common.Exceptions;
using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule;
using System.Threading;
using System.Threading.Tasks;
namespace Mirea.Api.DataAccess.Application.Cqrs.LectureHall.Queries.GetLectureHallDetails;
public class GetLectureHallInfoQueryHandler(ILectureHallDbContext dbContext) : IRequestHandler<GetLectureHallInfoQuery, LectureHallInfoVm>
{
public async Task<LectureHallInfoVm> Handle(GetLectureHallInfoQuery request, CancellationToken cancellationToken)
{
var lectureHall = await dbContext.LectureHalls
.Include(l => l.Campus)
.FirstOrDefaultAsync(l => l.Id == request.Id, cancellationToken)
?? throw new NotFoundException(typeof(Domain.Schedule.LectureHall), request.Id);
return new LectureHallInfoVm()
{
Id = lectureHall.Id,
Name = lectureHall.Name,
CampusId = lectureHall.CampusId,
CampusCode = lectureHall.Campus?.CodeName,
CampusName = lectureHall.Campus?.FullName
};
}
}