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

View File

@ -1,11 +1,9 @@
package cli
import (
"database/sql"
"fmt"
"io"
"sevenkeys/database"
"sevenkeys/logic"
)
var (
@ -36,20 +34,3 @@ 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

@ -9,9 +9,12 @@ import (
const SLOTS_PER_BINDER_PAGE = 18 // TODO: Make this configurable
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
if pagePosition == 0 {
pagePosition = SLOTS_PER_BINDER_PAGE
}
var slot int
var frontOrBack string

View File

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

View File

@ -2,13 +2,11 @@ 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
@ -20,43 +18,19 @@ func CreateStorageArea(db *sql.DB, storageArea database.StorageArea) error {
return nil
}
func SelectStorageArea(db *sql.DB) (database.StorageArea, error) {
var selectedStorageArea database.StorageArea
func GetStorageAreaSearchOptions(db *sql.DB) (StorageAreaSearchOptions, error) {
var options StorageAreaSearchOptions
storageAreas, err := database.GetAllStorageAreas(db)
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 {
io.WriteString(fzfStdin, area.Name+"\n")
}
}()
fzfOutput, err := cmd.Output()
if err != nil {
return selectedStorageArea, err
options[area.Name] = area.Id
}
key := strings.TrimSuffix(string(fzfOutput), "\n")
for _, area := range storageAreas {
if area.Name == key {
selectedStorageArea = area
break
}
}
return selectedStorageArea, nil
return options, nil
}
func StoreAfterLastCard(db *sql.DB, cardLocation database.CardLocation) error {