Implement basic search-and-store functionality
This commit is contained in:
parent
867143c511
commit
4018f26998
|
@ -25,7 +25,7 @@ func GetLastPositionInBox(db *sql.DB, storageBox string) (int, error) {
|
||||||
return lastPosition, nil
|
return lastPosition, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func InsertCardStorageLocation(db *sql.DB, location CardStorageLocation) error {
|
func InsertCardStorageLocation(db *sql.DB, storageLocation CardStorageLocation) error {
|
||||||
query := `INSERT INTO CardStorageLocation
|
query := `INSERT INTO CardStorageLocation
|
||||||
(CardPrintingId, StorageBox, Position, Source)
|
(CardPrintingId, StorageBox, Position, Source)
|
||||||
VALUES (?, ?, ?, ?);`
|
VALUES (?, ?, ?, ?);`
|
||||||
|
@ -35,7 +35,7 @@ func InsertCardStorageLocation(db *sql.DB, location CardStorageLocation) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = insert.Exec(location.CardPrintingId, location.StorageBox, location.Position, location.Source)
|
_, err = insert.Exec(storageLocation.CardPrintingId, storageLocation.StorageBox, storageLocation.Position, storageLocation.Source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,11 @@ package logic
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
"sevenkeys/database"
|
"sevenkeys/database"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// The SearchOptions type is a map of `string`s (which can be searched using fzf)
|
// The SearchOptions type is a map of `string`s (which can be searched using fzf)
|
||||||
|
@ -34,3 +38,29 @@ func GetAllSearchOptions(db *sql.DB) (SearchOptions, error) {
|
||||||
|
|
||||||
return searchOptions, err
|
return searchOptions, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Search(searchOptions SearchOptions) (int, string, error) {
|
||||||
|
cmd := exec.Command("fzf")
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
|
||||||
|
fzfStdin, err := cmd.StdinPipe()
|
||||||
|
if err != nil {
|
||||||
|
return -1, "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer fzfStdin.Close()
|
||||||
|
for searchString, _ := range searchOptions {
|
||||||
|
io.WriteString(fzfStdin, searchString+"\n")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
fzfOutput, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return -1, "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
key := strings.TrimSuffix(string(fzfOutput), "\n")
|
||||||
|
|
||||||
|
return searchOptions[key], key, nil
|
||||||
|
}
|
||||||
|
|
|
@ -1,12 +1,22 @@
|
||||||
package logic
|
package logic
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"sevenkeys/database"
|
||||||
|
)
|
||||||
|
|
||||||
func GetStorageLocation() string {
|
func StoreCard(db *sql.DB, storageLocation database.CardStorageLocation) error {
|
||||||
fmt.Print("Enter storage location: ")
|
lastPosition, err := database.GetLastPositionInBox(db, storageLocation.StorageBox)
|
||||||
|
if err != nil {
|
||||||
var response string
|
return err
|
||||||
fmt.Scan(&response)
|
}
|
||||||
|
|
||||||
return response
|
storageLocation.Position = lastPosition + 1
|
||||||
|
|
||||||
|
err = database.InsertCardStorageLocation(db, storageLocation)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
package logic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetResponse(prompt string) string {
|
||||||
|
fmt.Print(prompt, " ")
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
var response string
|
||||||
|
if scanner.Scan() {
|
||||||
|
response = scanner.Text()
|
||||||
|
}
|
||||||
|
|
||||||
|
return response
|
||||||
|
}
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"sevenkeys/database"
|
"sevenkeys/database"
|
||||||
"sevenkeys/logic"
|
"sevenkeys/logic"
|
||||||
"sevenkeys/logic/scryfall"
|
"sevenkeys/logic/scryfall"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/inancgumus/screen"
|
"github.com/inancgumus/screen"
|
||||||
)
|
)
|
||||||
|
@ -40,13 +39,38 @@ func main() {
|
||||||
fmt.Println("No update required.")
|
fmt.Println("No update required.")
|
||||||
}
|
}
|
||||||
|
|
||||||
storageLocation := logic.GetStorageLocation()
|
storageBox := logic.GetResponse("Enter storage box label:")
|
||||||
|
source := logic.GetResponse("Enter source:")
|
||||||
|
searchOptions, err := logic.GetAllSearchOptions(db)
|
||||||
|
logic.Check(err)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
screen.Clear()
|
screen.Clear()
|
||||||
screen.MoveTopLeft()
|
screen.MoveTopLeft()
|
||||||
fmt.Println("Current storage location:", storageLocation)
|
fmt.Println("Storage location:", storageBox)
|
||||||
|
fmt.Println("Source:", source)
|
||||||
|
|
||||||
time.Sleep(1 * time.Second)
|
selectedCardPrintingId, selectedSearchOption, err := logic.Search(searchOptions)
|
||||||
|
logic.Check(err)
|
||||||
|
|
||||||
|
fmt.Println("Inserted card:", selectedSearchOption)
|
||||||
|
storageLocation := database.CardStorageLocation{
|
||||||
|
CardPrintingId: selectedCardPrintingId,
|
||||||
|
StorageBox: storageBox,
|
||||||
|
Source: source,
|
||||||
|
}
|
||||||
|
err = logic.StoreCard(db, storageLocation)
|
||||||
|
logic.Check(err)
|
||||||
|
|
||||||
|
nextAction := logic.GetResponse("[s]earch again/[r]epeat last insert/[q]uit:")
|
||||||
|
switch nextAction {
|
||||||
|
case "s":
|
||||||
|
continue
|
||||||
|
case "r":
|
||||||
|
err = logic.StoreCard(db, storageLocation)
|
||||||
|
logic.Check(err)
|
||||||
|
case "q":
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
/*
|
|
||||||
func main() {
|
|
||||||
db := database.GetDatabaseFromConfig("config.json")
|
|
||||||
cardSearchOptions, err := operations.GetCardSearchOptions(db)
|
|
||||||
check(err)
|
|
||||||
|
|
||||||
cmd := exec.Command("fzf")
|
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
|
|
||||||
fzfStdin, err := cmd.StdinPipe()
|
|
||||||
check(err)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer fzfStdin.Close()
|
|
||||||
for _, option := range cardSearchOptions {
|
|
||||||
io.WriteString(fzfStdin, option+"\n")
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
fzfOutput, err := cmd.Output()
|
|
||||||
check(err)
|
|
||||||
|
|
||||||
fmt.Println("Output:", string(fzfOutput))
|
|
||||||
}
|
|
||||||
*/
|
|
Loading…
Reference in New Issue