mirror of
https://github.com/farcasclaudiu/PracticeCalendar.git
synced 2026-06-22 09:01:18 +03:00
nice refactorings
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
using MediatR;
|
||||
using PracticeCalendar.Domain.Common.Interfaces;
|
||||
using PracticeCalendar.Domain.Entities;
|
||||
using PracticeCalendar.Domain.Entities.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 PracticeEventByIdWithAttendees(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using MediatR;
|
||||
using PracticeCalendar.Domain.Common.Interfaces;
|
||||
using PracticeCalendar.Domain.Entities;
|
||||
using PracticeCalendar.Domain.Entities.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<AttendeeDeclineEventCommand>
|
||||
{
|
||||
private readonly IRepository<PracticeEvent> eventsRepo;
|
||||
|
||||
public AttendeeDeclineEventCommandHandler(IRepository<PracticeEvent> eventsRepo)
|
||||
{
|
||||
this.eventsRepo = eventsRepo;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(AttendeeDeclineEventCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var spec = new PracticeEventByIdWithAttendees(request.EventId);
|
||||
var practiceEvent = await eventsRepo.FirstOrDefaultAsync(spec, cancellationToken);
|
||||
if (practiceEvent == null)
|
||||
{
|
||||
throw new PracticeEventNotFoundException();
|
||||
}
|
||||
practiceEvent.AttendeeDeclineEvent(request.AttendeeId);
|
||||
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using MediatR;
|
||||
using PracticeCalendar.Domain.Common.Interfaces;
|
||||
using PracticeCalendar.Domain.Entities;
|
||||
using PracticeCalendar.Domain.Exceptions;
|
||||
|
||||
namespace PracticeCalendar.Application.PracticeEvents.Commands
|
||||
{
|
||||
public record DeletePracticeEventCommand : IRequest
|
||||
{
|
||||
public DeletePracticeEventCommand(int practiceEventId)
|
||||
{
|
||||
PracticeEventId = practiceEventId;
|
||||
}
|
||||
|
||||
public int PracticeEventId { get; }
|
||||
}
|
||||
public class DeletePracticeEventCommandHandler : IRequestHandler<DeletePracticeEventCommand>
|
||||
{
|
||||
private readonly IRepository<PracticeEvent> eventsRepo;
|
||||
|
||||
public DeletePracticeEventCommandHandler(IRepository<PracticeEvent> eventsRepo)
|
||||
{
|
||||
this.eventsRepo = eventsRepo;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(DeletePracticeEventCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var org = await eventsRepo.GetByIdAsync(request.PracticeEventId, cancellationToken);
|
||||
if (org == null)
|
||||
{
|
||||
throw new PracticeEventNotFoundException();
|
||||
}
|
||||
await eventsRepo.DeleteAsync(org, cancellationToken);
|
||||
await eventsRepo.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Mapster;
|
||||
using MediatR;
|
||||
using PracticeCalendar.Application.PracticeEvents.Queries;
|
||||
using PracticeCalendar.Domain.Common.Interfaces;
|
||||
using PracticeCalendar.Domain.Entities;
|
||||
using PracticeCalendar.Domain.Exceptions;
|
||||
|
||||
namespace PracticeCalendar.Application.PracticeEvents.Commands
|
||||
{
|
||||
public record UpdatePracticeEventCommand : IRequest<PracticeEventDto>
|
||||
{
|
||||
public UpdatePracticeEventCommand(PracticeEventDto eventDto)
|
||||
{
|
||||
Event = eventDto;
|
||||
}
|
||||
|
||||
public PracticeEventDto Event { get; }
|
||||
}
|
||||
|
||||
public class UpdatePracticeEventCommandHandler : IRequestHandler<UpdatePracticeEventCommand, PracticeEventDto>
|
||||
{
|
||||
private readonly IRepository<PracticeEvent> eventsRepo;
|
||||
|
||||
public UpdatePracticeEventCommandHandler(IRepository<PracticeEvent> eventsRepo)
|
||||
{
|
||||
this.eventsRepo = eventsRepo;
|
||||
}
|
||||
|
||||
public async Task<PracticeEventDto> Handle(UpdatePracticeEventCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var eventModel = request.Event;
|
||||
var practiceEvent = await eventsRepo.GetByIdAsync(eventModel.Id, cancellationToken);
|
||||
if (practiceEvent == null)
|
||||
{
|
||||
throw new PracticeEventNotFoundException();
|
||||
}
|
||||
practiceEvent.UpdateTitleAndDescription(eventModel.Title, eventModel.Description);
|
||||
await eventsRepo.UpdateAsync(practiceEvent, cancellationToken);
|
||||
await eventsRepo.SaveChangesAsync(cancellationToken);
|
||||
return practiceEvent.Adapt<PracticeEventDto>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace PracticeCalendar.Application.PracticeEvents.Queries
|
||||
{
|
||||
public class AttendeeDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string EmailAddress { get; set; } = string.Empty;
|
||||
public bool IsAttending { get; set; }
|
||||
}
|
||||
}
|
||||
+14
-8
@@ -1,4 +1,6 @@
|
||||
using MediatR;
|
||||
using Mapster;
|
||||
using MapsterMapper;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PracticeCalendar.Domain.Common.Interfaces;
|
||||
using PracticeCalendar.Domain.Entities;
|
||||
@@ -6,27 +8,31 @@ using PracticeCalendar.Domain.Entities.Specifications;
|
||||
|
||||
namespace PracticeCalendar.Application.PracticeEvents.Queries.GetPracticeEvents
|
||||
{
|
||||
public record class GetPracticeEventsQuery : IRequest<List<PracticeEvent>>
|
||||
public record class GetPracticeEventsQuery : IRequest<List<PracticeEventDto>>
|
||||
{
|
||||
}
|
||||
|
||||
public class GetPracticeEventsQueryHandler : IRequestHandler<GetPracticeEventsQuery, List<PracticeEvent>>
|
||||
public class GetPracticeEventsQueryHandler : IRequestHandler<GetPracticeEventsQuery, List<PracticeEventDto>>
|
||||
{
|
||||
private readonly ILogger<GetPracticeEventsQueryHandler> logger;
|
||||
private readonly IRepository<PracticeEvent> eventsRepo;
|
||||
private readonly IMapper mapper;
|
||||
|
||||
public GetPracticeEventsQueryHandler(IRepository<PracticeEvent> eventsRepo,
|
||||
ILogger<GetPracticeEventsQueryHandler> logger)
|
||||
public GetPracticeEventsQueryHandler(IRepository<PracticeEvent> eventsRepo,
|
||||
ILogger<GetPracticeEventsQueryHandler> logger,
|
||||
IMapper mapper)
|
||||
{
|
||||
this.eventsRepo = eventsRepo;
|
||||
this.logger = logger;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<List<PracticeEvent>> Handle(GetPracticeEventsQuery request, CancellationToken cancellationToken)
|
||||
public async Task<List<PracticeEventDto>> Handle(GetPracticeEventsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var spec = new PracticeEventsWithAttendees();
|
||||
var evList = await eventsRepo.ListAsync(spec);
|
||||
return evList;
|
||||
var evList = await eventsRepo.ListAsync(spec, cancellationToken);
|
||||
var lst = evList.Adapt<List<PracticeEventDto>>(mapper.Config);
|
||||
return lst;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace PracticeCalendar.Application.PracticeEvents.Queries
|
||||
{
|
||||
public class PracticeEventDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string Description { get; set; } = string.Empty;
|
||||
|
||||
public DateTime StartTime { get; set; }
|
||||
public DateTime EndTime { get; set; }
|
||||
|
||||
public IList<AttendeeDto> Attendees { get; set; } = new List<AttendeeDto>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user