TheMathemagicians/sevenkeys/database/storagearea.go

66 lines
1.3 KiB
Go

package database
import "database/sql"
var StorageAreaTypeBinder string = "Binder"
var StorageAreaTypeBox string = "Box"
type StorageArea struct {
Id int
Name string
StorageType string
}
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() {
err := rows.Scan(&area.Id, &area.Name, &area.StorageType)
if err != nil {
return storageAreas, err
}
storageAreas = append(storageAreas, area)
}
return storageAreas, nil
}
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.StorageType)
if err != nil {
return err
}
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
}