Exemplo n.º 1
0
        public async Task <CursorPagedResult <Participant, string> > Handle(GetParticipantWithSuitabilityAnswersQuery query)
        {
            IQueryable <Participant> participants = _context.Participants
                                                    .Include("Person")
                                                    .Include("HearingRole")
                                                    .Include("Hearing.HearingCases.Case")
                                                    .Include("Questionnaire")
                                                    .Include("Questionnaire.SuitabilityAnswers");

            participants = participants.Where(x => x.Questionnaire != null && x.Questionnaire.SuitabilityAnswers.Any()).OrderByDescending(x => x.Questionnaire.UpdatedDate).ThenBy(x => x.Id.ToString());
            if (!string.IsNullOrEmpty(query.Cursor))
            {
                TryParseCursor(query.Cursor, out var updatedDateTime, out var id);
                participants = participants.Where(x => x.Questionnaire.UpdatedDate <= updatedDateTime &&
                                                  x.Id != Guid.Parse(id));
            }


            // Add one to the limit to know whether or not we have a next page
            var result = await participants.Take(query.Limit + 1).ToListAsync();

            string nextCursor = null;

            if (result.Count > query.Limit)
            {
                // The next cursor should be built based on the last item in the list
                result = result.Take(query.Limit).ToList();
                var lastResult = result.Last();
                nextCursor = $"{lastResult.Questionnaire.UpdatedDate.Ticks}_{lastResult.Id}";
            }

            return(new CursorPagedResult <Participant, string>(result, nextCursor));
        }
Exemplo n.º 2
0
        public async Task <ActionResult <SuitabilityAnswersResponse> > GetSuitabilityAnswers([FromQuery] string cursor = DefaultCursor, [FromQuery] int limit = DefaultLimit)
        {
            var query = new GetParticipantWithSuitabilityAnswersQuery()
            {
                Cursor = cursor == DefaultCursor ? null : cursor,
                Limit  = limit
            };
            var result = await _queryHandler.Handle <GetParticipantWithSuitabilityAnswersQuery, CursorPagedResult <Participant, string> >(query);

            var mapper = new SuitabilityAnswersListToResponseMapper();

            var response = new SuitabilityAnswersResponse
            {
                PrevPageUrl = BuildCursorPageUrl(cursor, limit),
                NextPageUrl = BuildCursorPageUrl(result.NextCursor, limit),
                NextCursor  = result.NextCursor,
                Limit       = limit,
                ParticipantSuitabilityAnswerResponse = mapper.MapParticipantSuitabilityAnswerResponses(result)
            };

            return(Ok(response));
        }