public async Task<PrizesEnvelope> Handle(Query request, CancellationToken cancellationToken) { // Getting the parent draw & your prizes var draw = await _context .Draws .Include(d => d.Prizes) .ThenInclude(p => p.SelectionSteps) .FirstOrDefaultAsync(d => d.Id == request.DrawId, cancellationToken); // Validations if (draw == null) { throw new RestException(HttpStatusCode.NotFound, new { Error = $"El sorteo con id '{ request.DrawId }' no existe." }); } // Loading entrant info var allPrizeSelectionSteps = draw .Prizes .SelectMany(p => p.SelectionSteps); foreach (var pss in allPrizeSelectionSteps) { await _context .Entry(pss) .Reference(p => p.Entrant) .LoadAsync(); } // Mapping return new PrizesEnvelope { Prizes = _mapper.Map<List<Prize>, List<PrizeEnvelope>>(draw.Prizes), PrizesQty = draw.PrizesQty }; }
public async Task <DrawEnvelope> Handle(Query request, CancellationToken cancellationToken) { // Getting draw var draw = await _context .Draws .Include(d => d.Prizes) .ThenInclude(p => p.SelectionSteps) .FirstOrDefaultAsync(d => d.Id == request.DrawId, cancellationToken); // Validations if (draw == null) { throw new RestException(HttpStatusCode.NotFound, new { Error = $"El sorteo con el id '{ request.DrawId }' no existe." }); } // Loading entrant info var allPrizeSelectionSteps = draw .Prizes .SelectMany(p => p.SelectionSteps); foreach (var pss in allPrizeSelectionSteps) { await _context .Entry(pss) .Reference(p => p.Entrant) .LoadAsync(); } // Mapping var drawEnvelope = _mapper.Map <Draw, DrawEnvelope>(draw); // Getting quantity of entries for the draw. drawEnvelope.EntriesQty = await _context.DrawEntries.CountAsync(de => de.DrawId == drawEnvelope.Id, cancellationToken); return(drawEnvelope); }