53 lines
1.6 KiB
C#
53 lines
1.6 KiB
C#
using Indotalent.ConfigBackEnd.Exceptions;
|
|
using Indotalent.Infrastructure.Database;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Indotalent.Features.Utilities.BookingGroup.Cqrs;
|
|
|
|
public class CreateBookingGroupRequest
|
|
{
|
|
public string? Name { get; set; }
|
|
public string? Description { get; set; }
|
|
}
|
|
|
|
public class CreateBookingGroupResponse
|
|
{
|
|
public string? Id { get; set; }
|
|
public string? Name { get; set; }
|
|
}
|
|
|
|
public record CreateBookingGroupCommand(CreateBookingGroupRequest Data) : IRequest<CreateBookingGroupResponse>;
|
|
|
|
public class CreateBookingGroupHandler : IRequestHandler<CreateBookingGroupCommand, CreateBookingGroupResponse>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public CreateBookingGroupHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<CreateBookingGroupResponse> Handle(CreateBookingGroupCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var isExists = await _context.BookingGroup
|
|
.AnyAsync(x => x.Name == request.Data.Name, cancellationToken);
|
|
|
|
if (isExists)
|
|
{
|
|
throw new AlreadyExistsException("Booking Group", request.Data.Name ?? string.Empty);
|
|
}
|
|
|
|
var entity = new Data.Entities.BookingGroup
|
|
{
|
|
Name = request.Data.Name,
|
|
Description = request.Data.Description
|
|
};
|
|
|
|
_context.BookingGroup.Add(entity);
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return new CreateBookingGroupResponse
|
|
{
|
|
Id = entity.Id,
|
|
Name = entity.Name
|
|
};
|
|
}
|
|
} |