feat: add a professor create
This commit is contained in:
parent
9b0e5a3f15
commit
654065f016
@ -0,0 +1,9 @@
|
||||
using MediatR;
|
||||
|
||||
namespace Mirea.Api.DataAccess.Application.Cqrs.Professor.Commands.CreateProfessor;
|
||||
|
||||
public class CreateProfessorCommand : IRequest<int>
|
||||
{
|
||||
public required string Name { get; set; }
|
||||
public string? AltName { get; set; }
|
||||
}
|
@ -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<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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user