nice refactorings

This commit is contained in:
2022-10-10 03:17:14 +03:00
parent 4ca234aec8
commit 28a6981001
30 changed files with 622 additions and 141 deletions
@@ -0,0 +1,41 @@
using Mapster;
using MediatR;
using PracticeCalendar.Application.PracticeEvents.Queries;
using PracticeCalendar.Domain.Common.Interfaces;
using PracticeCalendar.Domain.Entities;
namespace PracticeCalendar.Application.PracticeEvents.Commands
{
public record CreatePracticeEventCommand : IRequest<PracticeEventDto>
{
public CreatePracticeEventCommand(PracticeEventDto eventDto)
{
Event = eventDto;
}
public PracticeEventDto Event { get; }
}
public class CreatePracticeEventCommandHandler : IRequestHandler<CreatePracticeEventCommand, PracticeEventDto>
{
private readonly IRepository<PracticeEvent> eventsRepo;
public CreatePracticeEventCommandHandler(IRepository<PracticeEvent> eventsRepo)
{
this.eventsRepo = eventsRepo;
}
public async Task<PracticeEventDto> Handle(CreatePracticeEventCommand request, CancellationToken cancellationToken)
{
var input = request.Event;
var practiceEvent = new PracticeEvent(input.Title, input.Description);
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<PracticeEventDto>();
}
}
}