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) {
var command string
var selectedStorageArea database.StorageArea
var cardLocation database.CardLocation
for {
ShowSplashScreen()
@ -47,12 +48,7 @@ func MainCliLoop(db *sql.DB) {
area, err := logic.SelectStorageArea(db)
logic.Check(err)
selectedStorageArea = area
break
case "r", "source":
cardStorageLocation.Source = GetStringResponse("Card source:")
break
case "p", "condition":
cardStorageLocation.CardCondition = GetStringResponse("Card condition:")
cardLocation.StorageAreaId = area.Id
break
case "c", "criteria":
getSearchCriteria()
@ -60,9 +56,9 @@ func MainCliLoop(db *sql.DB) {
case "s", "search":
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
if errors.As(err, &exitError) {
break
@ -70,12 +66,33 @@ func MainCliLoop(db *sql.DB) {
logic.Check(err)
output = ""
if cardStorageLocation.CardPrintingId != previousCardPrintingId {
if cardLocation.CardPrintingId != previousCardPrintingId {
copiesInserted = 0
}
break
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
default:
fmt.Println("Unrecognized command:", command)

View File

@ -9,7 +9,6 @@ import (
)
var (
cardStorageLocation database.CardStorageLocation
selectedCardPrintingSearchLine string
copiesInserted int
@ -25,7 +24,7 @@ func getInfoDisplay(info string) string {
func showStorageInfo(w io.Writer, area database.StorageArea) {
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")
return
}
@ -38,18 +37,18 @@ func showCopiesInserted() {
fmt.Println("Copies inserted:", copiesInserted)
}
func insertSelectedCard(db *sql.DB) {
if cardStorageLocation.CardPrintingId == "" {
func insertSelectedCard(db *sql.DB, area database.StorageArea, location database.CardLocation) {
if area.Name == "" {
output = "No storage area selected."
return
}
if location.CardPrintingId == "" {
output = "No card selected, please [search] for a card printing."
return
}
if cardStorageLocation.StorageBox == "" || cardStorageLocation.CardCondition == "" {
cardStorageLocation.StorageBox = GetStringResponse("Storage location:")
cardStorageLocation.CardCondition = GetStringResponse("Card condition:")
}
err := logic.StoreCard(db, cardStorageLocation)
err := logic.StoreCard(db, location)
logic.Check(err)
copiesInserted++

View File

@ -4,20 +4,18 @@ import (
"database/sql"
)
type CardStorageLocation struct {
type CardLocation struct {
Id int
CardPrintingId string
CardCondition string
StorageBox string
StorageAreaId int
Position int
Source string
}
func GetLastPositionInBox(db *sql.DB, storageBox string) (int, error) {
query := "SELECT Position FROM CardStorageLocation WHERE StorageBox = ? ORDER BY Position DESC LIMIT 1;"
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, storageBox).Scan(&lastPosition)
err := db.QueryRow(query, storageAreaId).Scan(&lastPosition)
if err == sql.ErrNoRows {
return 0, nil
@ -28,17 +26,17 @@ func GetLastPositionInBox(db *sql.DB, storageBox string) (int, error) {
return lastPosition, nil
}
func InsertCardStorageLocation(db *sql.DB, storageLocation CardStorageLocation) error {
query := `INSERT INTO CardStorageLocation
(CardPrintingId, CardCondition, StorageBox, Position, Source)
VALUES (?, ?, ?, ?, ?);`
func InsertCardLocation(db *sql.DB, storageLocation CardLocation) error {
query := `INSERT INTO CardLocation
(CardPrintingId, StorageAreaId, Position)
VALUES (?, ?, ?);`
insert, err := db.Prepare(query)
if err != nil {
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 {
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 (
Id INT AUTO_INCREMENT PRIMARY KEY,
CardPrintingId VARCHAR(37) NOT NULL,
CardPrintingId VARCHAR(37) NULL,
FOREIGN KEY (CardPrintingId) REFERENCES CardPrinting(Id),
CardCondition ENUM('Mint', 'Near Mint', 'Excellent', 'Good', 'Light Played', 'Played', 'Poor') NOT NULL,
StorageAreaId INT NOT NULL,
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() {
defer fzfStdin.Close()
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
}
func StoreCard(db *sql.DB, storageLocation database.CardStorageLocation) error {
lastPosition, err := database.GetLastPositionInBox(db, storageLocation.StorageBox)
func StoreCard(db *sql.DB, cardLocation database.CardLocation) error {
lastPosition, err := database.GetLastPositionInStorageArea(db, cardLocation.StorageAreaId)
if err != nil {
return err
}
storageLocation.Position = lastPosition + 1
cardLocation.Position = lastPosition + 1
err = database.InsertCardStorageLocation(db, storageLocation)
err = database.InsertCardLocation(db, cardLocation)
if err != nil {
return err
}