mirror of
https://github.com/farcasclaudiu/PracticeCalendar.git
synced 2026-06-22 15:01:23 +03:00
45 lines
1.5 KiB
C#
45 lines
1.5 KiB
C#
using MediatR;
|
|
using PracticeCalendar.Domain.Common.Interfaces;
|
|
using PracticeCalendar.Domain.Entities.PracticeEvent;
|
|
using PracticeCalendar.Domain.Entities.PracticeEvent.Specifications;
|
|
using PracticeCalendar.Domain.Exceptions;
|
|
|
|
namespace PracticeCalendar.Application.PracticeEvents.Commands
|
|
{
|
|
public record AttendeeAcceptEventCommand : IRequest
|
|
{
|
|
public AttendeeAcceptEventCommand(int eventId, int attendeeId)
|
|
{
|
|
EventId = eventId;
|
|
AttendeeId = attendeeId;
|
|
}
|
|
|
|
public int EventId { get; }
|
|
public int AttendeeId { get; }
|
|
}
|
|
|
|
public class AttendeeAcceptEventCommandHandler : IRequestHandler<AttendeeAcceptEventCommand>
|
|
{
|
|
private readonly IRepository<PracticeEvent> eventsRepo;
|
|
|
|
public AttendeeAcceptEventCommandHandler(IRepository<PracticeEvent> eventsRepo)
|
|
{
|
|
this.eventsRepo = eventsRepo;
|
|
}
|
|
|
|
public async Task<Unit> Handle(AttendeeAcceptEventCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var spec = new PracticeEventByIdWithAttendeesSpecification(request.EventId);
|
|
var practiceEvent = await eventsRepo.FirstOrDefaultAsync(spec, cancellationToken);
|
|
if (practiceEvent == null)
|
|
{
|
|
throw new PracticeEventNotFoundException();
|
|
}
|
|
practiceEvent.AttendeeAcceptEvent(request.AttendeeId);
|
|
await eventsRepo.SaveChangesAsync(cancellationToken);
|
|
|
|
return Unit.Value;
|
|
}
|
|
}
|
|
}
|