Update primary key type in CardPrinting to fix duplicate update bug

This commit is contained in:
The Magician 2024-06-03 17:08:15 +01:00
parent 343421362c
commit 14e5a81ca6
7 changed files with 18 additions and 14 deletions

View File

@ -5,7 +5,7 @@ import (
) )
type CardPrinting struct { type CardPrinting struct {
Id int Id string
Name string Name string
SetCode string SetCode string
IsFoil bool IsFoil bool
@ -16,6 +16,7 @@ type CardPrinting struct {
func InsertCardPrinting(db *sql.DB, cardPrinting CardPrinting) error { func InsertCardPrinting(db *sql.DB, cardPrinting CardPrinting) error {
query := `INSERT INTO CardPrinting ( query := `INSERT INTO CardPrinting (
Id,
Name, Name,
SetCode, SetCode,
IsFoil, IsFoil,
@ -30,7 +31,7 @@ func InsertCardPrinting(db *sql.DB, cardPrinting CardPrinting) error {
return err return err
} }
_, err = insert.Exec(cardPrinting.Name, cardPrinting.SetCode, cardPrinting.IsFoil, cardPrinting.IsPromo, cardPrinting.CollectorNumber, cardPrinting.Language) _, err = insert.Exec(cardPrinting.Id, cardPrinting.Name, cardPrinting.SetCode, cardPrinting.IsFoil, cardPrinting.IsPromo, cardPrinting.CollectorNumber, cardPrinting.Language)
if err != nil { if err != nil {
return err return err
} }

View File

@ -6,7 +6,7 @@ import (
type CardStorageLocation struct { type CardStorageLocation struct {
Id int Id int
CardPrintingId int CardPrintingId string
StorageBox string StorageBox string
Position int Position int
Source string Source string

View File

@ -15,7 +15,7 @@ CREATE TABLE IF NOT EXISTS ExpansionSet (
); );
CREATE TABLE IF NOT EXISTS CardPrinting ( CREATE TABLE IF NOT EXISTS CardPrinting (
Id INT AUTO_INCREMENT PRIMARY KEY, Id VARCHAR(37) PRIMARY KEY, -- GUID, plus one character for foil/nonfoil
Name VARCHAR(150) NOT NULL, Name VARCHAR(150) NOT NULL,
SetCode VARCHAR(6) NOT NULL, SetCode VARCHAR(6) NOT NULL,
FOREIGN KEY (SetCode) REFERENCES ExpansionSet(SetCode), FOREIGN KEY (SetCode) REFERENCES ExpansionSet(SetCode),
@ -27,7 +27,7 @@ CREATE TABLE IF NOT EXISTS CardPrinting (
CREATE TABLE IF NOT EXISTS CardStorageLocation ( CREATE TABLE IF NOT EXISTS CardStorageLocation (
Id INT AUTO_INCREMENT PRIMARY KEY, Id INT AUTO_INCREMENT PRIMARY KEY,
CardPrintingId INT NOT NULL, CardPrintingId VARCHAR(37) NOT NULL,
FOREIGN KEY (CardPrintingId) REFERENCES CardPrinting(Id), FOREIGN KEY (CardPrintingId) REFERENCES CardPrinting(Id),
StorageBox VARCHAR(20) NOT NULL, StorageBox VARCHAR(20) NOT NULL,
Position INT NOT NULL, Position INT NOT NULL,

View File

@ -1,6 +1,7 @@
package scryfall package scryfall
type Card struct { type Card struct {
Id string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Set string `json:"set"` Set string `json:"set"`
Games []string `json:"games"` Games []string `json:"games"`

View File

@ -10,12 +10,12 @@ import (
"strings" "strings"
) )
// The SearchOptions type is a map of `string`s (which can be searched using fzf) // The SearchOptions type is a map of `string` keys (which can be searched using fzf)
// to `int`s (which represent a primary key in the CardPrintings table) // to `string` values (which represent a primary key in the CardPrintings table)
type SearchOptions map[string]int type SearchOptions map[string]string
func GetAllSearchOptions(db *sql.DB) (SearchOptions, error) { func GetAllSearchOptions(db *sql.DB) (SearchOptions, error) {
var searchOptions SearchOptions = make(map[string]int) var searchOptions SearchOptions = make(map[string]string)
cardPrintings, err := database.GetAllCardPrintings(db) cardPrintings, err := database.GetAllCardPrintings(db)
if err != nil { if err != nil {
@ -39,13 +39,13 @@ func GetAllSearchOptions(db *sql.DB) (SearchOptions, error) {
return searchOptions, err return searchOptions, err
} }
func Search(searchOptions SearchOptions) (int, string, error) { func Search(searchOptions SearchOptions) (string, string, error) {
cmd := exec.Command("fzf") cmd := exec.Command("fzf")
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
fzfStdin, err := cmd.StdinPipe() fzfStdin, err := cmd.StdinPipe()
if err != nil { if err != nil {
return -1, "", err return "", "", err
} }
go func() { go func() {
@ -57,7 +57,7 @@ func Search(searchOptions SearchOptions) (int, string, error) {
fzfOutput, err := cmd.Output() fzfOutput, err := cmd.Output()
if err != nil { if err != nil {
return -1, "", err return "", "", err
} }
key := strings.TrimSuffix(string(fzfOutput), "\n") key := strings.TrimSuffix(string(fzfOutput), "\n")

View File

@ -127,6 +127,7 @@ func getCardPrintings(card scryfall.Card) []database.CardPrinting {
if card.Foil { if card.Foil {
printings = append(printings, database.CardPrinting{ printings = append(printings, database.CardPrinting{
Id: card.Id + "f",
Name: card.Name, Name: card.Name,
SetCode: card.Set, SetCode: card.Set,
IsFoil: true, IsFoil: true,
@ -138,6 +139,7 @@ func getCardPrintings(card scryfall.Card) []database.CardPrinting {
if card.NonFoil { if card.NonFoil {
printings = append(printings, database.CardPrinting{ printings = append(printings, database.CardPrinting{
Id: card.Id + "n",
Name: card.Name, Name: card.Name,
SetCode: card.Set, SetCode: card.Set,
IsFoil: false, IsFoil: false,

View File

@ -51,7 +51,7 @@ func main() {
searchOptions, err := logic.GetAllSearchOptions(db) searchOptions, err := logic.GetAllSearchOptions(db)
logic.Check(err) logic.Check(err)
var selectedCardId int var selectedCardId string
var selectedCardSearchOption string = "None" var selectedCardSearchOption string = "None"
var lastOutput string var lastOutput string
for { for {
@ -77,7 +77,7 @@ func main() {
continue continue
case "i": case "i":
if selectedCardId == 0 { if selectedCardId == "" {
lastOutput = "No selected card, please search for one." lastOutput = "No selected card, please search for one."
continue continue
} }