From dbc8f7c68cb61d4de6aeb50c6212e8b60ce2efa4 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sun, 21 Jan 2024 23:17:10 +0300 Subject: [PATCH] feat: add a professor update --- .../UpdateProfessorCommand.cs | 9 +++++++++ .../UpdateProfessorCommandHandler.cs | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 Application/Cqrs/Professor/Commands/UpdateTypeOfOccupation/UpdateProfessorCommand.cs create mode 100644 Application/Cqrs/Professor/Commands/UpdateTypeOfOccupation/UpdateProfessorCommandHandler.cs diff --git a/Application/Cqrs/Professor/Commands/UpdateTypeOfOccupation/UpdateProfessorCommand.cs b/Application/Cqrs/Professor/Commands/UpdateTypeOfOccupation/UpdateProfessorCommand.cs new file mode 100644 index 0000000..0029701 --- /dev/null +++ b/Application/Cqrs/Professor/Commands/UpdateTypeOfOccupation/UpdateProfessorCommand.cs @@ -0,0 +1,9 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Commands.UpdateTypeOfOccupation; + +public class UpdateProfessorCommand : IRequest +{ + public required int Id { get; set; } + public required string AltName { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Professor/Commands/UpdateTypeOfOccupation/UpdateProfessorCommandHandler.cs b/Application/Cqrs/Professor/Commands/UpdateTypeOfOccupation/UpdateProfessorCommandHandler.cs new file mode 100644 index 0000000..1d04355 --- /dev/null +++ b/Application/Cqrs/Professor/Commands/UpdateTypeOfOccupation/UpdateProfessorCommandHandler.cs @@ -0,0 +1,20 @@ +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.Professor.Commands.UpdateTypeOfOccupation; + +public class UpdateProfessorCommandHandler(IProfessorDbContext dbContext) : IRequestHandler +{ + 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); + } +} \ No newline at end of file