feat: add a type of occupation create
All checks were successful
.NET Test Pipeline / build-and-test (pull_request) Successful in 2m35s

This commit is contained in:
Polianin Nikita 2024-01-16 12:52:35 +03:00
parent 6f5a662e2f
commit 67e9b89379
2 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,9 @@
using MediatR;
namespace Mirea.Api.DataAccess.Application.Cqrs.TypeOfOccupation.Commands.CreateTypeOfOccupation;
public class TypeOfOccupationCommand : IRequest<int>
{
public required string ShortName { get; set; }
public string? FullName { get; set; }
}

View File

@ -0,0 +1,30 @@
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.TypeOfOccupation.Commands.CreateTypeOfOccupation;
public class TypeOfOccupationCommandHandler(ITypeOfOccupationDbContext dbContext) : IRequestHandler<TypeOfOccupationCommand, int>
{
public async Task<int> Handle(TypeOfOccupationCommand request, CancellationToken cancellationToken)
{
var entity = await dbContext.TypeOfOccupations.FirstOrDefaultAsync(t => t.ShortName == request.ShortName, cancellationToken: cancellationToken);
if (entity != null)
throw new RecordExistException(typeof(Domain.Schedule.TypeOfOccupation), nameof(Domain.Schedule.TypeOfOccupation.ShortName), entity.Id);
var typeOfOccupation = new Domain.Schedule.TypeOfOccupation
{
ShortName = request.ShortName,
FullName = request.FullName
};
var result = await dbContext.TypeOfOccupations.AddAsync(typeOfOccupation, cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
return result.Entity.Id;
}
}