Skip to content

Commit 97c87d6

Browse files
committed
240: Create new filters param on previous
leaderboard endpoint and fixed leaderboard tests.
1 parent d2bff5c commit 97c87d6

2 files changed

Lines changed: 140 additions & 20 deletions

File tree

src/main/java/org/patinanetwork/codebloom/api/leaderboard/LeaderboardController.java

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import jakarta.servlet.http.HttpServletRequest;
1111
import java.util.List;
1212
import java.util.Optional;
13+
import java.util.Set;
1314
import org.patinanetwork.codebloom.common.components.LeaderboardManager;
1415
import org.patinanetwork.codebloom.common.db.models.leaderboard.Leaderboard;
1516
import org.patinanetwork.codebloom.common.db.models.user.UserWithScore;
@@ -93,6 +94,9 @@ public ResponseEntity<ApiResponder<LeaderboardDto>> getLeaderboardMetadataByLead
9394
})
9495
public ResponseEntity<ApiResponder<Page<Indexed<UserWithScoreDto>>>> getLeaderboardUsersById(
9596
@PathVariable final String leaderboardId,
97+
@Parameter(description = "Comma-separated list of active filters.", example = "nyu,hunter,globalIndex")
98+
@RequestParam(required = false)
99+
final Set<String> filters,
96100
@Parameter(description = "Page index", example = "1") @RequestParam(required = false, defaultValue = "1")
97101
final int page,
98102
@Parameter(description = "Page size (maximum of " + MAX_LEADERBOARD_PAGE_SIZE)
@@ -133,29 +137,51 @@ public ResponseEntity<ApiResponder<Page<Indexed<UserWithScoreDto>>>> getLeaderbo
133137
final boolean globalIndex,
134138
final HttpServletRequest request) {
135139
FakeLag.sleep(800);
136-
140+
final boolean globalIndexNew;
141+
final LeaderboardFilterOptions options;
137142
final int parsedPageSize = Math.min(pageSize, MAX_LEADERBOARD_PAGE_SIZE);
138143

139-
LeaderboardFilterOptions options = LeaderboardFilterOptions.builder()
140-
.page(page)
141-
.pageSize(parsedPageSize)
142-
.query(query)
143-
.patina(patina)
144-
.hunter(hunter)
145-
.nyu(nyu)
146-
.baruch(baruch)
147-
.rpi(rpi)
148-
.gwc(gwc)
149-
.sbu(sbu)
150-
.ccny(ccny)
151-
.columbia(columbia)
152-
.cornell(cornell)
153-
.bmcc(bmcc)
154-
.mhcplusplus(mhcplusplus)
155-
.build();
156-
144+
if (filters == null || filters.isEmpty()) {
145+
globalIndexNew = globalIndex;
146+
options = LeaderboardFilterOptions.builder()
147+
.page(page)
148+
.pageSize(parsedPageSize)
149+
.query(query)
150+
.patina(patina)
151+
.hunter(hunter)
152+
.nyu(nyu)
153+
.baruch(baruch)
154+
.rpi(rpi)
155+
.gwc(gwc)
156+
.sbu(sbu)
157+
.ccny(ccny)
158+
.columbia(columbia)
159+
.cornell(cornell)
160+
.bmcc(bmcc)
161+
.mhcplusplus(mhcplusplus)
162+
.build();
163+
} else {
164+
globalIndexNew = filters.contains("globalIndex");
165+
options = LeaderboardFilterOptions.builder()
166+
.page(page)
167+
.pageSize(parsedPageSize)
168+
.query(query)
169+
.patina(filters.contains("patina"))
170+
.hunter(filters.contains("hunter"))
171+
.nyu(filters.contains("nyu"))
172+
.baruch(filters.contains("baruch"))
173+
.rpi(filters.contains("rpi"))
174+
.gwc(filters.contains("gwc"))
175+
.sbu(filters.contains("sbu"))
176+
.ccny(filters.contains("ccny"))
177+
.columbia(filters.contains("columbia"))
178+
.cornell(filters.contains("cornell"))
179+
.bmcc(filters.contains("bmcc"))
180+
.mhcplusplus(filters.contains("mhcplusplus"))
181+
.build();
182+
}
157183
Page<Indexed<UserWithScoreDto>> createdPage =
158-
leaderboardManager.getLeaderboardUsers(leaderboardId, options, globalIndex);
184+
leaderboardManager.getLeaderboardUsers(leaderboardId, options, globalIndexNew);
159185

160186
return ResponseEntity.ok().body(ApiResponder.success("All leaderboards found!", createdPage));
161187
}

src/test/java/org/patinanetwork/codebloom/api/leaderboard/LeaderboardControllerTest.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.time.LocalDateTime;
77
import java.util.List;
88
import java.util.Optional;
9+
import java.util.Set;
910
import java.util.UUID;
1011
import org.junit.jupiter.api.BeforeEach;
1112
import org.junit.jupiter.api.DisplayName;
@@ -54,6 +55,7 @@ void setup() {
5455
void testGetLeaderboardUsersByIdMhcPlusPlus() {
5556
leaderboardController.getLeaderboardUsersById(
5657
LEADERBOARD_ID,
58+
null,
5759
PAGE,
5860
PAGE_SIZE,
5961
"",
@@ -135,6 +137,7 @@ void throws404WhenNotFound() {
135137
void capsPageSize() {
136138
leaderboardController.getLeaderboardUsersById(
137139
LEADERBOARD_ID,
140+
null,
138141
1,
139142
999,
140143
"",
@@ -164,6 +167,7 @@ void capsPageSize() {
164167
void passesAllFilters() {
165168
leaderboardController.getLeaderboardUsersById(
166169
LEADERBOARD_ID,
170+
null,
167171
2,
168172
10,
169173
"tahmid",
@@ -203,6 +207,96 @@ void passesAllFilters() {
203207
assertTrue(opts.isMhcplusplus());
204208
}
205209

210+
@Test
211+
@DisplayName("uses filters Set when provided, ignoring individual boolean params")
212+
void usesFiltersSetWhenProvided() {
213+
leaderboardController.getLeaderboardUsersById(
214+
LEADERBOARD_ID,
215+
Set.of("nyu", "hunter"),
216+
PAGE,
217+
PAGE_SIZE,
218+
"",
219+
false,
220+
false,
221+
false,
222+
false,
223+
false,
224+
false,
225+
false,
226+
false,
227+
false,
228+
false,
229+
false,
230+
false,
231+
false,
232+
null);
233+
234+
ArgumentCaptor<LeaderboardFilterOptions> captor = ArgumentCaptor.forClass(LeaderboardFilterOptions.class);
235+
verify(leaderboardManager).getLeaderboardUsers(eq(LEADERBOARD_ID), captor.capture(), eq(false));
236+
var opts = captor.getValue();
237+
assertTrue(opts.isNyu());
238+
assertTrue(opts.isHunter());
239+
assertFalse(opts.isPatina());
240+
}
241+
242+
@Test
243+
@DisplayName("sets globalIndex from filters Set when it contains globalIndex")
244+
void setsGlobalIndexFromFiltersSet() {
245+
leaderboardController.getLeaderboardUsersById(
246+
LEADERBOARD_ID,
247+
Set.of("nyu", "globalIndex"),
248+
PAGE,
249+
PAGE_SIZE,
250+
"",
251+
false,
252+
false,
253+
false,
254+
false,
255+
false,
256+
false,
257+
false,
258+
false,
259+
false,
260+
false,
261+
false,
262+
false,
263+
false,
264+
null);
265+
266+
ArgumentCaptor<LeaderboardFilterOptions> captor = ArgumentCaptor.forClass(LeaderboardFilterOptions.class);
267+
verify(leaderboardManager).getLeaderboardUsers(eq(LEADERBOARD_ID), captor.capture(), eq(true));
268+
assertTrue(captor.getValue().isNyu());
269+
}
270+
271+
@Test
272+
@DisplayName("falls back to boolean params when filters Set is empty")
273+
void fallsBackToBooleansWhenFiltersEmpty() {
274+
leaderboardController.getLeaderboardUsersById(
275+
LEADERBOARD_ID,
276+
Set.of(),
277+
PAGE,
278+
PAGE_SIZE,
279+
"",
280+
true,
281+
false,
282+
false,
283+
false,
284+
false,
285+
false,
286+
false,
287+
false,
288+
false,
289+
false,
290+
false,
291+
false,
292+
false,
293+
null);
294+
295+
ArgumentCaptor<LeaderboardFilterOptions> captor = ArgumentCaptor.forClass(LeaderboardFilterOptions.class);
296+
verify(leaderboardManager).getLeaderboardUsers(eq(LEADERBOARD_ID), captor.capture(), eq(false));
297+
assertTrue(captor.getValue().isPatina());
298+
}
299+
206300
@Test
207301
@DisplayName("returns metadata for the most recent leaderboard")
208302
void returnsCurrentMetadata() {

0 commit comments

Comments
 (0)