2024-05-30 14:19:31 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2024-05-31 19:32:44 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-05-30 14:19:31 +00:00
|
|
|
"sevenkeys/database"
|
2024-05-31 19:32:44 +00:00
|
|
|
"strings"
|
2024-05-30 14:19:31 +00:00
|
|
|
)
|
|
|
|
|
2024-06-11 11:47:55 +00:00
|
|
|
type Triadic int
|
|
|
|
|
|
|
|
const (
|
|
|
|
True Triadic = iota
|
|
|
|
False
|
|
|
|
Either
|
|
|
|
)
|
|
|
|
|
|
|
|
type SearchCriteria struct {
|
2024-10-28 19:47:23 +00:00
|
|
|
SetCode string
|
|
|
|
CollectorNumber string
|
|
|
|
Foil Triadic
|
|
|
|
Promo Triadic
|
|
|
|
Language string
|
2024-06-11 11:47:55 +00:00
|
|
|
}
|
|
|
|
|
2024-10-28 19:47:23 +00:00
|
|
|
type CardPrintingSearchOptions map[string]string
|
2024-05-30 14:19:31 +00:00
|
|
|
|
2024-10-28 19:47:23 +00:00
|
|
|
func GetAllCardPrintingSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (CardPrintingSearchOptions, error) {
|
|
|
|
var searchOptions CardPrintingSearchOptions = make(CardPrintingSearchOptions)
|
2024-05-30 14:19:31 +00:00
|
|
|
|
|
|
|
cardPrintings, err := database.GetAllCardPrintings(db)
|
|
|
|
if err != nil {
|
|
|
|
return searchOptions, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, printing := range cardPrintings {
|
2024-06-11 11:47:55 +00:00
|
|
|
// Filter based on search criteria
|
|
|
|
filter := filterPrinting(printing, searchCriteria)
|
|
|
|
if filter {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct search option string
|
2024-05-30 14:19:31 +00:00
|
|
|
searchString := fmt.Sprintf("%s (%s %s) [%s]", printing.Name, printing.SetCode, printing.CollectorNumber, printing.Language)
|
|
|
|
|
|
|
|
if printing.IsFoil {
|
|
|
|
searchString += " FOIL"
|
|
|
|
}
|
|
|
|
|
|
|
|
if printing.IsPromo {
|
|
|
|
searchString += " PROMO"
|
|
|
|
}
|
|
|
|
|
|
|
|
searchOptions[searchString] = printing.Id
|
|
|
|
}
|
|
|
|
|
|
|
|
return searchOptions, err
|
|
|
|
}
|
2024-05-31 19:32:44 +00:00
|
|
|
|
2024-10-04 12:44:22 +00:00
|
|
|
type StorageSearchOptions map[string]int
|
|
|
|
|
|
|
|
func GetAllStorageSearchOptions(db *sql.DB) (StorageSearchOptions, error) {
|
|
|
|
var searchOptions StorageSearchOptions = make(StorageSearchOptions)
|
|
|
|
|
|
|
|
storedCards, err := database.GetAllStoredCards(db)
|
|
|
|
if err != nil {
|
|
|
|
return searchOptions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, storedCard := range storedCards {
|
|
|
|
searchString := fmt.Sprintf("%s (%s %s) [%s]", storedCard.CardName, storedCard.SetCode, storedCard.CollectorNumber, storedCard.Language)
|
|
|
|
|
|
|
|
if storedCard.IsFoil {
|
|
|
|
searchString += " FOIL"
|
|
|
|
}
|
|
|
|
|
|
|
|
if storedCard.IsPromo {
|
|
|
|
searchString += " PROMO"
|
|
|
|
}
|
|
|
|
|
|
|
|
searchOptions[searchString] = storedCard.CardLocationId
|
|
|
|
}
|
|
|
|
|
|
|
|
return searchOptions, nil
|
|
|
|
}
|
|
|
|
|
2024-09-16 16:59:44 +00:00
|
|
|
func GenericSearch[pk string | int](options map[string]pk) (pk, string, error) {
|
2024-09-07 18:07:48 +00:00
|
|
|
var value pk
|
|
|
|
|
2024-05-31 19:32:44 +00:00
|
|
|
cmd := exec.Command("fzf")
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
fzfStdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
2024-09-16 16:59:44 +00:00
|
|
|
return value, "", err
|
2024-05-31 19:32:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer fzfStdin.Close()
|
2024-09-07 18:07:48 +00:00
|
|
|
for option, _ := range options {
|
|
|
|
io.WriteString(fzfStdin, option+"\n")
|
2024-05-31 19:32:44 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
fzfOutput, err := cmd.Output()
|
|
|
|
if err != nil {
|
2024-09-16 16:59:44 +00:00
|
|
|
return value, "", err
|
2024-05-31 19:32:44 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 18:07:48 +00:00
|
|
|
searchResult := strings.TrimSuffix(string(fzfOutput), "\n")
|
|
|
|
value = options[searchResult]
|
2024-09-16 16:59:44 +00:00
|
|
|
return value, searchResult, nil
|
2024-05-31 19:32:44 +00:00
|
|
|
}
|