TheMathemagicians/sevenkeys/database/operations/selects.go

87 lines
2.0 KiB
Go

package operations
import (
"database/sql"
"fmt"
"sevenkeys/database/entities"
"strings"
"time"
)
func GetGamepieceByName(db *sql.DB, name string) (entities.Gamepiece, error) {
var gamepiece entities.Gamepiece
query := "SELECT Id, Name FROM Gamepiece WHERE Name = ?;"
err := db.QueryRow(query, name).Scan(&gamepiece.Id, &gamepiece.Name)
return gamepiece, err
}
func GetCacheTimestampByType(db *sql.DB, cacheType string) (time.Time, error) {
var timestamp string
query := "SELECT Stamp FROM CacheTimestamp WHERE CacheType = ?;"
err := db.QueryRow(query, cacheType).Scan(&timestamp)
if err == sql.ErrNoRows {
return time.Unix(0, 0), nil
}
stamp, err := time.Parse("2006-01-02 15:04:05", timestamp)
if err != nil {
return time.Unix(0, 0), err
}
return stamp, err
}
func GetCardSearchOptions(db *sql.DB) ([]string, error) {
var searchOptions []string
query := "SELECT Id, Name, SetCode, HasFoil, HasNonFoil, CollectorNumber, Language FROM CardPrinting;"
rows, err := db.Query(query)
defer rows.Close()
if err != nil {
return searchOptions, err
}
var printing entities.CardPrinting
for rows.Next() {
err := rows.Scan(&printing.Id,
&printing.Name,
&printing.SetCode,
&printing.HasFoil,
&printing.HasNonFoil,
&printing.CollectorNumber,
&printing.Language)
if err != nil {
return searchOptions, err
}
// TODO: Make this configurable to be able to handle non-English cards
if printing.Language != "en" {
continue
}
if printing.HasNonFoil {
searchOption := fmt.Sprintf("%s | %s (%s %s)",
printing.Id,
printing.Name,
strings.ToUpper(printing.SetCode),
printing.CollectorNumber)
searchOptions = append(searchOptions, searchOption)
}
if printing.HasFoil {
searchOption := fmt.Sprintf("%s | %s (%s %s) FOIL",
printing.Id,
printing.Name,
strings.ToUpper(printing.SetCode),
printing.CollectorNumber)
searchOptions = append(searchOptions, searchOption)
}
}
return searchOptions, nil
}