33import org .apache .commons .lang3 .StringUtils ;
44import org .apereo .openlrw .caliper .exception .EventNotFoundException ;
55import org .apereo .openlrw .common .exception .BadRequestException ;
6+ import org .apereo .openlrw .oneroster .exception .OneRosterNotFoundException ;
67import org .apereo .openlrw .risk .MongoRisk ;
78import org .apereo .openlrw .risk .service .repository .MongoRiskRepository ;
89import 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