Fix bug in reporting binder locations

This commit is contained in:
The Magician 2024-09-17 11:25:51 +01:00
parent 2ecb448e29
commit 3ef49bc72c
5 changed files with 56 additions and 72 deletions

View File

@ -15,7 +15,7 @@ var output string
func MainCliLoop(db *sql.DB) { func MainCliLoop(db *sql.DB) {
var command string var command string
var selectedStorageArea database.StorageArea var selectedStorageAreaName string
var cardLocation database.CardLocation var cardLocation database.CardLocation
var insertSearchCriteria logic.SearchCriteria = logic.SearchCriteria{ var insertSearchCriteria logic.SearchCriteria = logic.SearchCriteria{
@ -36,7 +36,7 @@ func MainCliLoop(db *sql.DB) {
for { for {
ShowSplashScreen() ShowSplashScreen()
showStorageInfo(os.Stdout, selectedStorageArea) showStorageInfo(os.Stdout, selectedStorageAreaName)
showInsertSearchCriteria(insertSearchCriteria) showInsertSearchCriteria(insertSearchCriteria)
showSelectedCard() showSelectedCard()
showCopiesInserted() showCopiesInserted()
@ -56,10 +56,14 @@ func MainCliLoop(db *sql.DB) {
logic.Check(err) logic.Check(err)
break break
case "a", "area": case "a", "area":
area, err := logic.SelectStorageArea(db) options, err := logic.GetStorageAreaSearchOptions(db)
logic.Check(err) logic.Check(err)
selectedStorageArea = area
cardLocation.StorageAreaId = area.Id id, name, err := logic.GenericSearch(options)
logic.Check(err)
selectedStorageAreaName = name
cardLocation.StorageAreaId = id
break break
case "c", "criteria": case "c", "criteria":
insertSearchCriteria = getSearchCriteria() insertSearchCriteria = getSearchCriteria()
@ -87,7 +91,9 @@ func MainCliLoop(db *sql.DB) {
} }
break break
case "i", "insert": case "i", "insert":
insertSelectedCard(db, selectedStorageArea, cardLocation) err := logic.StoreCard(db, cardLocation)
logic.Check(err)
copiesInserted++
break break
case "l", "locate": case "l", "locate":
filename := GetStringResponse("Filename:") filename := GetStringResponse("Filename:")

View File

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

@ -9,9 +9,12 @@ import (
const SLOTS_PER_BINDER_PAGE = 18 // TODO: Make this configurable const SLOTS_PER_BINDER_PAGE = 18 // TODO: Make this configurable
func GetBinderLocationDescription(position int) string { func GetBinderLocationDescription(position int) string {
var page int = (position / SLOTS_PER_BINDER_PAGE) + 1 var page int = ((position - 1) / SLOTS_PER_BINDER_PAGE) + 1
var pagePosition int = position % SLOTS_PER_BINDER_PAGE var pagePosition int = position % SLOTS_PER_BINDER_PAGE
if pagePosition == 0 {
pagePosition = SLOTS_PER_BINDER_PAGE
}
var slot int var slot int
var frontOrBack string var frontOrBack string

View File

@ -2,24 +2,44 @@ package logic
import "testing" import "testing"
func Test_GetBinderLocationDescription_ReturnsCorrectFormat_ForFrontSlots(t *testing.T) { func assert(t *testing.T, description string, expected string) {
var position int = 24
var expected string = " on page 2 in front slot 6"
description := GetBinderLocationDescription(position)
if description != expected { if description != expected {
t.Errorf("expected %s, got %s\n", expected, description) t.Errorf("expected \"%s\", got \"%s\"\n", expected, description)
} }
} }
func Test_GetBinderLocationDescription_ReturnsCorrectFormat_ForBackSlots(t *testing.T) { func Test_GetBinderLocationDescription_ReturnsCorrectSlotNumber_ForFirstSlotInPage(t *testing.T) {
var position int = 17 var position int = 1
var expected string = " on page 1 in back slot 8" var expected string = " on page 1 in front slot 1"
description := GetBinderLocationDescription(position) description := GetBinderLocationDescription(position)
if description != expected { assert(t, description, expected)
t.Errorf("expected %s, got %s\n", expected, description) }
}
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)
} }

View File

@ -2,13 +2,11 @@ package logic
import ( import (
"database/sql" "database/sql"
"io"
"os"
"os/exec"
"sevenkeys/database" "sevenkeys/database"
"strings"
) )
type StorageAreaSearchOptions map[string]int
func CreateStorageArea(db *sql.DB, storageArea database.StorageArea) error { 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 there's already a storage are with the same name
// TODO: Check if the type entered is valid // TODO: Check if the type entered is valid
@ -20,43 +18,19 @@ func CreateStorageArea(db *sql.DB, storageArea database.StorageArea) error {
return nil return nil
} }
func SelectStorageArea(db *sql.DB) (database.StorageArea, error) { func GetStorageAreaSearchOptions(db *sql.DB) (StorageAreaSearchOptions, error) {
var selectedStorageArea database.StorageArea var options StorageAreaSearchOptions
storageAreas, err := database.GetAllStorageAreas(db) storageAreas, err := database.GetAllStorageAreas(db)
if err != nil { if err != nil {
return selectedStorageArea, err return options, 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 { for _, area := range storageAreas {
io.WriteString(fzfStdin, area.Name+"\n") options[area.Name] = area.Id
}
}()
fzfOutput, err := cmd.Output()
if err != nil {
return selectedStorageArea, err
} }
key := strings.TrimSuffix(string(fzfOutput), "\n") return options, nil
for _, area := range storageAreas {
if area.Name == key {
selectedStorageArea = area
break
}
}
return selectedStorageArea, nil
} }
func StoreAfterLastCard(db *sql.DB, cardLocation database.CardLocation) error { func StoreAfterLastCard(db *sql.DB, cardLocation database.CardLocation) error {