Update storage schema, add basic locate feature

This commit is contained in:
The Magician 2024-09-05 10:53:14 +01:00
parent fec8e6cf2d
commit c2ca6d284d
7 changed files with 102 additions and 40 deletions

View File

@ -21,6 +21,7 @@ func showOutput() {
func MainCliLoop(db *sql.DB) { func MainCliLoop(db *sql.DB) {
var command string var command string
var selectedStorageArea database.StorageArea var selectedStorageArea database.StorageArea
var cardLocation database.CardLocation
for { for {
ShowSplashScreen() ShowSplashScreen()
@ -47,12 +48,7 @@ func MainCliLoop(db *sql.DB) {
area, err := logic.SelectStorageArea(db) area, err := logic.SelectStorageArea(db)
logic.Check(err) logic.Check(err)
selectedStorageArea = area selectedStorageArea = area
break cardLocation.StorageAreaId = area.Id
case "r", "source":
cardStorageLocation.Source = GetStringResponse("Card source:")
break
case "p", "condition":
cardStorageLocation.CardCondition = GetStringResponse("Card condition:")
break break
case "c", "criteria": case "c", "criteria":
getSearchCriteria() getSearchCriteria()
@ -60,9 +56,9 @@ func MainCliLoop(db *sql.DB) {
case "s", "search": case "s", "search":
getSearchOptions(db) getSearchOptions(db)
var previousCardPrintingId = cardStorageLocation.CardPrintingId var previousCardPrintingId = cardLocation.CardPrintingId
cardStorageLocation.CardPrintingId, selectedCardPrintingSearchLine, err = logic.Search(searchOptions) cardLocation.CardPrintingId, selectedCardPrintingSearchLine, err = logic.Search(searchOptions)
var exitError *exec.ExitError var exitError *exec.ExitError
if errors.As(err, &exitError) { if errors.As(err, &exitError) {
break break
@ -70,12 +66,33 @@ func MainCliLoop(db *sql.DB) {
logic.Check(err) logic.Check(err)
output = "" output = ""
if cardStorageLocation.CardPrintingId != previousCardPrintingId { if cardLocation.CardPrintingId != previousCardPrintingId {
copiesInserted = 0 copiesInserted = 0
} }
break break
case "i", "insert": case "i", "insert":
insertSelectedCard(db) insertSelectedCard(db, selectedStorageArea, cardLocation)
break
case "l", "locate":
cardName := GetStringResponse("Card name:")
locations, err := logic.LocateCards(db, cardName)
logic.Check(err)
for _, location := range locations {
fmt.Printf("%s (%s %s) [%s]", location.CardName, location.SetCode, location.CollectorNumber, location.Language)
if location.IsFoil {
fmt.Printf(" FOIL")
}
if location.IsPromo {
fmt.Printf(" PROMO")
}
fmt.Printf(" in %s \"%s\" at position %d\n", location.StorageAreaType, location.StorageAreaName, location.Position)
}
fmt.Scanln()
break break
default: default:
fmt.Println("Unrecognized command:", command) fmt.Println("Unrecognized command:", command)

View File

@ -9,7 +9,6 @@ import (
) )
var ( var (
cardStorageLocation database.CardStorageLocation
selectedCardPrintingSearchLine string selectedCardPrintingSearchLine string
copiesInserted int copiesInserted int
@ -25,7 +24,7 @@ func getInfoDisplay(info string) string {
func showStorageInfo(w io.Writer, area database.StorageArea) { func showStorageInfo(w io.Writer, area database.StorageArea) {
fmt.Fprint(w, "Selected Storage Area: ") fmt.Fprint(w, "Selected Storage Area: ")
if area.Name == "" { if area.Name == "" { // TODO: Add a struct method to determine whether it is unset?
fmt.Fprint(w, "[None]\n") fmt.Fprint(w, "[None]\n")
return return
} }
@ -38,18 +37,18 @@ func showCopiesInserted() {
fmt.Println("Copies inserted:", copiesInserted) fmt.Println("Copies inserted:", copiesInserted)
} }
func insertSelectedCard(db *sql.DB) { func insertSelectedCard(db *sql.DB, area database.StorageArea, location database.CardLocation) {
if cardStorageLocation.CardPrintingId == "" { if area.Name == "" {
output = "No storage area selected."
return
}
if location.CardPrintingId == "" {
output = "No card selected, please [search] for a card printing." output = "No card selected, please [search] for a card printing."
return return
} }
if cardStorageLocation.StorageBox == "" || cardStorageLocation.CardCondition == "" { err := logic.StoreCard(db, location)
cardStorageLocation.StorageBox = GetStringResponse("Storage location:")
cardStorageLocation.CardCondition = GetStringResponse("Card condition:")
}
err := logic.StoreCard(db, cardStorageLocation)
logic.Check(err) logic.Check(err)
copiesInserted++ copiesInserted++

View File

@ -4,20 +4,18 @@ import (
"database/sql" "database/sql"
) )
type CardStorageLocation struct { type CardLocation struct {
Id int Id int
CardPrintingId string CardPrintingId string
CardCondition string StorageAreaId int
StorageBox string
Position int Position int
Source string
} }
func GetLastPositionInBox(db *sql.DB, storageBox string) (int, error) { func GetLastPositionInStorageArea(db *sql.DB, storageAreaId int) (int, error) {
query := "SELECT Position FROM CardStorageLocation WHERE StorageBox = ? ORDER BY Position DESC LIMIT 1;" query := "SELECT Position FROM CardLocation WHERE StorageAreaId = ? ORDER BY Position DESC LIMIT 1;"
var lastPosition int var lastPosition int
err := db.QueryRow(query, storageBox).Scan(&lastPosition) err := db.QueryRow(query, storageAreaId).Scan(&lastPosition)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return 0, nil return 0, nil
@ -28,17 +26,17 @@ func GetLastPositionInBox(db *sql.DB, storageBox string) (int, error) {
return lastPosition, nil return lastPosition, nil
} }
func InsertCardStorageLocation(db *sql.DB, storageLocation CardStorageLocation) error { func InsertCardLocation(db *sql.DB, storageLocation CardLocation) error {
query := `INSERT INTO CardStorageLocation query := `INSERT INTO CardLocation
(CardPrintingId, CardCondition, StorageBox, Position, Source) (CardPrintingId, StorageAreaId, Position)
VALUES (?, ?, ?, ?, ?);` VALUES (?, ?, ?);`
insert, err := db.Prepare(query) insert, err := db.Prepare(query)
if err != nil { if err != nil {
return err return err
} }
_, err = insert.Exec(storageLocation.CardPrintingId, storageLocation.CardCondition, storageLocation.StorageBox, storageLocation.Position, storageLocation.Source) _, err = insert.Exec(storageLocation.CardPrintingId, storageLocation.StorageAreaId, storageLocation.Position)
if err != nil { if err != nil {
return err return err
} }

View File

@ -0,0 +1,38 @@
package database
import "database/sql"
type LocateCardResult struct {
CardName string
SetCode string
IsFoil bool
IsPromo bool
CollectorNumber string
Language string
StorageAreaType string
StorageAreaName string
Position int
}
func GetLocateCardResultsByCardName(db *sql.DB, cardName string) ([]LocateCardResult, error) {
var results []LocateCardResult
query := "SELECT CardPrinting.Name, CardPrinting.SetCode, CardPrinting.IsFoil, CardPrinting.IsPromo, CardPrinting.CollectorNumber, CardPrinting.Language, StorageArea.StorageType, StorageArea.Name, CardLocation.Position FROM CardLocation JOIN CardPrinting ON CardLocation.CardPrintingId = CardPrinting.Id JOIN StorageArea ON CardLocation.StorageAreaId = StorageArea.Id WHERE CardPrinting.Name = ?;"
rows, err := db.Query(query, cardName)
defer rows.Close()
if err != nil {
return results, err
}
var result LocateCardResult
for rows.Next() {
err := rows.Scan(&result.CardName, &result.SetCode, &result.IsFoil, &result.IsPromo, &result.CollectorNumber, &result.Language, &result.StorageAreaType, &result.StorageAreaName, &result.Position)
if err != nil {
return results, err
}
results = append(results, result)
}
return results, nil
}

View File

@ -34,10 +34,9 @@ CREATE TABLE IF NOT EXISTS StorageArea (
CREATE TABLE IF NOT EXISTS CardLocation ( CREATE TABLE IF NOT EXISTS CardLocation (
Id INT AUTO_INCREMENT PRIMARY KEY, Id INT AUTO_INCREMENT PRIMARY KEY,
CardPrintingId VARCHAR(37) NOT NULL, CardPrintingId VARCHAR(37) NULL,
FOREIGN KEY (CardPrintingId) REFERENCES CardPrinting(Id), FOREIGN KEY (CardPrintingId) REFERENCES CardPrinting(Id),
CardCondition ENUM('Mint', 'Near Mint', 'Excellent', 'Good', 'Light Played', 'Played', 'Poor') NOT NULL,
StorageAreaId INT NOT NULL, StorageAreaId INT NOT NULL,
FOREIGN KEY (StorageAreaId) REFERENCES StorageArea(Id), FOREIGN KEY (StorageAreaId) REFERENCES StorageArea(Id),
Location INT NULL Position INT NULL
); );

11
sevenkeys/logic/locate.go Normal file
View File

@ -0,0 +1,11 @@
package logic
import (
"database/sql"
"sevenkeys/database"
)
func LocateCards(db *sql.DB, cardName string) ([]database.LocateCardResult, error) {
results, err := database.GetLocateCardResultsByCardName(db, cardName)
return results, err
}

View File

@ -39,7 +39,7 @@ func SelectStorageArea(db *sql.DB) (database.StorageArea, error) {
go func() { go func() {
defer fzfStdin.Close() defer fzfStdin.Close()
for _, area := range storageAreas { for _, area := range storageAreas {
io.WriteString(fzfStdin, area.Name) io.WriteString(fzfStdin, area.Name+"\n")
} }
}() }()
@ -59,15 +59,15 @@ func SelectStorageArea(db *sql.DB) (database.StorageArea, error) {
return selectedStorageArea, nil return selectedStorageArea, nil
} }
func StoreCard(db *sql.DB, storageLocation database.CardStorageLocation) error { func StoreCard(db *sql.DB, cardLocation database.CardLocation) error {
lastPosition, err := database.GetLastPositionInBox(db, storageLocation.StorageBox) lastPosition, err := database.GetLastPositionInStorageArea(db, cardLocation.StorageAreaId)
if err != nil { if err != nil {
return err return err
} }
storageLocation.Position = lastPosition + 1 cardLocation.Position = lastPosition + 1
err = database.InsertCardStorageLocation(db, storageLocation) err = database.InsertCardLocation(db, cardLocation)
if err != nil { if err != nil {
return err return err
} }