1
0
mirror of https://github.com/tcgdex/cards-database.git synced 2025-07-29 11:09:51 +00:00

feat: Add better sorting/filtering/pagination (#458)

This commit is contained in:
2024-01-03 02:27:56 +01:00
committed by GitHub
parent 034b7e2cec
commit 5c8ca20a41
13 changed files with 399 additions and 178 deletions

View File

@ -1,7 +1,7 @@
import { objectLoop } from '@dzeio/object-util'
import { Card as SDKCard, CardResume, SupportedLanguages } from '@tcgdex/sdk'
import { Pagination } from '../../interfaces'
import { lightCheck } from '../../util'
import { CardResume, Card as SDKCard, SupportedLanguages } from '@tcgdex/sdk'
import { Query } from '../../interfaces'
import { handlePagination, handleSort, handleValidation } from '../../util'
import Set from './Set'
type LocalCard = Omit<SDKCard, 'set'> & {set: () => Set}
@ -55,40 +55,25 @@ export default class Card implements LocalCard {
})
}
public set(): Set {
return Set.findOne(this.lang, {id: this.card.set.id}) as Set
return Set.findOne(this.lang, {filters: { id: this.card.set.id }}) as Set
}
public static find(lang: SupportedLanguages, params: Partial<Record<keyof SDKCard, any>> = {}, pagination?: Pagination) {
let list : Array<SDKCard> = (require(`../../../generated/${lang}/cards.json`) as Array<SDKCard>)
.filter((c) => objectLoop(params, (it, key) => {
return lightCheck(c[key as 'localId'], it)
}))
if (pagination) {
list = list
.splice(pagination.count * pagination.page - 1, pagination.count)
}
return list.map((it) => new Card(lang, it))
}
public static raw(lang: SupportedLanguages): Array<SDKCard> {
public static getAll(lang: SupportedLanguages): Array<SDKCard> {
return require(`../../../generated/${lang}/cards.json`)
}
public static findOne(lang: SupportedLanguages, params: Partial<Record<keyof SDKCard, any>> = {}) {
const res = (require(`../../../generated/${lang}/cards.json`) as Array<SDKCard>).find((c) => {
return objectLoop(params, (it, key) => {
if (key === 'set' && typeof it === 'string') {
return (c['set'].id === it || lightCheck(c['set'].name, it))
}
return lightCheck(c[key as 'localId'], it)
})
})
if (!res) {
public static find(lang: SupportedLanguages, query: Query<SDKCard>) {
return handlePagination(handleSort(handleValidation(this.getAll(lang), query), query), query)
.map((it) => new Card(lang, it))
}
public static findOne(lang: SupportedLanguages, query: Query<SDKCard>) {
const res = handleValidation(this.getAll(lang), query)
if (res.length === 0) {
return undefined
}
return new Card(lang, res)
return new Card(lang, res[0])
}
public resume(): CardResume {

View File

@ -1,7 +1,7 @@
import { objectLoop } from '@dzeio/object-util'
import { Serie as SDKSerie, SerieResume, SupportedLanguages } from '@tcgdex/sdk'
import { Pagination } from '../../interfaces'
import { lightCheck } from '../../util'
import { Query } from '../../interfaces'
import { handlePagination, handleSort, handleValidation } from '../../util'
import Set from './Set'
type LocalSerie = Omit<SDKSerie, 'sets'> & {sets: () => Array<Set>}
@ -25,34 +25,24 @@ export default class Serie implements LocalSerie {
}
public sets(): Array<Set> {
return this.serie.sets.map((s) => Set.findOne(this.lang, {id: s.id}) as Set)
return this.serie.sets.map((s) => Set.findOne(this.lang, {filters: { id: s.id }}) as Set)
}
public static find(lang: SupportedLanguages, params: Partial<Record<keyof SDKSerie, any>> = {}, pagination?: Pagination) {
let list = (require(`../../../generated/${lang}/series.json`) as Array<SDKSerie>)
.filter((c) => objectLoop(params, (it, key) => {
if (key === 'id') return c[key] === it
return lightCheck(c[key as 'id'], it)
}))
if (pagination) {
list = list
.splice(pagination.count * pagination.page - 1, pagination.count)
}
return list.map((it) => new Serie(lang, it))
public static getAll(lang: SupportedLanguages): Array<SDKSerie> {
return require(`../../../generated/${lang}/series.json`)
}
public static findOne(lang: SupportedLanguages, params: Partial<Record<keyof Serie, any>> = {}): Serie | undefined {
const res = (require(`../../../generated/${lang}/series.json`) as Array<SDKSerie>)
.find((c) => {
return objectLoop(params, (it, key) => {
if (key === 'id') return c[key] === it
return lightCheck(c[key as 'id'], it)
})
})
if (!res) {
public static find(lang: SupportedLanguages, query: Query<SDKSerie>) {
return handlePagination(handleSort(handleValidation(this.getAll(lang), query), query), query)
.map((it) => new Serie(lang, it))
}
public static findOne(lang: SupportedLanguages, query: Query<SDKSerie>) {
const res = handleValidation(this.getAll(lang), query)
if (res.length === 0) {
return undefined
}
return new Serie(lang, res)
return new Serie(lang, res[0])
}
public resume(): SerieResume {

View File

@ -1,7 +1,7 @@
import { objectLoop } from '@dzeio/object-util'
import { Set as SDKSet, SetResume, SupportedLanguages } from '@tcgdex/sdk'
import { Pagination } from '../../interfaces'
import { lightCheck } from '../../util'
import { Query } from '../../interfaces'
import { handlePagination, handleSort, handleValidation } from '../../util'
import Card from './Card'
import Serie from './Serie'
@ -39,45 +39,28 @@ export default class Set implements LocalSet {
symbol?: string | undefined
public serie(): Serie {
return Serie.findOne(this.lang, {id: this.set.serie.id}) as Serie
return Serie.findOne(this.lang, {filters: { id: this.set.serie.id }}) as Serie
}
public cards(): Array<Card> {
return this.set.cards.map((s) => Card.findOne(this.lang, {id: s.id}) as Card)
return this.set.cards.map((s) => Card.findOne(this.lang, { filters: { id: s.id }}) as Card)
}
public static find(lang: SupportedLanguages, params: Partial<Record<keyof SDKSet, any>> = {}, pagination?: Pagination) {
let list = (require(`../../../generated/${lang}/sets.json`) as Array<SDKSet>)
.filter((c) => objectLoop(params, (it, key) => {
if (key === 'id' || key === 'name') {
return c[key as 'id'].toLowerCase() === it.toLowerCase()
} else if (typeof it === 'string') {
return c[key as 'id'].toLowerCase().includes(it.toLowerCase())
}
return lightCheck(c[key as 'id'], it)
}))
if (pagination) {
list = list
.splice(pagination.count * pagination.page - 1, pagination.count)
}
return list.map((it) => new Set(lang, it))
public static getAll(lang: SupportedLanguages): Array<SDKSet> {
return require(`../../../generated/${lang}/sets.json`)
}
public static findOne(lang: SupportedLanguages, params: Partial<Record<keyof Set, any>> = {}) {
const res = (require(`../../../generated/${lang}/sets.json`) as Array<SDKSet>).find((c) => {
return objectLoop(params, (it, key) => {
if (key === 'id' || key === 'name') {
return c[key as 'id'].toLowerCase() === it.toLowerCase()
} else if (typeof it === 'string') {
return c[key as 'id'].toLowerCase().includes(it.toLowerCase())
}
return lightCheck(c[key as 'id'], it)
})
})
if (!res) {
public static find(lang: SupportedLanguages, query: Query<SDKSet>) {
return handlePagination(handleSort(handleValidation(this.getAll(lang), query), query), query)
.map((it) => new Set(lang, it))
}
public static findOne(lang: SupportedLanguages, query: Query<SDKSet>) {
const res = handleValidation(this.getAll(lang), query)
if (res.length === 0) {
return undefined
}
return new Set(lang, res)
return new Set(lang, res[0])
}
public resume(): SetResume {