From 67e9b89379211945c445462d01ac2b619dde2b1d Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Tue, 16 Jan 2024 12:52:35 +0300 Subject: [PATCH] feat: add a type of occupation create --- .../TypeOfOccupationCommand.cs | 9 ++++++ .../TypeOfOccupationCommandHandler.cs | 30 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 Application/Cqrs/TypeOfOccupation/Commands/CreateTypeOfOccupation/TypeOfOccupationCommand.cs create mode 100644 Application/Cqrs/TypeOfOccupation/Commands/CreateTypeOfOccupation/TypeOfOccupationCommandHandler.cs diff --git a/Application/Cqrs/TypeOfOccupation/Commands/CreateTypeOfOccupation/TypeOfOccupationCommand.cs b/Application/Cqrs/TypeOfOccupation/Commands/CreateTypeOfOccupation/TypeOfOccupationCommand.cs new file mode 100644 index 0000000..7aacbe9 --- /dev/null +++ b/Application/Cqrs/TypeOfOccupation/Commands/CreateTypeOfOccupation/TypeOfOccupationCommand.cs @@ -0,0 +1,9 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.TypeOfOccupation.Commands.CreateTypeOfOccupation; + +public class TypeOfOccupationCommand : IRequest +{ + public required string ShortName { get; set; } + public string? FullName { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/TypeOfOccupation/Commands/CreateTypeOfOccupation/TypeOfOccupationCommandHandler.cs b/Application/Cqrs/TypeOfOccupation/Commands/CreateTypeOfOccupation/TypeOfOccupationCommandHandler.cs new file mode 100644 index 0000000..b51507b --- /dev/null +++ b/Application/Cqrs/TypeOfOccupation/Commands/CreateTypeOfOccupation/TypeOfOccupationCommandHandler.cs @@ -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 +{ + public async Task 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; + } +} \ No newline at end of file