diff --git a/Application/Cqrs/Group/Commands/CreateGroup/CreateGroupCommand.cs b/Application/Cqrs/Group/Commands/CreateGroup/CreateGroupCommand.cs new file mode 100644 index 0000000..33a42cd --- /dev/null +++ b/Application/Cqrs/Group/Commands/CreateGroup/CreateGroupCommand.cs @@ -0,0 +1,9 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Group.Commands.CreateGroup; + +public class CreateGroupCommand : IRequest +{ + public required string Name { get; set; } + public int? FacultyId { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Group/Commands/CreateGroup/CreateGroupCommandHandler.cs b/Application/Cqrs/Group/Commands/CreateGroup/CreateGroupCommandHandler.cs new file mode 100644 index 0000000..30e556e --- /dev/null +++ b/Application/Cqrs/Group/Commands/CreateGroup/CreateGroupCommandHandler.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.Group.Commands.CreateGroup; + +public class CreateGroupCommandHandler(IGroupDbContext dbContext) : IRequestHandler +{ + public async Task Handle(CreateGroupCommand request, CancellationToken cancellationToken) + { + var entity = await dbContext.Groups.FirstOrDefaultAsync(g => g.Name == request.Name, cancellationToken: cancellationToken); + + if (entity != null) + throw new RecordExistException(typeof(Domain.Schedule.Group), nameof(Domain.Schedule.Group.Name), entity.Id); + + var group = new Domain.Schedule.Group() + { + Name = request.Name, + FacultyId = request.FacultyId + }; + + var result = await dbContext.Groups.AddAsync(group, cancellationToken); + await dbContext.SaveChangesAsync(cancellationToken); + + return result.Entity.Id; + } +} \ No newline at end of file diff --git a/Application/Cqrs/Group/Commands/UpdateGroup/UpdateGroupCommand.cs b/Application/Cqrs/Group/Commands/UpdateGroup/UpdateGroupCommand.cs new file mode 100644 index 0000000..497b7f6 --- /dev/null +++ b/Application/Cqrs/Group/Commands/UpdateGroup/UpdateGroupCommand.cs @@ -0,0 +1,9 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Group.Commands.UpdateGroup; + +public class UpdateGroupCommand : IRequest +{ + public required int Id { get; set; } + public required int FacultyId { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Group/Commands/UpdateGroup/UpdateGroupCommandHandler.cs b/Application/Cqrs/Group/Commands/UpdateGroup/UpdateGroupCommandHandler.cs new file mode 100644 index 0000000..7f2c0f4 --- /dev/null +++ b/Application/Cqrs/Group/Commands/UpdateGroup/UpdateGroupCommandHandler.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.Group.Commands.UpdateGroup; + +public class UpdateGroupCommandHandler(IGroupDbContext dbContext) : IRequestHandler +{ + public async Task Handle(UpdateGroupCommand request, CancellationToken cancellationToken) + { + var entity = await dbContext.Groups.FirstOrDefaultAsync(g => g.Id == request.Id, cancellationToken: cancellationToken) ?? throw new NotFoundException(typeof(Domain.Schedule.Group), nameof(Domain.Schedule.Group.Id), request.Id); + + entity.FacultyId = request.FacultyId; + + await dbContext.SaveChangesAsync(cancellationToken); + } +} \ No newline at end of file