Skip to content

Commit

Permalink
Changes to fix pagination with participant list API (#143)
Browse files Browse the repository at this point in the history
* Changes to fix pagination with participant list API

* Review chnages
  • Loading branch information
sreeragksgh authored Mar 21, 2024
1 parent 973af66 commit 78dcc0a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ public Map<String, Object> getBatchParticipantsByPage(RequestContext requestCont
String previousPageId = null;
int currentOffSet = 1;
String currentPagingState = null;
Response countResponse = cassandraOperation.getCountOfRecordByIdentifier(requestContext, KEYSPACE_NAME,
ENROLMENT_BATCH_LOOKUP, queryMap, JsonKey.USER_ID);
Long count = (Long) (((List<Map<String, Object>>)countResponse.getResult().get("response")).get(0)).get(JsonKey.USERS_COUNT);
do {
Response response = cassandraOperation.getRecordByIdentifierWithPage(requestContext, KEYSPACE_NAME,
ENROLMENT_BATCH_LOOKUP, queryMap,
Expand Down Expand Up @@ -225,9 +228,9 @@ public Map<String, Object> getBatchParticipantsByPage(RequestContext requestCont
if (currentOffSet >= limit) {
currentOffSet = currentOffSet - limit;
}
result.put(JsonKey.CURRENT_OFFSET, currentOffSet);
}
result.put(JsonKey.COUNT, userList.size());
result.put(JsonKey.CURRENT_OFFSET, (Integer) request.get(JsonKey.CURRENT_OFFSET));
result.put(JsonKey.COUNT, count);
result.put(JsonKey.PARTICIPANTS, userList);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,6 @@ public Response getRecordsWithLimit(
* @return
*/
Response getRecordByIdentifierWithPage(RequestContext requestContext, String keyspaceName, String tableName, Object key, List<String> fields, String pageString, int limit);

Response getCountOfRecordByIdentifier(RequestContext requestContext, String keyspaceName, String tableName, Object key, String field);
}
Original file line number Diff line number Diff line change
Expand Up @@ -846,4 +846,46 @@ public Response getRecordByIdentifierWithPage(RequestContext requestContext, Str
return response;
}

@Override
public Response getCountOfRecordByIdentifier(
RequestContext requestContext, String keyspaceName, String tableName, Object key, String field) {
long startTime = System.currentTimeMillis();
logger.debug(requestContext, "Cassandra Service getRecordBy key method started at ==" + startTime);
Response response = new Response();
try {
Session session = connectionManager.getSession(keyspaceName);
Builder selectBuilder;
if (StringUtils.isNotEmpty(field)) {
selectBuilder = QueryBuilder.select().count(field);
} else {
selectBuilder = QueryBuilder.select().countAll();
}
Select selectQuery = selectBuilder.from(keyspaceName, tableName);
Where selectWhere = selectQuery.where();
if (key instanceof String) {
selectWhere.and(eq(Constants.IDENTIFIER, key));
} else if (key instanceof Map) {
Map<String, Object> compositeKey = (Map<String, Object>) key;
compositeKey
.entrySet()
.stream()
.forEach(
x -> {
CassandraUtil.createQuery(x.getKey(), x.getValue(), selectWhere);
});
}
logger.debug(requestContext, selectQuery.getQueryString());
ResultSet results = session.execute(selectQuery);
response = CassandraUtil.createResponse(results);
} catch (Exception e) {
logger.error(requestContext, Constants.EXCEPTION_MSG_FETCH + tableName + " : " + e.getMessage(), e);
throw new ProjectCommonException(
ResponseCode.SERVER_ERROR.getErrorCode(),
ResponseCode.SERVER_ERROR.getErrorMessage(),
ResponseCode.SERVER_ERROR.getResponseCode());
}
logQueryElapseTime("getRecordByIdentifier", startTime);
return response;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,6 @@ public final class JsonKey {
public static final String ADD_INFO = "addinfo";
public static final String USERID_LIST="userIdList";
public static final String LRC_PROGRESS_DETAILS = "lrcProgressDetails";

public static final String USERS_COUNT = "system.count(userid)";
private JsonKey() {}
}

0 comments on commit 78dcc0a

Please sign in to comment.