Compare commits

..

No commits in common. "70f0b4d4f7b5fe428d0ac75821b48b4199b6a17d" and "1e967674b68e34fa06cd1b2fe924f4db9076c152" have entirely different histories.

13 changed files with 145 additions and 325 deletions

View File

@ -4,9 +4,9 @@ import (
"database/sql"
"errors"
"fmt"
"os"
"os/exec"
"sevenkeys/database"
"sevenkeys/delverlens"
"sevenkeys/logic"
)
@ -15,7 +15,7 @@ var output string
func MainCliLoop(db *sql.DB) {
var command string
//var selectedStorageAreaName string
var selectedStorageArea database.StorageArea
var cardLocation database.CardLocation
var insertSearchCriteria logic.SearchCriteria = logic.SearchCriteria{
@ -26,18 +26,17 @@ func MainCliLoop(db *sql.DB) {
}
var insertSearchOptions logic.InsertSearchOptions
// TODO: Add the ability to modify this
// TODO: Make these do something and add the ability to modify them
var locateSearchCriteria logic.SearchCriteria = logic.SearchCriteria{
SetCode: "",
Foil: logic.True,
Foil: logic.Either,
Promo: logic.Either,
Language: "en",
}
for {
ShowSplashScreen()
// TODO: Fix this
//showStorageInfo(os.Stdout, selectedStorageAreaName)
showStorageInfo(os.Stdout, selectedStorageArea)
showInsertSearchCriteria(insertSearchCriteria)
showSelectedCard()
showCopiesInserted()
@ -57,14 +56,10 @@ func MainCliLoop(db *sql.DB) {
logic.Check(err)
break
case "a", "area":
options, err := logic.GetStorageAreaSearchOptions(db)
area, err := logic.SelectStorageArea(db)
logic.Check(err)
id, _, err := logic.GenericSearch(options)
logic.Check(err)
//selectedStorageAreaName = name
cardLocation.StorageAreaId = id
selectedStorageArea = area
cardLocation.StorageAreaId = area.Id
break
case "c", "criteria":
insertSearchCriteria = getSearchCriteria()
@ -76,9 +71,8 @@ func MainCliLoop(db *sql.DB) {
var previousCardPrintingId = cardLocation.CardPrintingId
pk, searchLine, err := logic.GenericSearch(insertSearchOptions)
pk, err := logic.GenericSearch(insertSearchOptions)
cardLocation.CardPrintingId = pk
selectedCardPrintingSearchLine = searchLine
var exitError *exec.ExitError
if errors.As(err, &exitError) {
@ -92,18 +86,7 @@ func MainCliLoop(db *sql.DB) {
}
break
case "i", "insert":
err := logic.StoreCard(db, cardLocation)
logic.Check(err)
copiesInserted++
break
case "d", "delverlens":
filename := GetStringResponse("Filename:")
cards, err := delverlens.ParseExportFile(filename)
logic.Check(err)
err = logic.ImportDelverLensCards(cards)
logic.Check(err)
insertSelectedCard(db, selectedStorageArea, cardLocation)
break
case "l", "locate":
filename := GetStringResponse("Filename:")
@ -113,32 +96,11 @@ func MainCliLoop(db *sql.DB) {
locations, err := logic.LocateCards(db, cardNames, locateSearchCriteria)
logic.Check(err)
if len(locations) == 0 {
fmt.Println("No results found")
fmt.Scanln()
break
}
for _, location := range locations {
description := logic.GetLocationDescription(location)
fmt.Println(description)
for true {
todo := GetStringResponse("TODO:")
if todo == "r" {
logic.RemoveFromStorage(db, location)
break
} else if todo == "n" {
break
} else {
fmt.Printf("Unrecognized option: %s\n", todo)
fmt.Println(location)
}
}
}
fmt.Println("Though this query has ended, its relics still slumber in New Argive.")
fmt.Scanln()
break
default:
fmt.Println("Unrecognized command:", command)

View File

@ -1,9 +1,11 @@
package cli
import (
"database/sql"
"fmt"
"io"
"sevenkeys/database"
"sevenkeys/logic"
)
var (
@ -34,3 +36,20 @@ func showStorageInfo(w io.Writer, area database.StorageArea) {
func showCopiesInserted() {
fmt.Println("Copies inserted:", copiesInserted)
}
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
}
err := logic.StoreCard(db, location)
logic.Check(err)
copiesInserted++
}

View File

@ -43,19 +43,3 @@ func InsertCardLocation(db *sql.DB, storageLocation CardLocation) error {
return 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

@ -7,14 +7,12 @@ 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
@ -23,21 +21,7 @@ type LocateCardResult struct {
func GetLocateResults(db *sql.DB, cardNames []string) ([]LocateCardResult, error) {
var results []LocateCardResult
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 := "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, args, err := sqlx.In(query, cardNames)
if err != nil {
return results, err
@ -51,7 +35,7 @@ func GetLocateResults(db *sql.DB, cardNames []string) ([]LocateCardResult, error
var result LocateCardResult
for rows.Next() {
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)
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
}

View File

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

View File

@ -34,34 +34,6 @@ func GetAllStorageAreas(db *sql.DB) ([]StorageArea, error) {
return storageAreas, nil
}
func GetStorageAreaTypeById(db *sql.DB, storageAreaId int) (string, error) {
var storageType string
query := `SELECT StorageType FROM StorageArea WHERE Id = ?;`
row := db.QueryRow(query, storageAreaId)
err := row.Scan(&storageType)
if err != nil {
return storageType, err
}
return storageType, nil
}
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;`
var emptySlotId int
err := db.QueryRow(query, storageAreaId).Scan(&emptySlotId)
if err == sql.ErrNoRows {
return -1, nil
} else if err != nil {
return 0, err
}
return emptySlotId, nil
}
func InsertStorageArea(db *sql.DB, storageArea StorageArea) error {
query := `INSERT INTO StorageArea (Name, StorageType) VALUES (?, ?);`
@ -77,3 +49,17 @@ func InsertStorageArea(db *sql.DB, storageArea StorageArea) error {
return nil
}
func GetStorageAreaTypeById(db *sql.DB, storageAreaId int) (string, error) {
var storageType string
query := `SELECT StorageType FROM StorageArea WHERE Id = ?;`
row := db.QueryRow(query, storageAreaId)
err := row.Scan(&storageType)
if err != nil {
return storageType, err
}
return storageType, nil
}

View File

@ -1,50 +0,0 @@
package delverlens
import (
"encoding/csv"
"io"
"os"
"strings"
)
type DelverLensCard struct {
ScryfallID string
IsFoil bool
}
func ParseExportFile(filename string) ([]DelverLensCard, error) {
var cards []DelverLensCard
file, err := os.Open("/home/viciouscirce/dox/sevenkeys_imports/" + filename)
if err != nil {
return cards, err
}
r := csv.NewReader(file)
var isHeader bool = true
for {
record, err := r.Read()
if err == io.EOF {
break
} else if err != nil {
return cards, err
}
// Skip the header line
if isHeader {
isHeader = false
continue
}
card := DelverLensCard{
Name: record[0],
IsFoil: record[1] == "Foil",
CollectorNumber: record[2],
SetCode: strings.ToLower(record[3]),
}
cards = append(cards, card)
}
return cards, nil
}

View File

@ -1,25 +0,0 @@
package logic
import (
"database/sql"
"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 + "n"
} else {
cardPrintingId = card.ScryfallID + "f"
}
cardLocation := database.CardLocation{
CardPrintingId: cardPrintingId,
StorageAreaId: storageAreaId,
}
StoreCard(cardLocation)
}
}

View File

@ -9,12 +9,9 @@ import (
const SLOTS_PER_BINDER_PAGE = 18 // TODO: Make this configurable
func GetBinderLocationDescription(position int) string {
var page int = ((position - 1) / SLOTS_PER_BINDER_PAGE) + 1
var page int = (position / SLOTS_PER_BINDER_PAGE) + 1
var pagePosition int = position % SLOTS_PER_BINDER_PAGE
if pagePosition == 0 {
pagePosition = SLOTS_PER_BINDER_PAGE
}
var slot int
var frontOrBack string
@ -30,14 +27,15 @@ 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) ([]database.LocateCardResult, error) {
func LocateCards(db *sql.DB, cardNames []string, criteria SearchCriteria) ([]string, error) {
var locations []string
results, err := database.GetLocateResults(db, cardNames)
if err != nil {
return results, err
return locations, err
}
var filteredResults []database.LocateCardResult
var location string
for _, result := range results {
printing := database.CardPrinting{
SetCode: result.SetCode,
@ -51,37 +49,31 @@ func LocateCards(db *sql.DB, cardNames []string, criteria SearchCriteria) ([]dat
continue
}
filteredResults = append(filteredResults, result)
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"
}
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
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
}

View File

@ -2,44 +2,24 @@ package logic
import "testing"
func assert(t *testing.T, description string, expected string) {
func Test_GetBinderLocationDescription_ReturnsCorrectFormat_ForFrontSlots(t *testing.T) {
var position int = 24
var expected string = " on page 2 in front slot 6"
description := GetBinderLocationDescription(position)
if description != expected {
t.Errorf("expected \"%s\", got \"%s\"\n", expected, description)
t.Errorf("expected %s, got %s\n", expected, description)
}
}
func Test_GetBinderLocationDescription_ReturnsCorrectSlotNumber_ForFirstSlotInPage(t *testing.T) {
var position int = 1
var expected string = " on page 1 in front slot 1"
func Test_GetBinderLocationDescription_ReturnsCorrectFormat_ForBackSlots(t *testing.T) {
var position int = 17
var expected string = " on page 1 in back slot 8"
description := GetBinderLocationDescription(position)
assert(t, description, expected)
}
func Test_GetBinderLocationDescription_ReturnsCorrectSlotNumber_ForFirstSlotOnBackOfPage(t *testing.T) {
var position int = 10
var expected string = " on page 1 in back slot 1"
description := GetBinderLocationDescription(position)
assert(t, description, expected)
}
func Test_GetBinderLocationDescription_ReturnsCorrectSlotNumber_ForLastSlotOnBackOfPage(t *testing.T) {
var position int = 18
var expected string = " on page 1 in back slot 9"
description := GetBinderLocationDescription(position)
assert(t, description, expected)
}
func Test_GetBinderLocationDescription_ReturnsCorrectSlotNumber_ForLastSlotOnBackOfSecondPage(t *testing.T) {
var position int = 36
var expected string = " on page 2 in back slot 9"
description := GetBinderLocationDescription(position)
assert(t, description, expected)
if description != expected {
t.Errorf("expected %s, got %s\n", expected, description)
}
}

View File

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

View File

@ -61,7 +61,7 @@ func GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (InsertSearc
return searchOptions, err
}
func GenericSearch[pk string | int](options map[string]pk) (pk, string, error) {
func GenericSearch[pk string | int](options map[string]pk) (pk, error) {
var value pk
cmd := exec.Command("fzf")
@ -69,7 +69,7 @@ func GenericSearch[pk string | int](options map[string]pk) (pk, string, error) {
fzfStdin, err := cmd.StdinPipe()
if err != nil {
return value, "", err
return value, err
}
go func() {
@ -81,10 +81,10 @@ func GenericSearch[pk string | int](options map[string]pk) (pk, string, error) {
fzfOutput, err := cmd.Output()
if err != nil {
return value, "", err
return value, err
}
searchResult := strings.TrimSuffix(string(fzfOutput), "\n")
value = options[searchResult]
return value, searchResult, nil
return value, nil
}

View File

@ -2,11 +2,13 @@ package logic
import (
"database/sql"
"io"
"os"
"os/exec"
"sevenkeys/database"
"strings"
)
type StorageAreaSearchOptions map[string]int
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
@ -18,22 +20,46 @@ func CreateStorageArea(db *sql.DB, storageArea database.StorageArea) error {
return nil
}
func GetStorageAreaSearchOptions(db *sql.DB) (StorageAreaSearchOptions, error) {
var options StorageAreaSearchOptions
func SelectStorageArea(db *sql.DB) (database.StorageArea, error) {
var selectedStorageArea database.StorageArea
storageAreas, err := database.GetAllStorageAreas(db)
if err != nil {
return options, err
return selectedStorageArea, err
}
cmd := exec.Command("fzf")
cmd.Stderr = os.Stderr
fzfStdin, err := cmd.StdinPipe()
if err != nil {
return selectedStorageArea, err
}
go func() {
defer fzfStdin.Close()
for _, area := range storageAreas {
options[area.Name] = area.Id
io.WriteString(fzfStdin, area.Name+"\n")
}
}()
fzfOutput, err := cmd.Output()
if err != nil {
return selectedStorageArea, err
}
return options, nil
key := strings.TrimSuffix(string(fzfOutput), "\n")
for _, area := range storageAreas {
if area.Name == key {
selectedStorageArea = area
break
}
}
return selectedStorageArea, nil
}
func StoreAfterLastCard(db *sql.DB, cardLocation database.CardLocation) error {
func StoreCard(db *sql.DB, cardLocation database.CardLocation) error {
lastPosition, err := database.GetLastPositionInStorageArea(db, cardLocation.StorageAreaId)
if err != nil {
return err
@ -48,46 +74,3 @@ func StoreAfterLastCard(db *sql.DB, cardLocation database.CardLocation) error {
return nil
}
func StoreInEmptySlot(db *sql.DB, cardLocation database.CardLocation, cardLocationId int) error {
cardLocation.Id = cardLocationId
err := database.InsertCardInExistingLocation(db, cardLocation)
if err != nil {
return err
}
return nil
}
func StoreCard(db *sql.DB, cardLocation database.CardLocation) error {
storageAreaType, err := database.GetStorageAreaTypeById(db, cardLocation.StorageAreaId)
if err != nil {
return err
}
if storageAreaType == database.StorageAreaTypeBinder {
nextEmptySlotId, err := database.GetNextEmptySlotInBinder(db, cardLocation.StorageAreaId)
if err == sql.ErrNoRows {
err = StoreAfterLastCard(db, cardLocation)
if err != nil {
return err
}
} else if err != nil {
return err
} else {
err = StoreInEmptySlot(db, cardLocation, nextEmptySlotId)
if err != nil {
return err
}
}
return nil
}
err = StoreAfterLastCard(db, cardLocation)
if err != nil {
return err
}
return nil
}