-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetPlayOffersByParticipantIdHandler.cs
49 lines (43 loc) · 2.55 KB
/
GetPlayOffersByParticipantIdHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using MediatR;
using PlayOfferService.Application.Queries;
using PlayOfferService.Domain.Models;
using PlayOfferService.Domain.Repositories;
namespace PlayOfferService.Application.Handlers;
public class GetPlayOffersByParticipantIdHandler : IRequestHandler<GetPlayOffersByParticipantIdQuery, IEnumerable<PlayOfferDto>>
{
private readonly PlayOfferRepository _playOfferRepository;
private readonly MemberRepository _memberRepository;
private readonly ClubRepository _clubRepository;
private readonly ReservationRepository _reservationRepository;
private readonly CourtRepository _courtRepository;
public GetPlayOffersByParticipantIdHandler(PlayOfferRepository playOfferRepository, MemberRepository memberRepository, ClubRepository clubRepository, ReservationRepository reservationRepository, CourtRepository courtRepository)
{
_playOfferRepository = playOfferRepository;
_memberRepository = memberRepository;
_clubRepository = clubRepository;
_reservationRepository = reservationRepository;
_courtRepository = courtRepository;
}
public async Task<IEnumerable<PlayOfferDto>> Handle(GetPlayOffersByParticipantIdQuery request, CancellationToken cancellationToken)
{
var playOffers = await _playOfferRepository.GetPlayOffersByParticipantId(request.ParticipantId);
var clubDto = (await _clubRepository.GetAllClubs()).Select(club => new ClubDto(club)).ToList();
var memberDtos = (await _memberRepository.GetAllMembers()).Select(member => new MemberDto(member)).ToList();
var courtDtos = (await _courtRepository.GetAllCourts()).Select(court => new CourtDto(court)).ToList();
var reservationDtos = (await _reservationRepository.GetAllReservations())
.Select(reservation => new ReservationDto(
reservation,
courtDtos.First(courtDto => courtDto.Id == reservation.CourtId)))
.ToList();
var playOfferDtos = new List<PlayOfferDto>();
foreach (var playOffer in playOffers)
{
var club = clubDto.First(club => club.Id == playOffer.ClubId);
var creator = memberDtos.First(member => member.Id == playOffer.CreatorId);
var opponent = memberDtos.FirstOrDefault(member => member.Id == playOffer.OpponentId);
var reservation = reservationDtos.FirstOrDefault(reservation => reservation.Id == playOffer.ReservationId);
playOfferDtos.Add(new PlayOfferDto(playOffer, club, creator, opponent, reservation));
}
return playOfferDtos;
}
}