Fix filtering to use queries rather than ad-hoc code

This commit is contained in:
shylie 2025-12-23 05:35:20 -05:00
parent 6a24cd6546
commit cea9a47de4
3 changed files with 11 additions and 17 deletions

View File

@ -44,7 +44,7 @@ otherwise you'll get a list of cards as a search result.`,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, cards []string) error {
if len(cards) == 0 {
cardList := Cards(rarity, set, sortby, name, oracle, cardType, reserved, foil, 0, 0)
cardList := Cards(rarity, set, sortby, name, oracle, cardType, reserved, foil, 0, 0, omitInDeck)
showCardList(cardList, detail)
} else {
ShowCard(cards)
@ -73,7 +73,7 @@ func ShowCard(cardids []string) {
}
}
func Cards(rarity, set, sortby, name, oracle, cardType string, reserved, foil bool, skip, limit int64) []Card {
func Cards(rarity, set, sortby, name, oracle, cardType string, reserved, foil bool, skip, limit int64, omitInDeck bool) []Card {
client := storageConnect()
coll := &Collection{client.Database("serra").Collection("cards")}
defer storageDisconnect(client)
@ -146,6 +146,13 @@ func Cards(rarity, set, sortby, name, oracle, cardType string, reserved, foil bo
filter = append(filter, bson.E{"serra_count_foil", bson.D{{"$gt", 0}}})
}
if omitInDeck {
filter = append(filter, bson.E{ "$expr", bson.D{{"$or",bson.A{
bson.D{{"$ne", bson.A{"$serra_count", "$serra_count_deck"}}},
bson.D{{"$ne", bson.A{"$serra_count_foil", "$serra_count_foil_deck"}}},
}}}})
}
cards, _ := coll.storageFind(filter, sortStage, skip, limit)
// This is needed because collectornumbers are strings (ie. "23a") but still we
@ -175,24 +182,15 @@ func showCardList(cards []Card, detail bool) {
var total float64
if drawImg {
for _, card := range cards {
if shouldOmit(&card) {
continue
}
drawImage(&card)
}
} else if detail {
for _, card := range cards {
if shouldOmit(&card) {
continue
}
fmt.Printf("* %dx %s%s%s (%s/%s) %s%.2f%s %s %s %s\n", card.SerraCount+card.SerraCountFoil+card.SerraCountEtched, Purple, card.Name, Reset, card.Set, card.CollectorNumber, Yellow, card.getValue(false), getCurrency(), Background, strings.Replace(card.ScryfallURI, "?utm_source=api", "", 1), Reset)
total = total + card.getValue(false)*float64(card.SerraCount) + card.getValue(true)*float64(card.SerraCountFoil)
}
} else {
for _, card := range cards {
if shouldOmit(&card) {
continue
}
fmt.Printf("* %dx (%dx) %s%s%s (%s/%s) %s%.2f%s%s\n",
card.SerraCount+card.SerraCountFoil+card.SerraCountEtched-card.SerraCountDeck-card.SerraCountFoilDeck-card.SerraCountEtchedDeck,
card.SerraCount+card.SerraCountFoil+card.SerraCountEtched,
@ -230,10 +228,6 @@ func showCardDetails(card *Card) error {
return nil
}
func shouldOmit(card *Card) bool {
return omitInDeck && card.SerraCount == card.SerraCountDeck && card.SerraCountFoil == card.SerraCountFoilDeck
}
func drawImage(card *Card) {
fmt.Printf("%s - %s (%s/%s)\n", card.Name, card.SetName, card.Set, card.CollectorNumber)
data, err := base64.StdEncoding.DecodeString(card.SerraImage64)

View File

@ -25,7 +25,7 @@ var exportCmd = &cobra.Command{
Supports multiple output formats depending on where you want to export your collection.`,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
cardList := Cards(rarity, set, sortby, name, oracle, cardType, reserved, foil, 0, 0)
cardList := Cards(rarity, set, sortby, name, oracle, cardType, reserved, foil, 0, 0, false)
// filter out cards that do not reach the minimum amount (--min-count)
// this is done after query result because find query constructed does not support

View File

@ -71,7 +71,7 @@ func landingPage(c *gin.Context) {
sets := Sets("release")
// Fetch all results based on filter criteria
cards := Cards("", query.Set, query.Sort, query.Name, "", "", false, false, query.Page*int64(limit), limit)
cards := Cards("", query.Set, query.Sort, query.Name, "", "", false, false, query.Page*int64(limit), limit, false)
// Construct quick way for counting results
filter := bson.D{}