Skip to content

Commit 7e96126

Browse files
committed
feat: add limit query parameter
1 parent b0329d6 commit 7e96126

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/main/java/org/apereo/openlrw/risk/endpoint/RiskController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ public Collection<MongoRisk> getClassUser(
5252
JwtAuthenticationToken token,
5353
@PathVariable final String classSourcedId,
5454
@PathVariable final String userSourcedId,
55-
@RequestParam(value="date", required=false, defaultValue = "") String date
55+
@RequestParam(value="date", required=false, defaultValue = "") String date,
56+
@RequestParam(value="limit", required=false, defaultValue = "0") int limit
5657
) {
5758
UserContext userContext = (UserContext) token.getPrincipal();
5859
try {
59-
return riskService.getRisksForUserAndClass(userContext.getTenantId(), userContext.getOrgId(), classSourcedId, userSourcedId, date);
60+
return riskService.getRisksForUserAndClass(userContext.getTenantId(), userContext.getOrgId(), classSourcedId, userSourcedId, date, limit);
6061
} catch (EventNotFoundException e) {
6162
throw new EventNotFoundException(e.getMessage());
6263
} catch (BadRequestException e) {

src/main/java/org/apereo/openlrw/risk/service/RiskService.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.apache.commons.lang3.StringUtils;
44
import org.apereo.openlrw.caliper.exception.EventNotFoundException;
55
import org.apereo.openlrw.common.exception.BadRequestException;
6+
import org.apereo.openlrw.oneroster.exception.OneRosterNotFoundException;
67
import org.apereo.openlrw.risk.MongoRisk;
78
import org.apereo.openlrw.risk.service.repository.MongoRiskRepository;
89
import org.joda.time.DateTime;
@@ -112,29 +113,34 @@ public MongoRisk save(final String tenantId, final String orgId, MongoRisk mongo
112113
* @param classId
113114
* @param userId
114115
* @param date
115-
* @return
116+
* @return Collection<MongoRisk>
116117
*/
117-
public Collection<MongoRisk> getRisksForUserAndClass(final String tenantId, final String orgId, final String classId, final String userId, final String date) {
118+
public Collection<MongoRisk> getRisksForUserAndClass(
119+
final String tenantId, final String orgId, final String classId,
120+
final String userId, final String date, final int limit
121+
) {
118122
if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(orgId) || StringUtils.isBlank(userId) || StringUtils.isBlank(classId))
119123
throw new IllegalArgumentException();
120124

121-
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");
125+
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
122126

123127
Collection<MongoRisk> mongoRisks;
124128

125129
Query query = new Query();
130+
query.with(new Sort(Sort.Direction.DESC, "dateTime")); // Order by date: the more recent
126131
query.addCriteria(where("userSourcedId").is(userId).and("classSourcedId").is(classId).and("orgId").is(orgId).and("tenantId").is(tenantId));
127132

133+
if (limit > 0)
134+
query.limit(limit);
135+
128136
if (!date.isEmpty()) {
129137
if (date.equals("latest")){
130138
query.limit(1);
131-
query.with(new Sort(Sort.Direction.DESC, "dateTime"));
132139
} else {
133140
try {
134-
DateTime dateTimeUtc = formatter.parseDateTime(date).withZone(DateTimeZone.UTC); // change rien mais en fait pas besoin a reflechir comment organiser la query
135-
DateTime previous = dateTimeUtc.minusMinutes(1);
136-
DateTime after = dateTimeUtc.plusMinutes(1);
137-
query.addCriteria(where("dateTime").lt(after).gt(previous)); // Get the risks for the minute given
141+
DateTime startDate = formatter.parseDateTime(date).withZone(DateTimeZone.UTC);
142+
DateTime endDate = startDate.plusDays(1);
143+
query.addCriteria(where("dateTime").gte(startDate).lt(endDate)); // Get the risks for the day given
138144
} catch (Exception e) {
139145
throw new BadRequestException("Not able to parse the date, it has to be in the following format: `yyyy-MM-dd hh:mm` ");
140146
}
@@ -146,7 +152,7 @@ public Collection<MongoRisk> getRisksForUserAndClass(final String tenantId, fina
146152
if (!mongoRisks.isEmpty())
147153
return new ArrayList<>(mongoRisks);
148154

149-
throw new EventNotFoundException("Risks not found.");
155+
throw new OneRosterNotFoundException("Risks not found.");
150156
}
151157
}
152158

0 commit comments

Comments
 (0)