30 lines
1.1 KiB
C#
30 lines
1.1 KiB
C#
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<CreateGroupCommand, int>
|
|
{
|
|
public async Task<int> 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;
|
|
}
|
|
} |