TheMathemagicians/sevenkeys/database/storagearea.go

49 lines
908 B
Go
Raw Normal View History

2024-08-20 11:57:36 +00:00
package database
import "database/sql"
type StorageArea struct {
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() {
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-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
}
_, err = insert.Exec(storageArea.Name, storageArea.StorageType)
2024-08-20 11:57:36 +00:00
if err != nil {
return err
}
return nil
}