From e860e529f17ecff026463656a3c864e4e80b256d Mon Sep 17 00:00:00 2001 From: The Magician Date: Fri, 13 Sep 2024 12:32:18 +0100 Subject: [PATCH] Wire up removing cards to UI --- sevenkeys/cli/mainui.go | 27 ++++++++++++--- sevenkeys/database/locate.go | 20 +++++++++-- sevenkeys/database/remove.go | 12 +++---- sevenkeys/logic/locate.go | 65 +++++++++++++++++++----------------- sevenkeys/logic/remove.go | 4 +-- 5 files changed, 84 insertions(+), 44 deletions(-) diff --git a/sevenkeys/cli/mainui.go b/sevenkeys/cli/mainui.go index bd2f60a..94a0a9f 100644 --- a/sevenkeys/cli/mainui.go +++ b/sevenkeys/cli/mainui.go @@ -26,7 +26,7 @@ func MainCliLoop(db *sql.DB) { } 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{ SetCode: "", Foil: logic.Either, @@ -96,11 +96,30 @@ func MainCliLoop(db *sql.DB) { locations, err := logic.LocateCards(db, cardNames, locateSearchCriteria) logic.Check(err) - for _, location := range locations { - fmt.Println(location) + if len(locations) == 0 { + 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 default: fmt.Println("Unrecognized command:", command) diff --git a/sevenkeys/database/locate.go b/sevenkeys/database/locate.go index cb1f217..82c685b 100644 --- a/sevenkeys/database/locate.go +++ b/sevenkeys/database/locate.go @@ -7,12 +7,14 @@ import ( ) type LocateCardResult struct { + CardLocationId int CardName string SetCode string IsFoil bool IsPromo bool CollectorNumber string Language string + StorageAreaId int StorageAreaType string StorageAreaName string Position int @@ -21,7 +23,21 @@ type LocateCardResult struct { func GetLocateResults(db *sql.DB, cardNames []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 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) if err != nil { return results, err @@ -35,7 +51,7 @@ func GetLocateResults(db *sql.DB, cardNames []string) ([]LocateCardResult, error 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) + 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 { return results, err } diff --git a/sevenkeys/database/remove.go b/sevenkeys/database/remove.go index 38c558b..5af37cd 100644 --- a/sevenkeys/database/remove.go +++ b/sevenkeys/database/remove.go @@ -2,7 +2,7 @@ package database 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 = ?;` update, err := db.Prepare(query) @@ -11,7 +11,7 @@ func RemoveFromBinder(db *sql.DB, location CardLocation) error { return err } - _, err = update.Exec(location.Id) + _, err = update.Exec(location.CardLocationId) if err != nil { return err } @@ -19,7 +19,7 @@ func RemoveFromBinder(db *sql.DB, location CardLocation) error { return nil } -func RemoveFromBox(db *sql.DB, location CardLocation) error { +func RemoveFromBox(db *sql.DB, location LocateCardResult) error { deleteQuery := `DELETE FROM CardStorageLocation WHERE Id = ?;` del, err := db.Prepare(deleteQuery) @@ -28,12 +28,12 @@ func RemoveFromBox(db *sql.DB, location CardLocation) error { return err } - _, err = del.Exec(location.Id) + _, err = del.Exec(location.CardLocationId) if err != nil { 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) defer update.Close() @@ -41,7 +41,7 @@ func RemoveFromBox(db *sql.DB, location CardLocation) error { return err } - _, err = update.Exec(location.Id) + _, err = update.Exec(location.Position, location.StorageAreaId) if err != nil { return err } diff --git a/sevenkeys/logic/locate.go b/sevenkeys/logic/locate.go index c71880e..ce6008b 100644 --- a/sevenkeys/logic/locate.go +++ b/sevenkeys/logic/locate.go @@ -27,15 +27,14 @@ func GetBinderLocationDescription(position int) string { return fmt.Sprintf(" on page %d in %s slot %d", page, frontOrBack, slot) } -func LocateCards(db *sql.DB, cardNames []string, criteria SearchCriteria) ([]string, error) { - var locations []string - +func LocateCards(db *sql.DB, cardNames []string, criteria SearchCriteria) ([]database.LocateCardResult, error) { results, err := database.GetLocateResults(db, cardNames) if err != nil { - return locations, err + return results, err } - var location string + var filteredResults []database.LocateCardResult + for _, result := range results { printing := database.CardPrinting{ SetCode: result.SetCode, @@ -49,31 +48,37 @@ func LocateCards(db *sql.DB, cardNames []string, criteria SearchCriteria) ([]str continue } - location = fmt.Sprintf("%s (%s %s) [%s]", - 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) + filteredResults = append(filteredResults, result) } - 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 } diff --git a/sevenkeys/logic/remove.go b/sevenkeys/logic/remove.go index 4919c2b..be617a6 100644 --- a/sevenkeys/logic/remove.go +++ b/sevenkeys/logic/remove.go @@ -9,8 +9,8 @@ import ( var UnrecognizedStorageAreaTypeError error = errors.New("Unrecognized storage area type.") -func RemoveFromStorage(db *sql.DB, location database.CardLocation) error { - locationType, err := database.GetStorageAreaTypeById(db, location.Id) +func RemoveFromStorage(db *sql.DB, location database.LocateCardResult) error { + locationType, err := database.GetStorageAreaTypeById(db, location.CardLocationId) if err != nil { return err }