diff --git a/Application/Cqrs/Professor/Commands/CreateProfessor/CreateProfessorCommand.cs b/Application/Cqrs/Professor/Commands/CreateProfessor/CreateProfessorCommand.cs new file mode 100644 index 0000000..34d1e12 --- /dev/null +++ b/Application/Cqrs/Professor/Commands/CreateProfessor/CreateProfessorCommand.cs @@ -0,0 +1,9 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Commands.CreateProfessor; + +public class CreateProfessorCommand : IRequest +{ + public required string Name { get; set; } + public string? AltName { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Professor/Commands/CreateProfessor/CreateProfessorCommandHandler.cs b/Application/Cqrs/Professor/Commands/CreateProfessor/CreateProfessorCommandHandler.cs new file mode 100644 index 0000000..f0e1006 --- /dev/null +++ b/Application/Cqrs/Professor/Commands/CreateProfessor/CreateProfessorCommandHandler.cs @@ -0,0 +1,30 @@ +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.CreateProfessor; + +public class CreateProfessorCommandHandler(IProfessorDbContext dbContext) : IRequestHandler +{ + public async Task Handle(CreateProfessorCommand request, CancellationToken cancellationToken) + { + var entity = await dbContext.Professors.FirstOrDefaultAsync(p => p.Name == request.Name, cancellationToken: cancellationToken); + + if (entity != null) + throw new RecordExistException(typeof(Domain.Schedule.Professor), nameof(Domain.Schedule.Professor.Name), entity.Id); + + var professor = new Domain.Schedule.Professor() + { + Name = request.Name, + AltName = request.AltName + }; + + var result = await dbContext.Professors.AddAsync(professor, cancellationToken); + await dbContext.SaveChangesAsync(cancellationToken); + + return result.Entity.Id; + } +} \ No newline at end of file