diff --git a/pkg/serra/add.go b/pkg/serra/add.go index fd52cde..c91b4f4 100644 --- a/pkg/serra/add.go +++ b/pkg/serra/add.go @@ -156,7 +156,7 @@ func addCards(cards []string, unique bool, count int64) error { } // Write card to mongodb - var total int64 = 0 + var total int64 if foil { c.SerraCountFoil = count c.SerraCountFoilDeck = 0 diff --git a/pkg/serra/card.go b/pkg/serra/card.go index 0c983a0..a168d8d 100644 --- a/pkg/serra/card.go +++ b/pkg/serra/card.go @@ -23,7 +23,7 @@ func init() { cardCmd.Flags().StringVarP(&name, "name", "n", "", "Name of the card (regex compatible)") cardCmd.Flags().Int64VarP(&cmc, "cmc", "m", -1, "Cumulative mana cost of card") cardCmd.Flags().StringVarP(&color, "color", "i", "", "Color identity of card (w,u,b,r,g)") - cardCmd.Flags().StringVarP(&oracle, "oracle", "o", "", "Contains string in card text") + cardCmd.Flags().StringArrayVarP(&oracle, "oracle", "o", []string{}, "Contains string in card text") cardCmd.Flags().StringVarP(&cardType, "type", "t", "", "Contains string in card type line") cardCmd.Flags().Int64VarP(&count, "min-count", "c", 0, "Occource more than X in your collection") cardCmd.Flags().BoolVarP(&detail, "detail", "d", false, "Show details for cards (url)") @@ -73,7 +73,7 @@ func ShowCard(cardids []string) { } } -func Cards(rarity, set, sortby, name, oracle, cardType string, reserved, foil bool, skip, limit int64, omitInDeck bool) []Card { +func Cards(rarity, set, sortby, name string, oracle []string, cardType string, reserved, foil bool, skip, limit int64, omitInDeck bool) []Card { client := storageConnect() coll := &Collection{client.Database("serra").Collection("cards")} defer storageDisconnect(client) @@ -125,8 +125,10 @@ func Cards(rarity, set, sortby, name, oracle, cardType string, reserved, foil bo filter = append(filter, bson.E{"cmc", cmc}) } - if len(oracle) > 0 { - filter = append(filter, bson.E{"oracletext", bson.D{{"$regex", ".*" + oracle + ".*"}, {"$options", "i"}}}) + for _, o:= range oracle { + if len(o) > 0 { + filter = append(filter, bson.E{"oracletext", bson.D{{"$regex", ".*" + o + ".*"}, {"$options", "i"}}}) + } } if len(cardType) > 0 { diff --git a/pkg/serra/helpers.go b/pkg/serra/helpers.go index c5c53c7..bfe3045 100644 --- a/pkg/serra/helpers.go +++ b/pkg/serra/helpers.go @@ -312,12 +312,11 @@ func coloredValue(value float64) string { func askConfirmation(card *Card) bool { drawImage(card) - fmt.Println("Is this correct (y/n)?") - - var char = 'x' - for char != 'y' && char != 'n' { - fmt.Scanf("%c\n", &char) + response := "some invalid response" + for response != "y" && response != "n" { + fmt.Println("Is this correct (y/n)?") + fmt.Scanln(&response) } - return char == 'y' + return response == "y" } diff --git a/pkg/serra/initialize.go b/pkg/serra/initialize.go new file mode 100644 index 0000000..eab1ab3 --- /dev/null +++ b/pkg/serra/initialize.go @@ -0,0 +1,56 @@ +package serra + +import ( + "context" + + "github.com/spf13/cobra" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" +) + +func init() { + rootCmd.AddCommand(initializeCmd) +} + +var initializeCmd = &cobra.Command{ + Use: "init", + Short: "Initialize database", + Long: "Create database optimization structures", + SilenceErrors: true, + RunE: func(cmd *cobra.Command, _ []string) error { + return initializeDatabase() + }, +} + +func initializeDatabase() error { + client := storageConnect() + l := Logger() + defer storageDisconnect(client) + + // create sets collection + client.Database("serra").Collection("sets") + + // create total collection and insert entry if it doesn't exist + totalcoll := &Collection{client.Database("serra").Collection("total")} + totalcoll.InsertOne(context.TODO(), Total{ID: "1", Value: []PriceEntry{}}) + + cardscoll := &Collection{client.Database("serra").Collection("cards")} + + fieldsToIndex := []string{ "name", "prices.eur", "prices.usd", "collectornumber", "serra_created" } + for _, fieldName := range fieldsToIndex { + if createIndex(cardscoll, fieldName) != nil { + l.Warnf("Failed to create index on field '%s'", fieldName) + } + } + + return nil +} + +func createIndex(coll *Collection, fieldName string) error { + indexModel := mongo.IndexModel{ + Keys: bson.D{{fieldName, 1}}, + } + + _, err := coll.Indexes().CreateOne(context.TODO(), indexModel) + return err +} diff --git a/pkg/serra/root.go b/pkg/serra/root.go index e2ab1ed..c49758a 100644 --- a/pkg/serra/root.go +++ b/pkg/serra/root.go @@ -24,7 +24,7 @@ var ( limit float64 name string omitInDeck bool - oracle string + oracle []string port uint64 rarity string reserved bool @@ -44,7 +44,6 @@ var rootCmd = &cobra.Command{ } func Execute() { - l := Logger() if err := rootCmd.Execute(); err != nil { l.Fatal(err) diff --git a/pkg/serra/web.go b/pkg/serra/web.go index a98f3a6..06f18e0 100644 --- a/pkg/serra/web.go +++ b/pkg/serra/web.go @@ -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, false) + cards := Cards("", query.Set, query.Sort, query.Name, []string{}, "", false, false, query.Page*int64(limit), limit, false) // Construct quick way for counting results filter := bson.D{}