mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-13 02:53:45 +00:00
feat(*): Enable filtering to either do a forms dex or a regular dex
This commit is contained in:
+2104
-2104
File diff suppressed because it is too large
Load Diff
@@ -3,9 +3,13 @@ import CatchRecordModel, { type CatchRecord } from '$lib/models/CatchRecord';
|
|||||||
import { CombinedData } from '$lib/models/CombinedData';
|
import { CombinedData } from '$lib/models/CombinedData';
|
||||||
|
|
||||||
class CombinedDataRepository {
|
class CombinedDataRepository {
|
||||||
async findCombinedData(page: number = 1, limit: number = 20): Promise<CombinedData[]> {
|
async findCombinedData(
|
||||||
|
page: number = 1,
|
||||||
|
limit: number = 20,
|
||||||
|
enableForms: boolean = true
|
||||||
|
): Promise<CombinedData[]> {
|
||||||
const skip = (page - 1) * limit;
|
const skip = (page - 1) * limit;
|
||||||
const combinedData: CombinedData[] = await PokedexEntryModel.aggregate([
|
const pipeline: any[] = [
|
||||||
{
|
{
|
||||||
$lookup: {
|
$lookup: {
|
||||||
from: 'catchrecords',
|
from: 'catchrecords',
|
||||||
@@ -29,7 +33,17 @@ class CombinedDataRepository {
|
|||||||
},
|
},
|
||||||
{ $skip: skip },
|
{ $skip: skip },
|
||||||
{ $limit: limit }
|
{ $limit: limit }
|
||||||
]).exec();
|
];
|
||||||
|
|
||||||
|
if (!enableForms) {
|
||||||
|
pipeline.unshift({
|
||||||
|
$match: {
|
||||||
|
'boxPlacement.box': { $ne: null }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const combinedData: CombinedData[] = await PokedexEntryModel.aggregate(pipeline).exec();
|
||||||
|
|
||||||
// Filter out entries with no catch records
|
// Filter out entries with no catch records
|
||||||
const filteredData = combinedData.filter((data) => data.catchRecord);
|
const filteredData = combinedData.filter((data) => data.catchRecord);
|
||||||
@@ -40,8 +54,14 @@ class CombinedDataRepository {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
async countCombinedData(): Promise<number> {
|
async countCombinedData(enableForms: boolean): Promise<number> {
|
||||||
return PokedexEntryModel.countDocuments().exec();
|
const filter: any = {};
|
||||||
|
|
||||||
|
if (!enableForms) {
|
||||||
|
filter['boxPlacement.box'] = { $ne: null };
|
||||||
|
}
|
||||||
|
|
||||||
|
return PokedexEntryModel.countDocuments(filter).exec();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,16 +5,17 @@ import CombinedDataRepository from '$lib/repositories/CombinedDataRepository';
|
|||||||
export const GET = async ({ url }) => {
|
export const GET = async ({ url }) => {
|
||||||
const page = parseInt(url.searchParams.get('page') || '1', 10);
|
const page = parseInt(url.searchParams.get('page') || '1', 10);
|
||||||
const limit = parseInt(url.searchParams.get('limit') || '20', 10);
|
const limit = parseInt(url.searchParams.get('limit') || '20', 10);
|
||||||
|
const enableForms = url.searchParams.get('enableForms') === 'true';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await dbConnect();
|
await dbConnect();
|
||||||
const repo = new CombinedDataRepository();
|
const repo = new CombinedDataRepository();
|
||||||
const combinedData = await repo.findCombinedData(page, limit);
|
const combinedData = await repo.findCombinedData(page, limit, enableForms);
|
||||||
|
|
||||||
if (combinedData.length === 0) {
|
if (combinedData.length === 0) {
|
||||||
return json({ error: 'No combined data found' }, { status: 404 });
|
return json({ error: 'No combined data found' }, { status: 404 });
|
||||||
}
|
}
|
||||||
const totalCount = await repo.countCombinedData();
|
const totalCount = await repo.countCombinedData(enableForms);
|
||||||
const totalPages = Math.ceil(totalCount / limit);
|
const totalPages = Math.ceil(totalCount / limit);
|
||||||
|
|
||||||
return json({ combinedData, totalPages });
|
return json({ combinedData, totalPages });
|
||||||
|
|||||||
@@ -30,14 +30,17 @@
|
|||||||
showOrigins = !showOrigins;
|
showOrigins = !showOrigins;
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleForms() {
|
async function toggleForms() {
|
||||||
showForms = !showForms;
|
showForms = !showForms;
|
||||||
|
await getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getData() {
|
async function getData() {
|
||||||
combinedData = null;
|
combinedData = null;
|
||||||
|
|
||||||
const response = await fetch(`/api/combined-data?page=${currentPage}&limit=${itemsPerPage}`);
|
const response = await fetch(
|
||||||
|
`/api/combined-data?page=${currentPage}&limit=${itemsPerPage}&enableForms=${showForms}`
|
||||||
|
);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
if (data.error) {
|
if (data.error) {
|
||||||
@@ -156,6 +159,7 @@
|
|||||||
</ul> -->
|
</ul> -->
|
||||||
<div class="mb-4">
|
<div class="mb-4">
|
||||||
<button class="btn btn-sm" on:click={() => toggleForms()}>Toggle Forms</button>
|
<button class="btn btn-sm" on:click={() => toggleForms()}>Toggle Forms</button>
|
||||||
|
{showForms ? 'On' : 'Off'}
|
||||||
<button class="btn btn-sm" on:click={() => toggleOrigins()}>Toggle Origins</button>
|
<button class="btn btn-sm" on:click={() => toggleOrigins()}>Toggle Origins</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
+2104
-2104
File diff suppressed because it is too large
Load Diff
+869
-869
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user