MireaBackend/Application/Cqrs/Professor/Commands/UpdateProfessor/UpdateProfessorCommandHandler.cs

20 lines
906 B
C#

using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Mirea.Api.DataAccess.Application.Common.Exceptions;
using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule;
namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Commands.UpdateProfessor;
public class UpdateProfessorCommandHandler(IProfessorDbContext dbContext) : IRequestHandler<UpdateProfessorCommand>
{
public async Task Handle(UpdateProfessorCommand request, CancellationToken cancellationToken)
{
var entity = await dbContext.Professors.FirstOrDefaultAsync(p => p.Id == request.Id, cancellationToken: cancellationToken) ?? throw new NotFoundException(typeof(Domain.Schedule.Professor), nameof(Domain.Schedule.Professor.Id), request.Id);
entity.AltName = request.AltName;
await dbContext.SaveChangesAsync(cancellationToken);
}
}