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 {
Id int
Id string
Name string
SetCode string
IsFoil bool
@ -16,6 +16,7 @@ type CardPrinting struct {
func InsertCardPrinting(db *sql.DB, cardPrinting CardPrinting) error {
query := `INSERT INTO CardPrinting (
Id,
Name,
SetCode,
IsFoil,
@ -30,7 +31,7 @@ func InsertCardPrinting(db *sql.DB, cardPrinting CardPrinting) error {
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 {
return err
}

View File

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

View File

@ -15,7 +15,7 @@ CREATE TABLE IF NOT EXISTS ExpansionSet (
);
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,
SetCode VARCHAR(6) NOT NULL,
FOREIGN KEY (SetCode) REFERENCES ExpansionSet(SetCode),
@ -27,7 +27,7 @@ CREATE TABLE IF NOT EXISTS CardPrinting (
CREATE TABLE IF NOT EXISTS CardStorageLocation (
Id INT AUTO_INCREMENT PRIMARY KEY,
CardPrintingId INT NOT NULL,
CardPrintingId VARCHAR(37) NOT NULL,
FOREIGN KEY (CardPrintingId) REFERENCES CardPrinting(Id),
StorageBox VARCHAR(20) NOT NULL,
Position INT NOT NULL,

View File

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

View File

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

View File

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

View File

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