From 0285725b6d00bf28293e7c9563f791bebe56ae73 Mon Sep 17 00:00:00 2001 From: Josh Creek <8179928+jcreek@users.noreply.github.com> Date: Sun, 14 Jul 2024 20:51:05 +0100 Subject: [PATCH] feat(*): Add ability to filter by game and region --- .../repositories/CombinedDataRepository.ts | 37 ++++++- src/routes/api/combined-data/+server.ts | 6 +- src/routes/mydex/+page.svelte | 99 ++++++++++++++++--- 3 files changed, 126 insertions(+), 16 deletions(-) diff --git a/src/lib/repositories/CombinedDataRepository.ts b/src/lib/repositories/CombinedDataRepository.ts index 95555da..2eee0a1 100644 --- a/src/lib/repositories/CombinedDataRepository.ts +++ b/src/lib/repositories/CombinedDataRepository.ts @@ -6,7 +6,9 @@ class CombinedDataRepository { async findCombinedData( page: number = 1, limit: number = 20, - enableForms: boolean = true + enableForms: boolean = true, + region: string = '', + game: string = '' ): Promise { const skip = (page - 1) * limit; const pipeline: any[] = [ @@ -43,6 +45,22 @@ class CombinedDataRepository { }); } + if (region.length > 0) { + pipeline.unshift({ + $match: { + regionToCatchIn: region + } + }); + } + + if (game.length > 0) { + pipeline.unshift({ + $match: { + gamesToCatchIn: { $in: [game] } + } + }); + } + const combinedData: CombinedData[] = await PokedexEntryModel.aggregate(pipeline).exec(); // Filter out entries with no catch records @@ -54,13 +72,28 @@ class CombinedDataRepository { })); } - async countCombinedData(enableForms: boolean): Promise { + async countCombinedData(enableForms: boolean, region: string, game: string): Promise { const filter: any = {}; if (!enableForms) { filter['boxPlacement.box'] = { $ne: null }; } + if (region.length > 0) { + filter['regionToCatchIn'] = region; + } + + if (game.length > 0) { + // Check if gamesToCatchIn is not already defined in filter + if (!filter['gamesToCatchIn']) { + filter['gamesToCatchIn'] = []; + } + // Add the game to the filter if it's not already included + if (!filter['gamesToCatchIn'].includes(game)) { + filter['gamesToCatchIn'].push(game); + } + } + return PokedexEntryModel.countDocuments(filter).exec(); } } diff --git a/src/routes/api/combined-data/+server.ts b/src/routes/api/combined-data/+server.ts index 342c265..b778135 100644 --- a/src/routes/api/combined-data/+server.ts +++ b/src/routes/api/combined-data/+server.ts @@ -6,16 +6,18 @@ export const GET = async ({ url }) => { const page = parseInt(url.searchParams.get('page') || '1', 10); const limit = parseInt(url.searchParams.get('limit') || '20', 10); const enableForms = url.searchParams.get('enableForms') === 'true'; + const region = url.searchParams.get('region'); + const game = url.searchParams.get('game'); try { await dbConnect(); const repo = new CombinedDataRepository(); - const combinedData = await repo.findCombinedData(page, limit, enableForms); + const combinedData = await repo.findCombinedData(page, limit, enableForms, region, game); if (combinedData.length === 0) { return json({ error: 'No combined data found' }, { status: 404 }); } - const totalCount = await repo.countCombinedData(enableForms); + const totalCount = await repo.countCombinedData(enableForms, region, game); const totalPages = Math.ceil(totalCount / limit); return json({ combinedData, totalPages }); diff --git a/src/routes/mydex/+page.svelte b/src/routes/mydex/+page.svelte index d2e8199..a024599 100644 --- a/src/routes/mydex/+page.svelte +++ b/src/routes/mydex/+page.svelte @@ -16,6 +16,8 @@ let creatingRecords = false; let totalRecordsCreated = 0; let failedToLoad = false; + let catchRegion = ''; + let catchGame = ''; let localUser: User | null; const unsubscribe = user.subscribe((value) => { @@ -39,7 +41,7 @@ combinedData = null; const response = await fetch( - `/api/combined-data?page=${currentPage}&limit=${itemsPerPage}&enableForms=${showForms}` + `/api/combined-data?page=${currentPage}&limit=${itemsPerPage}&enableForms=${showForms}®ion=${catchRegion}&game=${catchGame}` ); const data = await response.json(); @@ -150,17 +152,90 @@