TheMathemagicians/sevenkeys/database/cardstoragelocation.go

67 lines
1.3 KiB
Go
Raw Normal View History

package database
2024-06-01 21:18:33 +00:00
import (
"database/sql"
)
type CardLocation struct {
Id int
CardPrintingId string
StorageAreaId int
Position int
}
func GetLastPositionInStorageArea(db *sql.DB, storageAreaId int) (int, error) {
query := "SELECT Position FROM CardLocation WHERE StorageAreaId = ? ORDER BY Position DESC LIMIT 1;"
var lastPosition int
err := db.QueryRow(query, storageAreaId).Scan(&lastPosition)
if err == sql.ErrNoRows {
return 0, nil
2024-06-01 21:18:33 +00:00
} else if err != nil {
return 0, err
}
return lastPosition, nil
}
func InsertCardLocation(db *sql.DB, storageLocation CardLocation) (int64, error) {
query := `INSERT INTO CardLocation
(CardPrintingId, StorageAreaId, Position)
VALUES (?, ?, ?);`
insert, err := db.Prepare(query)
if err != nil {
return -1, err
}
result, err := insert.Exec(storageLocation.CardPrintingId, storageLocation.StorageAreaId, storageLocation.Position)
if err != nil {
return -1, err
}
2024-05-30 13:33:37 +00:00
id, err := result.LastInsertId()
if err != nil {
return -1, err
}
return id, nil
}
func InsertCardInExistingLocation(db *sql.DB, cardLocation CardLocation) error {
query := `UPDATE CardLocation SET CardPrintingId = ? WHERE Id = ?;`
update, err := db.Prepare(query)
if err != nil {
return err
}
_, err = update.Exec(cardLocation.CardPrintingId, cardLocation.Id)
if err != nil {
return err
}
return nil
}