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 AttendeeDeclineEventCommand : IRequest { public AttendeeDeclineEventCommand(int eventId, int attendeeId) { EventId = eventId; AttendeeId = attendeeId; } public int EventId { get; } public int AttendeeId { get; } } public class AttendeeDeclineEventCommandHandler : IRequestHandler { private readonly IRepository eventsRepo; public AttendeeDeclineEventCommandHandler(IRepository eventsRepo) { this.eventsRepo = eventsRepo; } public async Task Handle(AttendeeDeclineEventCommand request, CancellationToken cancellationToken) { var spec = new PracticeEventByIdWithAttendeesSpecification(request.EventId); var practiceEvent = await eventsRepo.FirstOrDefaultAsync(spec, cancellationToken); if (practiceEvent == null) { throw new PracticeEventNotFoundException(); } practiceEvent.AttendeeDeclineEvent(request.AttendeeId); return Unit.Value; } } }