using Mapster; using MediatR; using PracticeCalendar.Application.PracticeEvents.Queries; using PracticeCalendar.Domain.Common.Interfaces; using PracticeCalendar.Domain.Entities.PracticeEvent; namespace PracticeCalendar.Application.PracticeEvents.Commands { public record CreatePracticeEventCommand : IRequest { public CreatePracticeEventCommand(PracticeEventDto eventDto) { Event = eventDto; } public PracticeEventDto Event { get; } } public class CreatePracticeEventCommandHandler : IRequestHandler { private readonly IRepository eventsRepo; public CreatePracticeEventCommandHandler(IRepository eventsRepo) { this.eventsRepo = eventsRepo; } public async Task Handle(CreatePracticeEventCommand request, CancellationToken cancellationToken) { var input = request.Event; var practiceEvent = new PracticeEvent(input.Title, input.Description, input.StartTime, input.EndTime); foreach (var att in input.Attendees) { practiceEvent.AddAttendee(new Attendee(att.Name, att.EmailAddress)); } var result = await eventsRepo.AddAsync(practiceEvent, cancellationToken); await eventsRepo.SaveChangesAsync(cancellationToken); return result.Adapt(); } } }