From 4fe25005affd82db6dd8a00e84139f9f4f51b8e4 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Thu, 25 Jan 2024 17:39:07 +0300 Subject: [PATCH] feat: add a faculty command --- .../CreateFaculty/CreateFacultyCommand.cs | 9 ++++++ .../CreateFacultyCommandHandler.cs | 30 +++++++++++++++++++ .../UpdateFaculty/UpdateFacultyCommand.cs | 9 ++++++ .../UpdateFacultyCommandHandler.cs | 20 +++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 Application/Cqrs/Faculty/Commands/CreateFaculty/CreateFacultyCommand.cs create mode 100644 Application/Cqrs/Faculty/Commands/CreateFaculty/CreateFacultyCommandHandler.cs create mode 100644 Application/Cqrs/Faculty/Commands/UpdateFaculty/UpdateFacultyCommand.cs create mode 100644 Application/Cqrs/Faculty/Commands/UpdateFaculty/UpdateFacultyCommandHandler.cs diff --git a/Application/Cqrs/Faculty/Commands/CreateFaculty/CreateFacultyCommand.cs b/Application/Cqrs/Faculty/Commands/CreateFaculty/CreateFacultyCommand.cs new file mode 100644 index 0000000..3f224e4 --- /dev/null +++ b/Application/Cqrs/Faculty/Commands/CreateFaculty/CreateFacultyCommand.cs @@ -0,0 +1,9 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Faculty.Commands.CreateFaculty; + +public class CreateFacultyCommand : IRequest +{ + public required string Name { get; set; } + public int? CampusId { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Faculty/Commands/CreateFaculty/CreateFacultyCommandHandler.cs b/Application/Cqrs/Faculty/Commands/CreateFaculty/CreateFacultyCommandHandler.cs new file mode 100644 index 0000000..07605fe --- /dev/null +++ b/Application/Cqrs/Faculty/Commands/CreateFaculty/CreateFacultyCommandHandler.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.Faculty.Commands.CreateFaculty; + +public class CreateFacultyCommandHandler(IFacultyDbContext dbContext) : IRequestHandler +{ + public async Task Handle(CreateFacultyCommand request, CancellationToken cancellationToken) + { + var entity = await dbContext.Faculties.FirstOrDefaultAsync(f => f.Name == request.Name, cancellationToken: cancellationToken); + + if (entity != null) + throw new RecordExistException(typeof(Domain.Schedule.Faculty), nameof(Domain.Schedule.Faculty.Name), entity.Id); + + var faculty = new Domain.Schedule.Faculty() + { + Name = request.Name, + CampusId = request.CampusId + }; + + var result = await dbContext.Faculties.AddAsync(faculty, cancellationToken); + await dbContext.SaveChangesAsync(cancellationToken); + + return result.Entity.Id; + } +} \ No newline at end of file diff --git a/Application/Cqrs/Faculty/Commands/UpdateFaculty/UpdateFacultyCommand.cs b/Application/Cqrs/Faculty/Commands/UpdateFaculty/UpdateFacultyCommand.cs new file mode 100644 index 0000000..1417317 --- /dev/null +++ b/Application/Cqrs/Faculty/Commands/UpdateFaculty/UpdateFacultyCommand.cs @@ -0,0 +1,9 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Faculty.Commands.UpdateFaculty; + +public class UpdateFacultyCommand : IRequest +{ + public required int Id { get; set; } + public required int CampusId { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Faculty/Commands/UpdateFaculty/UpdateFacultyCommandHandler.cs b/Application/Cqrs/Faculty/Commands/UpdateFaculty/UpdateFacultyCommandHandler.cs new file mode 100644 index 0000000..56e13cb --- /dev/null +++ b/Application/Cqrs/Faculty/Commands/UpdateFaculty/UpdateFacultyCommandHandler.cs @@ -0,0 +1,20 @@ +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.Faculty.Commands.UpdateFaculty; + +public class UpdateFacultyCommandHandler(IFacultyDbContext dbContext) : IRequestHandler +{ + public async Task Handle(UpdateFacultyCommand request, CancellationToken cancellationToken) + { + var entity = await dbContext.Faculties.FirstOrDefaultAsync(f => f.Id == request.Id, cancellationToken: cancellationToken) ?? throw new NotFoundException(typeof(Domain.Schedule.Faculty), nameof(Domain.Schedule.Faculty.Id), request.Id); + + entity.CampusId = request.CampusId; + + await dbContext.SaveChangesAsync(cancellationToken); + } +} \ No newline at end of file