Fix order of SQL migrations, modify scanner script

This commit is contained in:
The Magician 2025-06-07 15:19:00 +01:00
parent 62c7e621d5
commit a82355cf47
11 changed files with 156 additions and 19 deletions

View File

@ -17,7 +17,7 @@ printf "Storage area name: "
read storageAreaName
ADD_CARDS=0
echo "scantap: Beginning scan loop"
echo "card_scanner: Beginning scan loop"
while true; do
rng="$(cat /dev/random | tr -cd 'a-f0-9' | head -c 32)"
filename="$STORAGE_DIR/$rng.png"
@ -28,7 +28,7 @@ while true; do
if test $? -eq 7; then
if test $ADD_CARDS -eq 0; then
ADD_CARDS=1
echo "scantap: No more cards in feeder" >&2
echo "card_scanner: No more cards in feeder" >&2
fi
# If we have generated a zero-length file, then delete it
@ -44,13 +44,13 @@ while true; do
convert -rotate 180 "$filename" "$filename"
check_error "convert"
cardLocationId="$(./sevenkeys --profile="$profile" store --storagearea="$storageAreaName" --id="00000000-0000-0000-0000-0000000000000")"
cardLocationId="$(./sevenkeys --profile="$profile" store --storagearea="$storageAreaName" --id="-1")"
check_error "sevenkeys"
if test "$profile" == "development"; then
databaseName="sevenkeys_development"
databaseName="sevenkeys_dev"
else
databaseName="sevenkeys"
databaseName="sevenkeys_prod"
fi
mysql --silent --silent --user=root --password="$(pass show sevenkeys/mysql)" \

View File

@ -2,3 +2,4 @@ cache/
config.dev.json
config.prod.json
sevenkeys.sql
log.txt

View File

@ -62,7 +62,7 @@ func GetAllCardtraderBlueprints(db *sql.DB) ([]cardtrader.Blueprint, error) {
var blueprint cardtrader.Blueprint
for rows.Next() {
err := rows.Scan(&blueprint.Id, &blueprint.CategoryId, &blueprint.ExpansionId, &blueprint.Name, &blueprint.FixedProperties.Number)
err := rows.Scan(&blueprint.Id, &blueprint.ScryfallId, &blueprint.CategoryId, &blueprint.ExpansionId, &blueprint.Name, &blueprint.FixedProperties.Number)
if err != nil {
return blueprints, err
}

View File

@ -2,7 +2,8 @@
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS CardScan (
Id INT PRIMARY KEY AUTO_INCREMENT,
CardLocationId INT NOT NULL,
ProductLocationId INT NOT NULL,
FOREIGN KEY (ProductLocationId) REFERENCES ProductLocation(Id),
Filename VARCHAR(100) NOT NULL
);
-- +goose StatementEnd

View File

@ -65,3 +65,27 @@ func InsertProductInExistingLocation(db *sql.DB, productLocation ProductLocation
return nil
}
func GetAllProductsInStorageArea(db *sql.DB, storageAreaId int) ([]ProductLocation, error) {
var productLocations []ProductLocation
query := `SELECT * FROM ProductLocation WHERE StorageAreaId = ?;`
rows, err := db.Query(query)
defer rows.Close()
if err != nil {
return productLocations, err
}
var productLocation ProductLocation
for rows.Next() {
err = rows.Scan(&productLocation.Id, &productLocation.CardtraderBlueprintId, &productLocation.StorageAreaId, &productLocation.Position, &productLocation.CardtraderProductId)
if err != nil {
return productLocations, err
}
productLocations = append(productLocations, productLocation)
}
return productLocations, nil
}

View File

@ -3,9 +3,29 @@ package logic
import (
"database/sql"
"errors"
"log"
"sevenkeys/database"
)
func CreateStorageArea(db *sql.DB, storageArea database.StorageArea) error {
// TODO: Check if there's already a storage are with the same name
// TODO: Check if the type entered is valid
err := database.InsertStorageArea(db, storageArea)
if err != nil {
return err
}
return nil
}
func GetAllStorageAreas(db *sql.DB) ([]database.StorageArea, error) {
storageAreas, err := database.GetAllStorageAreas(db)
if err != nil {
log.Fatal(err)
}
return storageAreas, err
}
var ErrCouldNotGetStorageAreaId error = errors.New("Could not get storage area ID.")
func GetStorageAreaId(db *sql.DB, storageAreaName string) (int, error) {
@ -31,17 +51,6 @@ func GetStorageAreaId(db *sql.DB, storageAreaName string) (int, error) {
return id, ErrCouldNotGetStorageAreaId
}
func CreateStorageArea(db *sql.DB, storageArea database.StorageArea) error {
// TODO: Check if there's already a storage are with the same name
// TODO: Check if the type entered is valid
err := database.InsertStorageArea(db, storageArea)
if err != nil {
return err
}
return nil
}
type StorageAreaSearchOptions map[string]int
func GetStorageAreaSearchOptions(db *sql.DB) (StorageAreaSearchOptions, error) {
@ -129,3 +138,8 @@ func Replace(db *sql.DB, productLocationId int, cardtraderBlueprintId int) error
return database.InsertProductInExistingLocation(db, productLocation)
}
func GetAllProductsInStorageArea(db *sql.DB, storageAreaId int) ([]database.ProductLocation, error) {
productLocations, err := database.GetAllProductsInStorageArea(db, storageAreaId)
return productLocations, err
}

View File

@ -11,6 +11,7 @@ import (
"sevenkeys/delverlens"
"sevenkeys/logic"
"sevenkeys/update"
"sevenkeys/webui"
)
const (
@ -23,13 +24,15 @@ const (
AddSubcommand string = "add"
RemoveSubcommand string = "remove"
ReplaceSubcommand string = "replace"
WebUiSubcommand string = "webui"
BlueprintsCommand string = "blueprints"
)
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
var profile string
flag.StringVar(&profile, "profile", "development", "The database profile to use.")
flag.StringVar(&profile, "profile", "dev", "The database profile to use.")
flag.Parse()
@ -201,6 +204,25 @@ func main() {
err := logic.Replace(db, *cardLocationId, *cardtraderBlueprintId)
logic.Check(err)
break
case WebUiSubcommand:
webui.Start(db)
/*
storageAreas, err := database.GetAllStorageAreas(db)
if err != nil {
log.Fatal(err)
}
fmt.Println(storageAreas)
*/
break
case BlueprintsCommand:
blueprints, err := database.GetAllCardtraderBlueprints(db)
if err != nil {
log.Fatal(err)
}
fmt.Println(blueprints)
break
default:
fmt.Fprintf(os.Stderr, "Unrecognized subcommand: %s\n", os.Args[1])
break

75
sevenkeys/webui/webui.go Normal file
View File

@ -0,0 +1,75 @@
package webui
import (
"database/sql"
"html/template"
"net/http"
"sevenkeys/logic"
"strconv"
)
type BaseHandler struct {
db *sql.DB
}
func NewBaseHandler(db *sql.DB) *BaseHandler {
return &BaseHandler{
db: db,
}
}
func (h *BaseHandler) storageAreasHandler(w http.ResponseWriter, r *http.Request) {
storageAreas, err := logic.GetAllStorageAreas(h.db)
if err != nil {
return
}
const tmpl = `
<html>
<head><title>Sevenkeys</title></head>
<body>
{{range $val := .}}
<a href="/storage/{{$val.Id}}">{{$val.Name}} ({{$val.StorageType}})</a><br>
{{end}}
</body>
</html>
`
t := template.Must(template.New("tmpl").Parse(tmpl))
t.Execute(w, storageAreas)
}
func (h *BaseHandler) storageHandler(w http.ResponseWriter, r *http.Request) {
storageAreaIdParam := r.PathValue("id")
storageAreaId, err := strconv.Atoi(storageAreaIdParam)
if err != nil {
return
}
productLocations, err := logic.GetAllProductsInStorageArea(h.db, storageAreaId)
if err != nil {
return
}
const tmpl = `
<html>
<head><title>Sevenkeys</title></head>
<body>
<ol>
{{range $val := .}}
<li>{{$val}}</li>
{{end}}
<ol>
</body>
</html>
`
t := template.Must(template.New("tmpl").Parse(tmpl))
t.Execute(w, productLocations)
}
func Start(db *sql.DB) {
handlers := NewBaseHandler(db)
http.HandleFunc("/", handlers.storageAreasHandler)
http.HandleFunc("/storage/{id}", handlers.storageHandler)
http.ListenAndServe(":8080", nil)
}