30 lines
1.2 KiB
C#
30 lines
1.2 KiB
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.CreateProfessor;
|
|
|
|
public class CreateProfessorCommandHandler(IProfessorDbContext dbContext) : IRequestHandler<CreateProfessorCommand, int>
|
|
{
|
|
public async Task<int> 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;
|
|
}
|
|
} |