Add storage areas to database

This commit is contained in:
The Magician 2024-08-20 12:57:36 +01:00
parent e5f5fbaee2
commit 8fe02f46d6
3 changed files with 47 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"os/exec" "os/exec"
"sevenkeys/database"
"sevenkeys/logic" "sevenkeys/logic"
) )
@ -28,16 +29,25 @@ func MainCliLoop(db *sql.DB) {
command = GetStringResponse("SEVENKEYS $") command = GetStringResponse("SEVENKEYS $")
var err error
switch command { switch command {
case "q", "quit": case "q", "quit":
return return
case "n", "newstorage":
var storageArea database.StorageArea
storageArea.Name = GetStringResponse("Storage area name:")
storageArea.Type = GetStringResponse("Storage area type (Binder/Box):")
err = logic.CreateStorageArea(db, storageArea)
logic.Check(err)
break
case "l", "location": case "l", "location":
cardStorageLocation.StorageBox = GetStringResponse("Storage location:") cardStorageLocation.StorageBox = GetStringResponse("Storage location:")
break break
case "r", "source": case "r", "source":
cardStorageLocation.Source = GetStringResponse("Card source:") cardStorageLocation.Source = GetStringResponse("Card source:")
break break
case "n", "condition": case "p", "condition":
cardStorageLocation.CardCondition = GetStringResponse("Card condition:") cardStorageLocation.CardCondition = GetStringResponse("Card condition:")
break break
case "c", "criteria": case "c", "criteria":
@ -48,7 +58,6 @@ func MainCliLoop(db *sql.DB) {
var previousCardPrintingId = cardStorageLocation.CardPrintingId var previousCardPrintingId = cardStorageLocation.CardPrintingId
var err error
cardStorageLocation.CardPrintingId, selectedCardPrintingSearchLine, err = logic.Search(searchOptions) cardStorageLocation.CardPrintingId, selectedCardPrintingSearchLine, err = logic.Search(searchOptions)
var exitError *exec.ExitError var exitError *exec.ExitError
if errors.As(err, &exitError) { if errors.As(err, &exitError) {

View File

@ -0,0 +1,25 @@
package database
import "database/sql"
type StorageArea struct {
Id int
Name string
Type string
}
func InsertStorageArea(db *sql.DB, storageArea StorageArea) error {
query := `INSERT INTO StorageArea (Name, StorageType) VALUES (?, ?);`
insert, err := db.Prepare(query)
if err != nil {
return err
}
_, err = insert.Exec(storageArea.Name, storageArea.Type)
if err != nil {
return err
}
return nil
}

View File

@ -5,6 +5,17 @@ import (
"sevenkeys/database" "sevenkeys/database"
) )
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
err := database.InsertStorageArea(db, storageArea)
if err != nil {
return err
}
return 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 {