Compare commits
No commits in common. "0e3ee54e1cba02b80378aa54b7bd76bbf90f6993" and "70f0b4d4f7b5fe428d0ac75821b48b4199b6a17d" have entirely different histories.
0e3ee54e1c
...
70f0b4d4f7
|
@ -64,7 +64,6 @@ func MainCliLoop(db *sql.DB) {
|
||||||
logic.Check(err)
|
logic.Check(err)
|
||||||
|
|
||||||
//selectedStorageAreaName = name
|
//selectedStorageAreaName = name
|
||||||
// TODO: Make db call to cache StorageArea once we have the ID
|
|
||||||
cardLocation.StorageAreaId = id
|
cardLocation.StorageAreaId = id
|
||||||
break
|
break
|
||||||
case "c", "criteria":
|
case "c", "criteria":
|
||||||
|
@ -103,7 +102,7 @@ func MainCliLoop(db *sql.DB) {
|
||||||
cards, err := delverlens.ParseExportFile(filename)
|
cards, err := delverlens.ParseExportFile(filename)
|
||||||
logic.Check(err)
|
logic.Check(err)
|
||||||
|
|
||||||
err = logic.ImportDelverLensCards(db, cards, cardLocation.StorageAreaId)
|
err = logic.ImportDelverLensCards(cards)
|
||||||
logic.Check(err)
|
logic.Check(err)
|
||||||
break
|
break
|
||||||
case "l", "locate":
|
case "l", "locate":
|
||||||
|
|
|
@ -1,12 +1,6 @@
|
||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import "database/sql"
|
||||||
"database/sql"
|
|
||||||
"errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
var ErrStorageAreaDoesNotExist error = errors.New("Storage area does not exist.")
|
|
||||||
var ErrNoEmptySlotsInBinder error = errors.New("No empty slots in binder.")
|
|
||||||
|
|
||||||
var StorageAreaTypeBinder string = "Binder"
|
var StorageAreaTypeBinder string = "Binder"
|
||||||
var StorageAreaTypeBox string = "Box"
|
var StorageAreaTypeBox string = "Box"
|
||||||
|
@ -40,22 +34,6 @@ func GetAllStorageAreas(db *sql.DB) ([]StorageArea, error) {
|
||||||
return storageAreas, nil
|
return storageAreas, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetStorageAreaIdByName(db *sql.DB, storageAreaName string) (int, error) {
|
|
||||||
var storageAreaId int
|
|
||||||
|
|
||||||
query := `SELECT Id FROM StorageArea WHERE Name = ?;`
|
|
||||||
row := db.QueryRow(query, storageAreaName)
|
|
||||||
|
|
||||||
err := row.Scan(&storageAreaId)
|
|
||||||
if err == sql.ErrNoRows {
|
|
||||||
return storageAreaId, ErrStorageAreaDoesNotExist
|
|
||||||
} else if err != nil {
|
|
||||||
return storageAreaId, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return storageAreaId, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetStorageAreaTypeById(db *sql.DB, storageAreaId int) (string, error) {
|
func GetStorageAreaTypeById(db *sql.DB, storageAreaId int) (string, error) {
|
||||||
var storageType string
|
var storageType string
|
||||||
|
|
||||||
|
@ -76,7 +54,7 @@ func GetNextEmptySlotInBinder(db *sql.DB, storageAreaId int) (int, error) {
|
||||||
var emptySlotId int
|
var emptySlotId int
|
||||||
err := db.QueryRow(query, storageAreaId).Scan(&emptySlotId)
|
err := db.QueryRow(query, storageAreaId).Scan(&emptySlotId)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
return -1, ErrNoEmptySlotsInBinder
|
return -1, nil
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DelverLensCard struct {
|
type DelverLensCard struct {
|
||||||
|
@ -15,7 +16,6 @@ func ParseExportFile(filename string) ([]DelverLensCard, error) {
|
||||||
var cards []DelverLensCard
|
var cards []DelverLensCard
|
||||||
|
|
||||||
file, err := os.Open("/home/viciouscirce/dox/sevenkeys_imports/" + filename)
|
file, err := os.Open("/home/viciouscirce/dox/sevenkeys_imports/" + filename)
|
||||||
defer file.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cards, err
|
return cards, err
|
||||||
}
|
}
|
||||||
|
@ -38,8 +38,10 @@ func ParseExportFile(filename string) ([]DelverLensCard, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
card := DelverLensCard{
|
card := DelverLensCard{
|
||||||
ScryfallID: record[0],
|
Name: record[0],
|
||||||
IsFoil: record[1] == "Foil",
|
IsFoil: record[1] == "Foil",
|
||||||
|
CollectorNumber: record[2],
|
||||||
|
SetCode: strings.ToLower(record[3]),
|
||||||
}
|
}
|
||||||
cards = append(cards, card)
|
cards = append(cards, card)
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,9 +10,9 @@ func ImportDelverLensCards(db *sql.DB, cards []delverlens.DelverLensCard, storag
|
||||||
for _, card := range cards {
|
for _, card := range cards {
|
||||||
var cardPrintingId string
|
var cardPrintingId string
|
||||||
if card.IsFoil {
|
if card.IsFoil {
|
||||||
cardPrintingId = card.ScryfallID + "f"
|
|
||||||
} else {
|
|
||||||
cardPrintingId = card.ScryfallID + "n"
|
cardPrintingId = card.ScryfallID + "n"
|
||||||
|
} else {
|
||||||
|
cardPrintingId = card.ScryfallID + "f"
|
||||||
}
|
}
|
||||||
|
|
||||||
cardLocation := database.CardLocation{
|
cardLocation := database.CardLocation{
|
||||||
|
@ -20,8 +20,6 @@ func ImportDelverLensCards(db *sql.DB, cards []delverlens.DelverLensCard, storag
|
||||||
StorageAreaId: storageAreaId,
|
StorageAreaId: storageAreaId,
|
||||||
}
|
}
|
||||||
|
|
||||||
StoreCard(db, cardLocation)
|
StoreCard(cardLocation)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,34 +2,10 @@ package logic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
|
||||||
"sevenkeys/database"
|
"sevenkeys/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrCouldNotGetStorageAreaId error = errors.New("Could not get storage area ID.")
|
type StorageAreaSearchOptions map[string]int
|
||||||
|
|
||||||
func GetStorageAreaId(db *sql.DB, storageAreaName string) (int, error) {
|
|
||||||
id, err := database.GetStorageAreaIdByName(db, storageAreaName)
|
|
||||||
if err == nil {
|
|
||||||
return id, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err == database.ErrStorageAreaDoesNotExist {
|
|
||||||
storageOptions, err := GetStorageAreaSearchOptions(db)
|
|
||||||
if err != nil {
|
|
||||||
return id, err
|
|
||||||
}
|
|
||||||
|
|
||||||
id, _, err := GenericSearch(storageOptions)
|
|
||||||
if err == nil {
|
|
||||||
return id, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return id, ErrCouldNotGetStorageAreaId
|
|
||||||
}
|
|
||||||
|
|
||||||
return id, ErrCouldNotGetStorageAreaId
|
|
||||||
}
|
|
||||||
|
|
||||||
func CreateStorageArea(db *sql.DB, storageArea database.StorageArea) error {
|
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 there's already a storage are with the same name
|
||||||
|
@ -42,10 +18,8 @@ func CreateStorageArea(db *sql.DB, storageArea database.StorageArea) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type StorageAreaSearchOptions map[string]int
|
|
||||||
|
|
||||||
func GetStorageAreaSearchOptions(db *sql.DB) (StorageAreaSearchOptions, error) {
|
func GetStorageAreaSearchOptions(db *sql.DB) (StorageAreaSearchOptions, error) {
|
||||||
var options StorageAreaSearchOptions = make(map[string]int)
|
var options StorageAreaSearchOptions
|
||||||
|
|
||||||
storageAreas, err := database.GetAllStorageAreas(db)
|
storageAreas, err := database.GetAllStorageAreas(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -59,7 +33,7 @@ func GetStorageAreaSearchOptions(db *sql.DB) (StorageAreaSearchOptions, error) {
|
||||||
return options, nil
|
return options, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func storeAfterLastCard(db *sql.DB, cardLocation database.CardLocation) error {
|
func StoreAfterLastCard(db *sql.DB, cardLocation database.CardLocation) error {
|
||||||
lastPosition, err := database.GetLastPositionInStorageArea(db, cardLocation.StorageAreaId)
|
lastPosition, err := database.GetLastPositionInStorageArea(db, cardLocation.StorageAreaId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -75,7 +49,7 @@ func storeAfterLastCard(db *sql.DB, cardLocation database.CardLocation) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func storeInEmptySlot(db *sql.DB, cardLocation database.CardLocation, cardLocationId int) error {
|
func StoreInEmptySlot(db *sql.DB, cardLocation database.CardLocation, cardLocationId int) error {
|
||||||
cardLocation.Id = cardLocationId
|
cardLocation.Id = cardLocationId
|
||||||
err := database.InsertCardInExistingLocation(db, cardLocation)
|
err := database.InsertCardInExistingLocation(db, cardLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -94,15 +68,15 @@ func StoreCard(db *sql.DB, cardLocation database.CardLocation) error {
|
||||||
if storageAreaType == database.StorageAreaTypeBinder {
|
if storageAreaType == database.StorageAreaTypeBinder {
|
||||||
nextEmptySlotId, err := database.GetNextEmptySlotInBinder(db, cardLocation.StorageAreaId)
|
nextEmptySlotId, err := database.GetNextEmptySlotInBinder(db, cardLocation.StorageAreaId)
|
||||||
|
|
||||||
if err == database.ErrNoEmptySlotsInBinder {
|
if err == sql.ErrNoRows {
|
||||||
err = storeAfterLastCard(db, cardLocation)
|
err = StoreAfterLastCard(db, cardLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
err = storeInEmptySlot(db, cardLocation, nextEmptySlotId)
|
err = StoreInEmptySlot(db, cardLocation, nextEmptySlotId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -111,10 +85,9 @@ func StoreCard(db *sql.DB, cardLocation database.CardLocation) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err = storeAfterLastCard(db, cardLocation)
|
err = StoreAfterLastCard(db, cardLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,61 +1,25 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"sevenkeys/cli"
|
"sevenkeys/cli"
|
||||||
"sevenkeys/database"
|
"sevenkeys/database"
|
||||||
"sevenkeys/delverlens"
|
|
||||||
"sevenkeys/figlet"
|
"sevenkeys/figlet"
|
||||||
"sevenkeys/logic"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
ImportSubcommand string = "import"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var profile string
|
var profile string
|
||||||
flag.StringVar(&profile, "profile", "development", "The database profile to use.")
|
if len(os.Args) < 2 {
|
||||||
|
profile = "production"
|
||||||
flag.Parse()
|
} else {
|
||||||
|
profile = os.Args[1]
|
||||||
|
}
|
||||||
db := database.GetDatabaseFromConfig("config." + profile + ".json")
|
db := database.GetDatabaseFromConfig("config." + profile + ".json")
|
||||||
|
|
||||||
figlet.ReadFigletFonts()
|
figlet.ReadFigletFonts()
|
||||||
cli.ShowSplashScreen()
|
cli.ShowSplashScreen()
|
||||||
|
|
||||||
cli.RunUpdateCheck(db)
|
cli.RunUpdateCheck(db)
|
||||||
|
|
||||||
// TODO: Decide in what form we need to retain this functionality if any
|
cli.MainCliLoop(db)
|
||||||
//cli.MainCliLoop(db)
|
|
||||||
|
|
||||||
importCmd := flag.NewFlagSet("import", flag.ExitOnError)
|
|
||||||
storageArea := importCmd.String("storagearea", "", "The name of the StorageArea where cards should be imported.")
|
|
||||||
|
|
||||||
if len(flag.Args()) == 0 {
|
|
||||||
fmt.Fprintln(os.Stderr, "Please specify a subcommand.")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch flag.Args()[0] {
|
|
||||||
case ImportSubcommand:
|
|
||||||
importCmd.Parse(flag.Args()[1:])
|
|
||||||
|
|
||||||
storageAreaId, err := logic.GetStorageAreaId(db, *storageArea)
|
|
||||||
if err == logic.ErrCouldNotGetStorageAreaId {
|
|
||||||
fmt.Fprintf(os.Stderr, "[sevenkeys] No storage area was selected, exiting.\n")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
logic.Check(err)
|
|
||||||
|
|
||||||
delverLensCards, err := delverlens.ParseExportFile(importCmd.Args()[0])
|
|
||||||
logic.Check(err)
|
|
||||||
|
|
||||||
err = logic.ImportDelverLensCards(db, delverLensCards, storageAreaId)
|
|
||||||
logic.Check(err)
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
fmt.Fprintf(os.Stderr, "Unrecognized subcommand: %s\n", os.Args[1])
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue