From 961c75c8a09a77c69748b5347c1bc810231306e4 Mon Sep 17 00:00:00 2001 From: Avior Date: Fri, 2 May 2025 00:31:04 +0200 Subject: [PATCH] fix: dex-id url not returning valid cards & filtering not working using `dexId` (#722) --- .bruno/dex-ids/get-by-dex-id.bru | 21 +++++++++++++++++ .bruno/filtering/end-star-pattern.bru | 20 ++++++++++++++++ .../filtering/searching-using-the-dex-id.bru | 19 +++++++++++++++ .bruno/filtering/start-star-pattern.bru | 20 ++++++++++++++++ server/src/V2/endpoints/jsonEndpoints.ts | 9 ++++++++ server/src/libs/QueryEngine/filter.ts | 23 ++++++++++++++++++- server/src/libs/QueryEngine/parsers.ts | 3 ++- 7 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 .bruno/dex-ids/get-by-dex-id.bru create mode 100644 .bruno/filtering/end-star-pattern.bru create mode 100644 .bruno/filtering/searching-using-the-dex-id.bru create mode 100644 .bruno/filtering/start-star-pattern.bru diff --git a/.bruno/dex-ids/get-by-dex-id.bru b/.bruno/dex-ids/get-by-dex-id.bru new file mode 100644 index 000000000..97217825b --- /dev/null +++ b/.bruno/dex-ids/get-by-dex-id.bru @@ -0,0 +1,21 @@ +meta { + name: Get by Dex ID + type: http + seq: 1 +} + +get { + url: {{BASE_URL}}/v2/en/dex-ids/{{ID}} + body: none + auth: none +} + +vars:pre-request { + ID: 162 +} + +assert { + res.status: eq 200 + res.body.cards.length: gte 8 + res.body.name: eq {{ID}} +} diff --git a/.bruno/filtering/end-star-pattern.bru b/.bruno/filtering/end-star-pattern.bru new file mode 100644 index 000000000..97033ea1c --- /dev/null +++ b/.bruno/filtering/end-star-pattern.bru @@ -0,0 +1,20 @@ +meta { + name: End Star Pattern + type: http + seq: 1 +} + +get { + url: {{BASE_URL}}/v2/en/cards?name=*chu + body: none + auth: inherit +} + +params:query { + name: *chu +} + +assert { + res.body.length: gt 3 + res.body[1].name: neq Pikachu on the Ball +} diff --git a/.bruno/filtering/searching-using-the-dex-id.bru b/.bruno/filtering/searching-using-the-dex-id.bru new file mode 100644 index 000000000..a9d34c0c2 --- /dev/null +++ b/.bruno/filtering/searching-using-the-dex-id.bru @@ -0,0 +1,19 @@ +meta { + name: dexId Search + type: http + seq: 3 +} + +get { + url: {{BASE_URL}}/v2/ja/cards?dexId=eq:357 + body: none + auth: inherit +} + +params:query { + dexId: eq:357 +} + +assert { + res.body.length: eq 3 +} diff --git a/.bruno/filtering/start-star-pattern.bru b/.bruno/filtering/start-star-pattern.bru new file mode 100644 index 000000000..883e45563 --- /dev/null +++ b/.bruno/filtering/start-star-pattern.bru @@ -0,0 +1,20 @@ +meta { + name: Start star Pattern + type: http + seq: 2 +} + +get { + url: {{BASE_URL}}/v2/en/cards?name=fu* + body: none + auth: inherit +} + +params:query { + name: fu* +} + +assert { + res.body.length: gt 3 + res.body[1].name: neq Stufful +} diff --git a/server/src/V2/endpoints/jsonEndpoints.ts b/server/src/V2/endpoints/jsonEndpoints.ts index afe4a29cc..f5e799c80 100644 --- a/server/src/V2/endpoints/jsonEndpoints.ts +++ b/server/src/V2/endpoints/jsonEndpoints.ts @@ -247,6 +247,15 @@ server result = Serie.findOne(lang, { name: id })?.full() } break + case 'dex-ids': { + result = { + name: parseInt(id, 10), + // @ts-expect-error current behavior is normal + cards: Card.find(lang, { dexId: { $eq: parseInt(id, 10) }}) + .map((c) => c.resume()) + } + break + } default: if (!endpointToField[endpoint]) { break diff --git a/server/src/libs/QueryEngine/filter.ts b/server/src/libs/QueryEngine/filter.ts index da8775ff2..524c5a483 100644 --- a/server/src/libs/QueryEngine/filter.ts +++ b/server/src/libs/QueryEngine/filter.ts @@ -387,6 +387,10 @@ function filterItem(value: any, query: QueryValues): boolean { if (typeof query === 'string' && typeof value === 'string') { return query.toLowerCase() === value.toLowerCase() } + + if (Array.isArray(value)) { + return value.includes(query) + } return query === value } @@ -402,10 +406,27 @@ function filterItem(value: any, query: QueryValues): boolean { if (typeof value === 'number' && typeof query.$inc === 'number') { return value === query.$inc } - return (value.toString() as string).toLowerCase().includes(query.$inc!.toString()!.toLowerCase()) + + const valueTmp = value.toString().toLowerCase() + const comp = query.$inc!.toString().toLowerCase() + const ignoreStars = comp.startsWith('*') && comp.endsWith('*') + + if (!ignoreStars && comp.startsWith('*')) { + return valueTmp.endsWith(comp.slice(1)) + } else if (!ignoreStars && comp.endsWith('*')) { + return valueTmp.startsWith(comp.slice(0, -1)) + } else if (ignoreStars) { + return valueTmp.includes(comp.slice(1, -1)) + } + return valueTmp.includes(comp) } if ('$eq' in query) { + // handle searching using an array + if (Array.isArray(value)) { + return value.includes(query.$eq) + } + return query.$eq === value } diff --git a/server/src/libs/QueryEngine/parsers.ts b/server/src/libs/QueryEngine/parsers.ts index 561f3f9f8..17b6fe756 100644 --- a/server/src/libs/QueryEngine/parsers.ts +++ b/server/src/libs/QueryEngine/parsers.ts @@ -164,7 +164,7 @@ function parseParam(_key: string, value: string): QueryValues { case 'notlike': return { $not: { $inc: item } } case 'eq': - return item + return { $eq: item } case 'neq': return { $not: item } case 'gte': @@ -179,6 +179,7 @@ function parseParam(_key: string, value: string): QueryValues { return null case 'notnull': return { $not: null } + case 'like': default: return { $inc: item } }