diff --git a/sevenkeys/database/entities/cardstorage.go b/sevenkeys/database/entities/cardstorage.go new file mode 100644 index 0000000..47835d9 --- /dev/null +++ b/sevenkeys/database/entities/cardstorage.go @@ -0,0 +1,9 @@ +package entities + +type CardStorageLocation struct { + Id int + CardPrintingId string + StorageBox string + Source string + Position int +} diff --git a/sevenkeys/database/operations/inserts.go b/sevenkeys/database/operations/inserts.go index af42f71..e4d2ed5 100644 --- a/sevenkeys/database/operations/inserts.go +++ b/sevenkeys/database/operations/inserts.go @@ -65,3 +65,31 @@ func InsertCard(db *sql.DB, card types.Card) error { return nil } + +func InsertCardStorageLocation(db *sql.DB, cardPrintingId string, storageBox string, source string) error { + var lastPosition int + getLastPositionQuery := `SELECT Position FROM CardStorageLocation WHERE StorageBox = ? ORDER BY Position DESC LIMIT 1;` + err := db.QueryRow(query, storageBox).Scan(&lastPosition) + + var nextPosition int + if err == sql.ErrNoRows { + nextPosition = 1 + } else { + nextPosition = lastPosition + 1 + } + + insertQuery := `INSERT INTO CardStorageLocation + (CardPrintingId, StorageBox, Source, Position) + VALUES (?, ?, ?, ?);` + insert, err := db.Prepare(insertQuery) + if err != nil { + return err + } + + _, err = insert.Exec(cardPrintingId, storageBox, source, nextPosition) + if err != nil { + return err + } + + return nil +} diff --git a/sevenkeys/sql/createdb.sql b/sevenkeys/sql/createdb.sql index 824c43a..290b10a 100644 --- a/sevenkeys/sql/createdb.sql +++ b/sevenkeys/sql/createdb.sql @@ -27,3 +27,12 @@ CREATE TABLE IF NOT EXISTS CardPrinting ( CollectorNumber VARCHAR(10) NOT NULL, Language VARCHAR(3) NOT NULL ); + +CREATE TABLE IF NOT EXISTS CardStorageLocation ( + Id INT AUTO_INCREMENT PRIMARY KEY, + CardPrintingId VARCHAR(36) NOT NULL, + FOREIGN KEY (CardPrintingId) REFERENCES CardPrinting(Id), + StorageBox VARCHAR(20) NOT NULL, + Source VARCHAR(100) NULL, + Position INT NOT NULL +);