Fix position increment bug

This commit is contained in:
The Magician 2024-06-01 22:18:33 +01:00
parent 7dba7e4649
commit a5934a1d9f
3 changed files with 35 additions and 21 deletions

View File

@ -1,6 +1,8 @@
package database package database
import "database/sql" import (
"database/sql"
)
type CardStorageLocation struct { type CardStorageLocation struct {
Id int Id int
@ -11,14 +13,14 @@ type CardStorageLocation struct {
} }
func GetLastPositionInBox(db *sql.DB, storageBox string) (int, error) { func GetLastPositionInBox(db *sql.DB, storageBox string) (int, error) {
query := "SELECT Position FROM CardStorageLocation WHERE StorageBox = ? ORDER BY Position DESC LIMIT 1" query := "SELECT Position FROM CardStorageLocation WHERE StorageBox = ? ORDER BY Position DESC LIMIT 1;"
var lastPosition int var lastPosition int
err := db.QueryRow(query, storageBox).Scan(&lastPosition) err := db.QueryRow(query, storageBox).Scan(&lastPosition)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return 0, nil return 0, nil
} else { } else if err != nil {
return 0, err return 0, err
} }

View File

@ -2,7 +2,6 @@ package logic
import ( import (
"database/sql" "database/sql"
"log"
"sevenkeys/database" "sevenkeys/database"
) )
@ -13,7 +12,6 @@ func StoreCard(db *sql.DB, storageLocation database.CardStorageLocation) error {
} }
storageLocation.Position = lastPosition + 1 storageLocation.Position = lastPosition + 1
log.Println(storageLocation.Position)
err = database.InsertCardStorageLocation(db, storageLocation) err = database.InsertCardStorageLocation(db, storageLocation)
if err != nil { if err != nil {

View File

@ -40,39 +40,53 @@ func main() {
storageBox := logic.GetResponse("Enter storage box label:") storageBox := logic.GetResponse("Enter storage box label:")
source := logic.GetResponse("Enter source:") source := logic.GetResponse("Enter source:")
storageLocation := database.CardStorageLocation{
StorageBox: storageBox,
Source: source,
}
searchOptions, err := logic.GetAllSearchOptions(db) searchOptions, err := logic.GetAllSearchOptions(db)
logic.Check(err) logic.Check(err)
var selectedCardId int
var selectedCardSearchOption string = "None"
for { for {
/* /*
screen.Clear() screen.Clear()
screen.MoveTopLeft() screen.MoveTopLeft()
*/ */
fmt.Println("Storage location:", storageBox)
fmt.Println("Source:", source)
selectedCardPrintingId, selectedSearchOption, err := logic.Search(searchOptions) fmt.Println("Storage location:", storageBox, "|", "Source:", source)
logic.Check(err) fmt.Println("Selected card:", selectedCardSearchOption, "ID:", selectedCardId)
fmt.Println("Inserted card:", selectedSearchOption) var action string
storageLocation := database.CardStorageLocation{ action = logic.GetResponse("[s]earch for card/[i]nsert selected card/[q]uit:")
CardPrintingId: selectedCardPrintingId, switch action {
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": case "s":
selectedCardId, selectedCardSearchOption, err = logic.Search(searchOptions)
logic.Check(err)
storageLocation.CardPrintingId = selectedCardId
continue continue
case "r": case "i":
if selectedCardId == 0 {
fmt.Println("No selected card, please search for one.")
continue
}
err = logic.StoreCard(db, storageLocation) err = logic.StoreCard(db, storageLocation)
logic.Check(err) logic.Check(err)
break break
case "q": case "q":
os.Exit(0) os.Exit(0)
} default:
fmt.Println("Not a valid command:", action)
break
}
fmt.Print("\n\n")
} }
} }