2024-05-30 13:32:04 +00:00
|
|
|
package logic
|
|
|
|
|
2024-05-31 19:32:44 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
2024-08-20 17:00:23 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-05-31 19:32:44 +00:00
|
|
|
"sevenkeys/database"
|
2024-08-20 17:00:23 +00:00
|
|
|
"strings"
|
2024-05-31 19:32:44 +00:00
|
|
|
)
|
2024-05-30 13:32:04 +00:00
|
|
|
|
2024-08-20 11:57:36 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-20 17:00:23 +00:00
|
|
|
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 {
|
2024-09-05 09:53:14 +00:00
|
|
|
io.WriteString(fzfStdin, area.Name+"\n")
|
2024-08-20 17:00:23 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-09-05 09:53:14 +00:00
|
|
|
func StoreCard(db *sql.DB, cardLocation database.CardLocation) error {
|
|
|
|
lastPosition, err := database.GetLastPositionInStorageArea(db, cardLocation.StorageAreaId)
|
2024-05-31 19:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-30 13:32:04 +00:00
|
|
|
|
2024-09-05 09:53:14 +00:00
|
|
|
cardLocation.Position = lastPosition + 1
|
2024-05-30 13:32:04 +00:00
|
|
|
|
2024-09-05 09:53:14 +00:00
|
|
|
err = database.InsertCardLocation(db, cardLocation)
|
2024-05-31 19:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-05-30 13:32:04 +00:00
|
|
|
}
|