Add the ability to select storage area

This commit is contained in:
The Magician 2024-08-20 18:00:23 +01:00
parent de116aee20
commit eb5ecd5fb4
3 changed files with 70 additions and 2 deletions

View File

@ -43,8 +43,10 @@ func MainCliLoop(db *sql.DB) {
err = logic.CreateStorageArea(db, storageArea) err = logic.CreateStorageArea(db, storageArea)
logic.Check(err) logic.Check(err)
break break
case "l", "location": case "a", "area":
cardStorageLocation.StorageBox = GetStringResponse("Storage location:") area, err := logic.SelectStorageArea(db)
logic.Check(err)
selectedStorageArea = area
break break
case "r", "source": case "r", "source":
cardStorageLocation.Source = GetStringResponse("Card source:") cardStorageLocation.Source = GetStringResponse("Card source:")

View File

@ -8,6 +8,29 @@ type StorageArea struct {
Type string Type string
} }
func GetAllStorageAreas(db *sql.DB) ([]StorageArea, error) {
var storageAreas []StorageArea
query := `SELECT * FROM StorageArea;`
rows, err := db.Query(query)
defer rows.Close()
if err != nil {
return storageAreas, err
}
var area StorageArea
for rows.Next() {
err := rows.Scan(&area.Id, &area.Name, &area.Type)
if err != nil {
return storageAreas, err
}
storageAreas = append(storageAreas, area)
}
return storageAreas, nil
}
func InsertStorageArea(db *sql.DB, storageArea StorageArea) error { func InsertStorageArea(db *sql.DB, storageArea StorageArea) error {
query := `INSERT INTO StorageArea (Name, StorageType) VALUES (?, ?);` query := `INSERT INTO StorageArea (Name, StorageType) VALUES (?, ?);`

View File

@ -2,7 +2,11 @@ package logic
import ( import (
"database/sql" "database/sql"
"io"
"os"
"os/exec"
"sevenkeys/database" "sevenkeys/database"
"strings"
) )
func CreateStorageArea(db *sql.DB, storageArea database.StorageArea) error { func CreateStorageArea(db *sql.DB, storageArea database.StorageArea) error {
@ -16,6 +20,45 @@ func CreateStorageArea(db *sql.DB, storageArea database.StorageArea) error {
return nil return nil
} }
func SelectStorageArea(db *sql.DB) (database.StorageArea, error) {
var selectedStorageArea database.StorageArea
storageAreas, err := database.GetAllStorageAreas(db)
if err != nil {
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 {
io.WriteString(fzfStdin, area.Name)
}
}()
fzfOutput, err := cmd.Output()
if err != nil {
return selectedStorageArea, err
}
key := strings.TrimSuffix(string(fzfOutput), "\n")
for _, area := range storageAreas {
if area.Name == key {
selectedStorageArea = area
break
}
}
return selectedStorageArea, nil
}
func StoreCard(db *sql.DB, storageLocation database.CardStorageLocation) error { func StoreCard(db *sql.DB, storageLocation database.CardStorageLocation) error {
lastPosition, err := database.GetLastPositionInBox(db, storageLocation.StorageBox) lastPosition, err := database.GetLastPositionInBox(db, storageLocation.StorageBox)
if err != nil { if err != nil {