TheMathemagicians/sevenkeys/database/operations/selects.go

87 lines
2.0 KiB
Go
Raw Normal View History

2024-04-28 14:13:47 +00:00
package operations
import (
"database/sql"
"fmt"
2024-04-28 14:13:47 +00:00
"sevenkeys/database/entities"
"strings"
2024-05-19 14:01:18 +00:00
"time"
2024-04-28 14:13:47 +00:00
)
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
}
2024-05-19 14:01:18 +00:00
func GetCacheTimestampByType(db *sql.DB, cacheType string) (time.Time, error) {
var timestamp string
2024-05-21 13:31:20 +00:00
query := "SELECT Stamp FROM CacheTimestamp WHERE CacheType = ?;"
2024-05-19 14:01:18 +00:00
err := db.QueryRow(query, cacheType).Scan(&timestamp)
if err == sql.ErrNoRows {
2024-05-21 14:27:05 +00:00
return time.Unix(0, 0), nil
2024-05-19 14:01:18 +00:00
}
stamp, err := time.Parse("2006-01-02 15:04:05", timestamp)
if err != nil {
2024-05-21 13:31:20 +00:00
return time.Unix(0, 0), err
2024-05-19 14:01:18 +00:00
}
return stamp, err
}
2024-05-27 12:29:00 +00:00
func GetCardSearchOptions(db *sql.DB) ([]string, error) {
var searchOptions []string
query := "SELECT Id, Name, SetCode, HasFoil, HasNonFoil, CollectorNumber, Language FROM CardPrinting;"
2024-05-27 12:29:00 +00:00
rows, err := db.Query(query)
defer rows.Close()
if err != nil {
return searchOptions, err
2024-05-27 12:29:00 +00:00
}
var printing entities.CardPrinting
2024-05-27 12:29:00 +00:00
for rows.Next() {
err := rows.Scan(&printing.Id,
&printing.Name,
&printing.SetCode,
&printing.HasFoil,
&printing.HasNonFoil,
&printing.CollectorNumber,
&printing.Language)
2024-05-27 12:29:00 +00:00
if err != nil {
return searchOptions, err
}
// TODO: Make this configurable to be able to handle non-English cards
if printing.Language != "en" {
continue
2024-05-27 12:29:00 +00:00
}
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)
}
2024-05-27 12:29:00 +00:00
}
return searchOptions, nil
2024-05-27 12:29:00 +00:00
}