config and databse should be close to done
This commit is contained in:
116
database.go
Normal file
116
database.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/asdine/storm/v3"
|
||||
)
|
||||
|
||||
// Location is a top level component that starts the containerization process
|
||||
type Location struct {
|
||||
ID int `storm:"id,increment,index"`
|
||||
Name string `storm:"unique"`
|
||||
Description string
|
||||
CoverPhoto string // A "cover photo" for the location
|
||||
Photos []string // A list of additional photos for the location
|
||||
Rooms []Room
|
||||
Notes string
|
||||
}
|
||||
|
||||
// Room is a containerized area at a location
|
||||
type Room struct {
|
||||
ID int `storm:"id,increment,index"`
|
||||
CoverPhoto string // A "cover photo" for the room
|
||||
Photos []string // A list of additional photos for the room
|
||||
Name string `storm:"unique"`
|
||||
Description string
|
||||
LocationID int //Which location room is assigned to
|
||||
Notes string
|
||||
}
|
||||
|
||||
// Item is contained inside a room
|
||||
type Item struct {
|
||||
ID int `storm:"id, increment"`
|
||||
RoomID int // Room ID that item is assigned to
|
||||
CoverPhoto string // A "cover photo" for the item
|
||||
Photos []string // A list of additional photos for the room
|
||||
Name string
|
||||
Product string
|
||||
Serial string
|
||||
Warranty string
|
||||
Notes string
|
||||
}
|
||||
|
||||
//SetupDatabase initializes the storm/bbolt database
|
||||
func SetupDatabase(s *Server) (db *storm.DB) {
|
||||
db, err := storm.Open(filepath.Clean("./app/database/goInventorize.db"))
|
||||
if err != nil {
|
||||
s.Log.Fatal().Msgf("Unable to create/open database! %s", err)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
// AddLocation adds a location to the database
|
||||
func (s *Server) AddLocation(loc Location) error {
|
||||
s.Log.Info().Msgf("Adding new location to database: ", loc)
|
||||
err := s.Database.Save(&loc)
|
||||
if err != nil {
|
||||
s.Log.Error().Msgf("Unable to add new location to database: %s error: %s ", loc, err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddRoom adds a room to a location
|
||||
func (s *Server) AddRoom(room Room) error {
|
||||
s.Log.Info().Msgf("Adding new room to location: %s", room)
|
||||
err := s.Database.Save(&room)
|
||||
if err != nil {
|
||||
s.Log.Error().Msgf("Unable to add new room to database: %s error: %s", room, err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddItem adds an item to a room
|
||||
func (s *Server) AddItem(item Item) error {
|
||||
s.Log.Info().Msgf("Adding new item to room: ", item)
|
||||
err := s.Database.Save(&item)
|
||||
if err != nil {
|
||||
s.Log.Error().Msgf("Unable to add new item to room: %s error: %s", item, err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetLocations fetches all of the locations from the database
|
||||
func (s *Server) GetLocations() (locations []Location, err error) {
|
||||
err = s.Database.All(&locations)
|
||||
if err != nil {
|
||||
s.Log.Error().Msgf("Unable to fetch all locations: %s", err)
|
||||
return nil, err
|
||||
}
|
||||
return locations, nil
|
||||
}
|
||||
|
||||
// GetRoomsAtLocation fetches all the rooms assigned to a location by location ID
|
||||
func (s *Server) GetRoomsAtLocation(locID int) error {
|
||||
var rooms []Room
|
||||
err := s.Database.Find("Location", locID, &rooms)
|
||||
if err != nil {
|
||||
s.Log.Error().Msgf("Unable to fetch all rooms at location: %s", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetItemsInRoom fetches all of the items assigned to a room
|
||||
func (s *Server) GetItemsInRoom(roomID int) error {
|
||||
var items []Item
|
||||
err := s.Database.Find("Room", roomID, &items)
|
||||
if err != nil {
|
||||
s.Log.Error().Msgf("Unable to fetch all rooms at location: %s", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user