package database import ( "database/sql" ) type ProductLocation struct { Id int CardtraderBlueprintId int StorageAreaId int Position int CardtraderProductId int } func GetLastPositionInStorageArea(db *sql.DB, storageAreaId int) (int, error) { query := "SELECT Position FROM ProductLocation 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 InsertProductLocation(db *sql.DB, productLocation ProductLocation) (int64, error) { query := `INSERT INTO ProductLocation (CardtraderBlueprintId, StorageAreaId, Position) VALUES (?, ?, ?);` insert, err := db.Prepare(query) if err != nil { return -1, err } result, err := insert.Exec(productLocation.CardtraderBlueprintId, productLocation.StorageAreaId, productLocation.Position) if err != nil { return -1, err } id, err := result.LastInsertId() if err != nil { return -1, err } return id, nil } func InsertProductInExistingLocation(db *sql.DB, productLocation ProductLocation) error { query := `UPDATE ProductLocation SET CardtraderBlueprintId = ? WHERE Id = ?;` update, err := db.Prepare(query) if err != nil { return err } _, err = update.Exec(productLocation.CardtraderBlueprintId, productLocation.Id) if err != nil { return err } return nil }