Wire up removing cards to UI

This commit is contained in:
The Magician 2024-09-13 12:32:18 +01:00
parent 1e967674b6
commit e860e529f1
5 changed files with 84 additions and 44 deletions

View File

@ -26,7 +26,7 @@ func MainCliLoop(db *sql.DB) {
} }
var insertSearchOptions logic.InsertSearchOptions var insertSearchOptions logic.InsertSearchOptions
// TODO: Make these do something and add the ability to modify them // TODO: Add the ability to modify this
var locateSearchCriteria logic.SearchCriteria = logic.SearchCriteria{ var locateSearchCriteria logic.SearchCriteria = logic.SearchCriteria{
SetCode: "", SetCode: "",
Foil: logic.Either, Foil: logic.Either,
@ -96,11 +96,30 @@ func MainCliLoop(db *sql.DB) {
locations, err := logic.LocateCards(db, cardNames, locateSearchCriteria) locations, err := logic.LocateCards(db, cardNames, locateSearchCriteria)
logic.Check(err) logic.Check(err)
for _, location := range locations { if len(locations) == 0 {
fmt.Println(location) fmt.Println("No results found")
fmt.Scanln()
break
} }
fmt.Scanln()
for _, location := range locations {
description := logic.GetLocationDescription(location)
fmt.Println(description)
for true {
todo := GetStringResponse("TODO:")
if todo == "r" {
logic.RemoveFromStorage(db, location)
} else if todo == "n" {
continue
} else {
fmt.Printf("Unrecognized option: %s\n", todo)
}
}
}
fmt.Scanln()
break break
default: default:
fmt.Println("Unrecognized command:", command) fmt.Println("Unrecognized command:", command)

View File

@ -7,12 +7,14 @@ import (
) )
type LocateCardResult struct { type LocateCardResult struct {
CardLocationId int
CardName string CardName string
SetCode string SetCode string
IsFoil bool IsFoil bool
IsPromo bool IsPromo bool
CollectorNumber string CollectorNumber string
Language string Language string
StorageAreaId int
StorageAreaType string StorageAreaType string
StorageAreaName string StorageAreaName string
Position int Position int
@ -21,7 +23,21 @@ type LocateCardResult struct {
func GetLocateResults(db *sql.DB, cardNames []string) ([]LocateCardResult, error) { func GetLocateResults(db *sql.DB, cardNames []string) ([]LocateCardResult, error) {
var results []LocateCardResult 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 IN (?);" query := `SELECT CardLocation.Id,
CardPrinting.Name,
CardPrinting.SetCode,
CardPrinting.IsFoil,
CardPrinting.IsPromo,
CardPrinting.CollectorNumber,
CardPrinting.Language,
StorageArea.Id,
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 IN (?);`
query, args, err := sqlx.In(query, cardNames) query, args, err := sqlx.In(query, cardNames)
if err != nil { if err != nil {
return results, err return results, err
@ -35,7 +51,7 @@ func GetLocateResults(db *sql.DB, cardNames []string) ([]LocateCardResult, error
var result LocateCardResult var result LocateCardResult
for rows.Next() { for rows.Next() {
err := rows.Scan(&result.CardName, &result.SetCode, &result.IsFoil, &result.IsPromo, &result.CollectorNumber, &result.Language, &result.StorageAreaType, &result.StorageAreaName, &result.Position) err := rows.Scan(&result.CardLocationId, &result.CardName, &result.SetCode, &result.IsFoil, &result.IsPromo, &result.CollectorNumber, &result.Language, &result.StorageAreaId, &result.StorageAreaType, &result.StorageAreaName, &result.Position)
if err != nil { if err != nil {
return results, err return results, err
} }

View File

@ -2,7 +2,7 @@ package database
import "database/sql" import "database/sql"
func RemoveFromBinder(db *sql.DB, location CardLocation) error { func RemoveFromBinder(db *sql.DB, location LocateCardResult) error {
query := `UPDATE CardStorageLocation SET CardPrintingId = NULL WHERE Id = ?;` query := `UPDATE CardStorageLocation SET CardPrintingId = NULL WHERE Id = ?;`
update, err := db.Prepare(query) update, err := db.Prepare(query)
@ -11,7 +11,7 @@ func RemoveFromBinder(db *sql.DB, location CardLocation) error {
return err return err
} }
_, err = update.Exec(location.Id) _, err = update.Exec(location.CardLocationId)
if err != nil { if err != nil {
return err return err
} }
@ -19,7 +19,7 @@ func RemoveFromBinder(db *sql.DB, location CardLocation) error {
return nil return nil
} }
func RemoveFromBox(db *sql.DB, location CardLocation) error { func RemoveFromBox(db *sql.DB, location LocateCardResult) error {
deleteQuery := `DELETE FROM CardStorageLocation WHERE Id = ?;` deleteQuery := `DELETE FROM CardStorageLocation WHERE Id = ?;`
del, err := db.Prepare(deleteQuery) del, err := db.Prepare(deleteQuery)
@ -28,12 +28,12 @@ func RemoveFromBox(db *sql.DB, location CardLocation) error {
return err return err
} }
_, err = del.Exec(location.Id) _, err = del.Exec(location.CardLocationId)
if err != nil { if err != nil {
return err return err
} }
updateQuery := `UPDATE CardStorageLocation SET Position = Position - 1 WHERE Position > 5;` updateQuery := `UPDATE CardStorageLocation SET Position = Position - 1 WHERE Position > ? AND StorageAreaId = ?;`
update, err := db.Prepare(updateQuery) update, err := db.Prepare(updateQuery)
defer update.Close() defer update.Close()
@ -41,7 +41,7 @@ func RemoveFromBox(db *sql.DB, location CardLocation) error {
return err return err
} }
_, err = update.Exec(location.Id) _, err = update.Exec(location.Position, location.StorageAreaId)
if err != nil { if err != nil {
return err return err
} }

View File

@ -27,15 +27,14 @@ func GetBinderLocationDescription(position int) string {
return fmt.Sprintf(" on page %d in %s slot %d", page, frontOrBack, slot) return fmt.Sprintf(" on page %d in %s slot %d", page, frontOrBack, slot)
} }
func LocateCards(db *sql.DB, cardNames []string, criteria SearchCriteria) ([]string, error) { func LocateCards(db *sql.DB, cardNames []string, criteria SearchCriteria) ([]database.LocateCardResult, error) {
var locations []string
results, err := database.GetLocateResults(db, cardNames) results, err := database.GetLocateResults(db, cardNames)
if err != nil { if err != nil {
return locations, err return results, err
} }
var location string var filteredResults []database.LocateCardResult
for _, result := range results { for _, result := range results {
printing := database.CardPrinting{ printing := database.CardPrinting{
SetCode: result.SetCode, SetCode: result.SetCode,
@ -49,31 +48,37 @@ func LocateCards(db *sql.DB, cardNames []string, criteria SearchCriteria) ([]str
continue continue
} }
location = fmt.Sprintf("%s (%s %s) [%s]", filteredResults = append(filteredResults, result)
result.CardName,
result.SetCode,
result.CollectorNumber,
result.Language)
if result.IsFoil {
location += " FOIL"
}
if result.IsPromo {
location += " PROMO"
}
location += fmt.Sprintf(" in %s \"%s\"",
result.StorageAreaType,
result.StorageAreaName)
if result.StorageAreaType == "Binder" {
location += GetBinderLocationDescription(result.Position)
} else if result.StorageAreaType == "Box" {
location += fmt.Sprintf(" at position %d", result.Position)
}
locations = append(locations, location)
} }
return locations, nil return filteredResults, nil
}
func GetLocationDescription(location database.LocateCardResult) string {
var description string
description = fmt.Sprintf("%s (%s %s) [%s]",
location.CardName,
location.SetCode,
location.CollectorNumber,
location.Language)
if location.IsFoil {
description += " FOIL"
}
if location.IsPromo {
description += " PROMO"
}
description += fmt.Sprintf(" in %s \"%s\"",
location.StorageAreaType,
location.StorageAreaName)
if location.StorageAreaType == "Binder" {
description += GetBinderLocationDescription(location.Position)
} else if location.StorageAreaType == "Box" {
description += fmt.Sprintf(" at position %d", location.Position)
}
return description
} }

View File

@ -9,8 +9,8 @@ import (
var UnrecognizedStorageAreaTypeError error = errors.New("Unrecognized storage area type.") var UnrecognizedStorageAreaTypeError error = errors.New("Unrecognized storage area type.")
func RemoveFromStorage(db *sql.DB, location database.CardLocation) error { func RemoveFromStorage(db *sql.DB, location database.LocateCardResult) error {
locationType, err := database.GetStorageAreaTypeById(db, location.Id) locationType, err := database.GetStorageAreaTypeById(db, location.CardLocationId)
if err != nil { if err != nil {
return err return err
} }