Compare commits

...

2 Commits

15 changed files with 286 additions and 135 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

@ -1,4 +1,5 @@
cache/
config.development.json
config.production.json
config.dev.json
config.prod.json
sevenkeys.sql
log.txt

View File

@ -1,66 +0,0 @@
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
}

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
}
@ -72,3 +72,15 @@ func GetAllCardtraderBlueprints(db *sql.DB) ([]cardtrader.Blueprint, error) {
return blueprints, nil
}
func GetCardtraderBlueprintIdByScryfallId(db *sql.DB, scryfallId string) (int, error) {
query := `SELECT Id FROM CardtraderBlueprint WHERE ScryfallCardId = ?;`
var cardtraderBlueprintId int
err := db.QueryRow(query, scryfallId).Scan(&cardtraderBlueprintId)
if err != nil {
return cardtraderBlueprintId, err
}
return cardtraderBlueprintId, nil
}

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

@ -0,0 +1,91 @@
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
}
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

@ -71,7 +71,7 @@ func GetStorageAreaTypeById(db *sql.DB, storageAreaId int) (string, error) {
}
func GetNextEmptySlotInBinder(db *sql.DB, storageAreaId int) (int, error) {
query := `SELECT Id FROM CardLocation WHERE CardPrintingId IS NULL AND StorageAreaId = ? ORDER BY Position ASC LIMIT 1;`
query := `SELECT Id FROM ProductLocation WHERE CardtraderBlueprintId IS NULL AND StorageAreaId = ? ORDER BY Position ASC LIMIT 1;`
var emptySlotId int
err := db.QueryRow(query, storageAreaId).Scan(&emptySlotId)

View File

@ -7,7 +7,7 @@ import (
)
type DelverLensCard struct {
ScryfallID string
ScryfallId string
IsFoil bool
}
@ -38,7 +38,7 @@ func ParseExportFile(filename string) ([]DelverLensCard, error) {
}
card := DelverLensCard{
ScryfallID: record[0],
ScryfallId: record[0],
IsFoil: record[1] == "Foil",
}
cards = append(cards, card)

View File

@ -2,25 +2,27 @@ package logic
import (
"database/sql"
"log"
"sevenkeys/database"
"sevenkeys/delverlens"
)
func ImportDelverLensCards(db *sql.DB, cards []delverlens.DelverLensCard, storageAreaId int) error {
for _, card := range cards {
var cardPrintingId string
if card.IsFoil {
cardPrintingId = card.ScryfallID + "f"
} else {
cardPrintingId = card.ScryfallID + "n"
cardtraderBlueprintId, err := database.GetCardtraderBlueprintIdByScryfallId(db, card.ScryfallId)
if err == sql.ErrNoRows {
log.Printf("import: No Cardtrader product found for Scryfall ID: %s, inserting placeholder\n", card.ScryfallId)
cardtraderBlueprintId = -1
} else if err != nil {
return err
}
cardLocation := database.CardLocation{
CardPrintingId: cardPrintingId,
StorageAreaId: storageAreaId,
productLocation := database.ProductLocation{
CardtraderBlueprintId: cardtraderBlueprintId,
StorageAreaId: storageAreaId,
}
StoreCard(db, cardLocation)
StoreProduct(db, productLocation)
}
return 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) {
@ -59,15 +68,15 @@ func GetStorageAreaSearchOptions(db *sql.DB) (StorageAreaSearchOptions, error) {
return options, nil
}
func storeAfterLastCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) {
lastPosition, err := database.GetLastPositionInStorageArea(db, cardLocation.StorageAreaId)
func storeAfterLastCard(db *sql.DB, productLocation database.ProductLocation) (int64, error) {
lastPosition, err := database.GetLastPositionInStorageArea(db, productLocation.StorageAreaId)
if err != nil {
return -1, err
}
cardLocation.Position = lastPosition + 1
productLocation.Position = lastPosition + 1
id, err := database.InsertCardLocation(db, cardLocation)
id, err := database.InsertProductLocation(db, productLocation)
if err != nil {
return -1, err
}
@ -75,9 +84,9 @@ func storeAfterLastCard(db *sql.DB, cardLocation database.CardLocation) (int64,
return id, nil
}
func storeInEmptySlot(db *sql.DB, cardLocation database.CardLocation, cardLocationId int) error {
cardLocation.Id = cardLocationId
err := database.InsertCardInExistingLocation(db, cardLocation)
func storeInEmptySlot(db *sql.DB, productLocation database.ProductLocation, productLocationId int) error {
productLocation.Id = productLocationId
err := database.InsertProductInExistingLocation(db, productLocation)
if err != nil {
return err
}
@ -85,17 +94,17 @@ func storeInEmptySlot(db *sql.DB, cardLocation database.CardLocation, cardLocati
return nil
}
func StoreCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) {
storageAreaType, err := database.GetStorageAreaTypeById(db, cardLocation.StorageAreaId)
func StoreProduct(db *sql.DB, productLocation database.ProductLocation) (int64, error) {
storageAreaType, err := database.GetStorageAreaTypeById(db, productLocation.StorageAreaId)
if err != nil {
return -1, err
}
if storageAreaType == database.StorageAreaTypeBinder {
nextEmptySlotId, err := database.GetNextEmptySlotInBinder(db, cardLocation.StorageAreaId)
nextEmptySlotId, err := database.GetNextEmptySlotInBinder(db, productLocation.StorageAreaId)
if err == database.ErrNoEmptySlotsInBinder {
id, err := storeAfterLastCard(db, cardLocation)
id, err := storeAfterLastCard(db, productLocation)
if err != nil {
return -1, err
}
@ -104,7 +113,7 @@ func StoreCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) {
} else if err != nil {
return -1, err
} else {
err = storeInEmptySlot(db, cardLocation, nextEmptySlotId)
err = storeInEmptySlot(db, productLocation, nextEmptySlotId)
if err != nil {
return -1, err
}
@ -113,7 +122,7 @@ func StoreCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) {
return int64(nextEmptySlotId), nil
}
id, err := storeAfterLastCard(db, cardLocation)
id, err := storeAfterLastCard(db, productLocation)
if err != nil {
return -1, err
}
@ -121,11 +130,16 @@ func StoreCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) {
return id, nil
}
func Replace(db *sql.DB, cardLocationId int, cardPrintingId string) error {
cardLocation := database.CardLocation{
Id: cardLocationId,
CardPrintingId: cardPrintingId,
func Replace(db *sql.DB, productLocationId int, cardtraderBlueprintId int) error {
productLocation := database.ProductLocation{
Id: productLocationId,
CardtraderBlueprintId: cardtraderBlueprintId,
}
return database.InsertCardInExistingLocation(db, cardLocation)
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,11 +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()
@ -60,9 +65,9 @@ func main() {
break
case StoreSubcommand:
storeCmd := flag.NewFlagSet(StoreSubcommand, flag.ExitOnError)
storageArea := storeCmd.String("storagearea", "",
id := storeCmd.Int("id", -1, "The CardPrintingId of the card to store.")
storageArea := storeCmd.String("storage-area", "",
"The name of the StorageArea the card should be inserted to.")
id := storeCmd.String("id", "", "The CardPrintingId of the card to store.")
storeCmd.Parse(flag.Args()[1:])
@ -72,14 +77,14 @@ func main() {
os.Exit(1)
}
cardLocation := database.CardLocation{
CardPrintingId: *id,
StorageAreaId: storageAreaId,
productLocation := database.ProductLocation{
CardtraderBlueprintId: *id,
StorageAreaId: storageAreaId,
}
cardLocationId, err := logic.StoreCard(db, cardLocation)
productLocationId, err := logic.StoreProduct(db, productLocation)
logic.Check(err)
fmt.Printf("%d\n", cardLocationId)
fmt.Printf("%d\n", productLocationId)
break
case ImportSubcommand:
@ -147,8 +152,8 @@ func main() {
case AddSubcommand:
addCmd := flag.NewFlagSet(AddSubcommand, flag.ExitOnError)
cardPrintingId := addCmd.String("card-printing-id", "", "The ID of the card printing to add to storage.")
storageArea := addCmd.String("storagearea", "",
cardtraderBlueprintId := addCmd.Int("blueprint-id", -1, "The Cardtrader Bluepritn ID of the product to add to storage.")
storageArea := addCmd.String("storage-area", "",
"The name of the StorageArea where cards should be imported.")
addCmd.Parse(flag.Args()[1:])
@ -160,11 +165,11 @@ func main() {
}
logic.Check(err)
cardLocation := database.CardLocation{
CardPrintingId: *cardPrintingId,
StorageAreaId: storageAreaId,
productLocation := database.ProductLocation{
CardtraderBlueprintId: *cardtraderBlueprintId,
StorageAreaId: storageAreaId,
}
logic.StoreCard(db, cardLocation)
logic.StoreProduct(db, productLocation)
break
case RemoveSubcommand:
removeCmd := flag.NewFlagSet(RemoveSubcommand, flag.ExitOnError)
@ -188,19 +193,35 @@ func main() {
replaceCmd := flag.NewFlagSet(ReplaceSubcommand, flag.ExitOnError)
cardLocationId := replaceCmd.Int("card-location-id", -1, "The card location to replace the card at.")
cardPrintingId := replaceCmd.String("card-printing-id", "", "The card printing to put at the specified location.")
cardtraderBlueprintId := replaceCmd.Int("blueprint-id", -1, "The card printing to put at the specified location.")
replaceCmd.Parse(flag.Args()[1:])
if *cardLocationId == -1 {
log.Fatal(errors.New("No CardLocationId given."))
}
if *cardPrintingId == "" {
log.Fatal(errors.New("No CardPrintingId given."))
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)
}
err := logic.Replace(db, *cardLocationId, *cardPrintingId)
logic.Check(err)
fmt.Println(blueprints)
break
default:
fmt.Fprintf(os.Stderr, "Unrecognized subcommand: %s\n", os.Args[1])

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