Add database code for storage

This commit is contained in:
The Magician 2024-05-27 12:05:52 +01:00
parent 1dc003211a
commit 42a019b730
3 changed files with 46 additions and 0 deletions

View File

@ -0,0 +1,9 @@
package entities
type CardStorageLocation struct {
Id int
CardPrintingId string
StorageBox string
Source string
Position int
}

View File

@ -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
}

View File

@ -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
);