2024-08-20 11:57:36 +00:00
|
|
|
package database
|
|
|
|
|
2024-09-26 12:17:55 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrStorageAreaDoesNotExist error = errors.New("Storage area does not exist.")
|
2024-08-20 11:57:36 +00:00
|
|
|
|
2024-09-12 12:37:41 +00:00
|
|
|
var StorageAreaTypeBinder string = "Binder"
|
|
|
|
var StorageAreaTypeBox string = "Box"
|
|
|
|
|
2024-08-20 11:57:36 +00:00
|
|
|
type StorageArea struct {
|
2024-08-20 17:50:38 +00:00
|
|
|
Id int
|
|
|
|
Name string
|
|
|
|
StorageType string
|
2024-08-20 11:57:36 +00:00
|
|
|
}
|
|
|
|
|
2024-08-20 17:00:23 +00:00
|
|
|
func GetAllStorageAreas(db *sql.DB) ([]StorageArea, error) {
|
|
|
|
var storageAreas []StorageArea
|
|
|
|
|
|
|
|
query := `SELECT * FROM StorageArea;`
|
|
|
|
rows, err := db.Query(query)
|
|
|
|
defer rows.Close()
|
|
|
|
if err != nil {
|
|
|
|
return storageAreas, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var area StorageArea
|
|
|
|
for rows.Next() {
|
2024-08-20 17:50:38 +00:00
|
|
|
err := rows.Scan(&area.Id, &area.Name, &area.StorageType)
|
2024-08-20 17:00:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return storageAreas, err
|
|
|
|
}
|
|
|
|
|
|
|
|
storageAreas = append(storageAreas, area)
|
|
|
|
}
|
|
|
|
|
|
|
|
return storageAreas, nil
|
|
|
|
}
|
|
|
|
|
2024-09-26 12:17:55 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-09-16 16:44:57 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetNextEmptySlotInBinder(db *sql.DB, storageAreaId int) (int, error) {
|
|
|
|
query := `SELECT Id FROM CardLocation WHERE CardPrintingId IS NULL AND StorageAreaId = ? ORDER BY Position ASC LIMIT 1;`
|
|
|
|
|
|
|
|
var emptySlotId int
|
|
|
|
err := db.QueryRow(query, storageAreaId).Scan(&emptySlotId)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return -1, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return emptySlotId, nil
|
|
|
|
}
|
|
|
|
|
2024-08-20 11:57:36 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-20 17:50:38 +00:00
|
|
|
_, err = insert.Exec(storageArea.Name, storageArea.StorageType)
|
2024-08-20 11:57:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|