Skip to content

Commit

Permalink
fix: Add missing multi value filtering
Browse files Browse the repository at this point in the history
Signed-off-by: Avior <[email protected]>
  • Loading branch information
Aviortheking committed Oct 29, 2024
1 parent 1a52a6f commit 9cbe9e0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .bruno/cards/advanced-query.bru
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ meta {
}

get {
url: {{BASE_URL}}/v2/en/cards?name=eq:Pikachu&hp=gte:60&hp=lt:70&localId=5&localId=not:tg&id=neq:cel25-5
url: {{BASE_URL}}/v2/en/cards?name=eq:Pikachu|Pichu
body: none
auth: none
}

params:query {
name: eq:Pikachu
name: eq:Pikachu|Pichu
hp: gte:60
hp: lt:70
localId: 5
Expand Down
23 changes: 23 additions & 0 deletions .bruno/cards/multiple.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
meta {
name: Multiple values
type: http
seq: 1
}

get {
url: {{BASE_URL}}/v2/en/cards?name=eq:Pikachu|Pichu&hp=lt:70&localId=not:tg&id=neq:cel25-5
body: none
auth: none
}

params:query {
name: eq:Pikachu|Pichu
hp: lt:70
localId: not:tg
id: neq:cel25-5
}

assert {
res.status: eq 200
res.body: length 85
}
58 changes: 35 additions & 23 deletions server/src/libs/QueryEngine/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,31 +163,43 @@ function parseParam(_key: string, value: string): QueryValues<unknown> {
}
}

function process(item: string | number) {
switch (filter) {
case 'not':
case 'notlike':
return { $not: { $inc: item } }
case 'eq':
return item
case 'neq':
return { $not: item }
case 'gte':
return { $gte: item }
case 'gt':
return { $gt: item }
case 'lt':
return { $lt: item }
case 'lte':
return { $lte: item }
case 'null':
return null
case 'notnull':
return { $not: null }
default:
return { $inc: item }
}
}

if (/^\d+\.?\d*$/g.test(compared)) {
compared = Number.parseFloat(compared)
return process(Number.parseFloat(compared))
}

switch (filter) {
case 'not':
case 'notlike':
return { $not: { $inc: compared }}
case 'eq':
return compared
case 'neq':
return { $not: compared }
case 'gte':
return { $gte: compared }
case 'gt':
return { $gt: compared }
case 'lt':
return { $lt: compared }
case 'lte':
return { $lte: compared }
case 'null':
return null
case 'notnull':
return { $not: null }
default:
return { $inc: compared }
// @deprecated the `,` separator
// TODO: only use the `|` separator
const items = compared.split(compared.includes('|') ? '|' : ',')

if (items.length === 1) {
return process(items[0])
}

return { $or: items.map((it) => process(it)) }
}

0 comments on commit 9cbe9e0

Please sign in to comment.