Add the ability to remove cards

This commit is contained in:
The Magician 2024-09-12 13:37:41 +01:00
parent 52d9e9184f
commit 1e967674b6
3 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,50 @@
package database
import "database/sql"
func RemoveFromBinder(db *sql.DB, location CardLocation) error {
query := `UPDATE CardStorageLocation SET CardPrintingId = NULL WHERE Id = ?;`
update, err := db.Prepare(query)
defer update.Close()
if err != nil {
return err
}
_, err = update.Exec(location.Id)
if err != nil {
return err
}
return nil
}
func RemoveFromBox(db *sql.DB, location CardLocation) error {
deleteQuery := `DELETE FROM CardStorageLocation WHERE Id = ?;`
del, err := db.Prepare(deleteQuery)
defer del.Close()
if err != nil {
return err
}
_, err = del.Exec(location.Id)
if err != nil {
return err
}
updateQuery := `UPDATE CardStorageLocation SET Position = Position - 1 WHERE Position > 5;`
update, err := db.Prepare(updateQuery)
defer update.Close()
if err != nil {
return err
}
_, err = update.Exec(location.Id)
if err != nil {
return err
}
return nil
}

View File

@ -2,6 +2,9 @@ package database
import "database/sql"
var StorageAreaTypeBinder string = "Binder"
var StorageAreaTypeBox string = "Box"
type StorageArea struct {
Id int
Name string
@ -46,3 +49,17 @@ func InsertStorageArea(db *sql.DB, storageArea StorageArea) error {
return nil
}
func GetStorageAreaTypeById(db *sql.DB, storageAreaId int) (string, error) {
var storageType string
query := `SELECT StorageType FROM StorageArea WHERE Id = ?;`
row := db.QueryRow(query, storageAreaId)
err := row.Scan(&storageType)
if err != nil {
return storageType, err
}
return storageType, nil
}

27
sevenkeys/logic/remove.go Normal file
View File

@ -0,0 +1,27 @@
package logic
import (
"database/sql"
"errors"
"sevenkeys/database"
)
var UnrecognizedStorageAreaTypeError error = errors.New("Unrecognized storage area type.")
func RemoveFromStorage(db *sql.DB, location database.CardLocation) error {
locationType, err := database.GetStorageAreaTypeById(db, location.Id)
if err != nil {
return err
}
if locationType == database.StorageAreaTypeBinder {
database.RemoveFromBinder(db, location)
} else if locationType == database.StorageAreaTypeBox {
database.RemoveFromBox(db, location)
} else {
return UnrecognizedStorageAreaTypeError
}
return nil
}