67 lines
1.3 KiB
Go
67 lines
1.3 KiB
Go
package database
|
|
|
|
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
|
|
} 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
|
|
}
|
|
|
|
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
|
|
}
|