tcgdex / cards-database

Pokémon Trading Card Game Card (TCG) Database for the TCGdex API. ⭐ Leave a star if the project interest you !
https://www.tcgdex.net
MIT License
131 stars 31 forks source link

enhancement: Searching by set #445

Open fakeheal opened 1 year ago

fakeheal commented 1 year ago

Data affected

Please explain in more details what idea you have

Hi,

I want to add filters for searching cards based on sets. I am wondering which approach is better so I'd love some input.

Approach 1:

# Filters to be used with the Card query
input CardsFilters {
        # ...
    setId: String
        setName: String
       # other set properties
}

and then in Cards.ts:

public static find(lang: SupportedLanguages, params: Partial<Record<keyof SDKCard, any>> = {}, pagination?: Pagination) {
    // ....
    .filter((c) => {
        return objectLoop(params, (it, key) => {
            if(key == 'setId') {
                return c.set.id === it; // new line, same for all other set properties
            }
            return lightCheck(c[key as 'localId'], it)
        });
    })
    // ....
}

Approach 2:

# Filters to be used with the Card query
input CardsFilters {
        # ...   
    set: CardsFiltersSet
}

input CardsFiltersSet {
    id: String
    name: String
    tcgOnline: String
    releaseDate: String
    symbol: String
}

and then in Cards.ts:

public static find(lang: SupportedLanguages, params: Partial<Record<keyof SDKCard, any>> = {}, pagination?: Pagination) {
    // ...
    .filter((c) => {
        return objectLoop(params, (it, key) => {
                        // vvvv new code starts here vvvv
            if(key === 'set') {
                return objectLoop(it, (nestedIt, nestedKey) => {
                    return lightCheck(
                        c[key as 'set'][nestedKey as 'id' | 'name'],
                        nestedIt
                    );
                })
            }
                        // ^^^ new code starts here ^^^
        });
    })
    // ...
}

I have no idea why everything in the code snippets (ts) is so indented...

Aviortheking commented 9 months ago

Hi, sorry for the big delay, the second implementation seems to me to be a better one ! if you can test it and implement it it would be awesome !

fakeheal commented 9 months ago

No worries! I will do that in the upcoming weeks.

Aviortheking commented 8 months ago

Hey, I did some edits to implement new features set for the server, I also made some more place to simplify filtering, it should be easier to implement now, let me know if you need anything !