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
import "database/sql"
import (
"database/sql"
)
type CardStorageLocation struct {
Id int
@ -11,14 +13,14 @@ type CardStorageLocation struct {
}
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
err := db.QueryRow(query, storageBox).Scan(&lastPosition)
if err == sql.ErrNoRows {
return 0, nil
} else {
} else if err != nil {
return 0, err
}

View File

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

View File

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