Files
goInventorize/database.go

304 lines
9.1 KiB
Go

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
Address string
SquareFeet int
Latitude string
Longitude string
DatePurchased string
PurchasePrice string
CurrentValue string
}
// Room is a containerized area at a location
type Room struct {
ID int `storm:"id,increment,index"`
Name string
Description string
CoverPhoto string // A "cover photo" for the room
Photos []string // A list of additional photos for the room
Paint string // Details about the paint
Notes string
Cabinets []Cabinet
Items []Item
SquareFeet int
LocationID int //Which location room is assigned to
LocationName string // Location name room belongs to
}
// Cabinet is a containerized area in a room
type Cabinet struct {
ID int `storm:"id, increment"`
RoomID int // Room ID that the cabinet is assigned to
RoomName string // Room name cabinet belongs to
CoverPhoto string // A "Cover photo" for this item
Items []Item // List of items in the cabinet
Name string
Notes string
}
// Item is contained inside a room or cabinet
type Item struct {
ID int `storm:"id, increment"`
LocType string // Can be Room or Cabinet to show where it is stored
RoomID int // Room ID that item is assigned to (can also be in a cabinet)
RoomName string // Room name that item is assigned to
CabinetID int //The cabinet id that an item is assigned to
CabinetName string // The name of the cabinet it belongs to
CoverPhoto string // A "cover photo" for the item
Photos []string // A list of additional photos for the item
Count int // If item has duplicates
Name string
Product string
Serial string
Warranty string
Notes string
Files []string //filepath to any files relevant to this item
}
// Location Routes
//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
}
// DeleteLocation deletes a location by ID
func (s *Server) DeleteLocation(loc Location) error {
s.Log.Info().Msgf("Deleting location from database: ", loc)
err := s.Database.DeleteStruct(&loc)
if err != nil {
s.Log.Error().Msgf("Unable to delete location from database: %s error: %s ", loc, 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
}
// GetLocation fetches a single location
func (s *Server) GetLocation(locID int) (location Location, err error) {
err = s.Database.One("ID", locID, &location)
if err != nil {
s.Log.Error().Msgf("Unable to fetch all rooms at location: %s", err)
return location, err
}
return location, nil
}
// Room Routes
// 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
}
// DeleteRoom deletes a room
func (s *Server) DeleteRoom(room Room) error {
s.Log.Info().Msgf("Deleting room from database: %s", room)
err := s.Database.DeleteStruct(&room)
if err != nil {
s.Log.Error().Msgf("Unable to delete location from database: %s error: %s ", room, err)
return err
}
return nil
}
// GetAllRooms gets all of the rooms
func (s *Server) GetAllRooms() (rooms []Room, err error) {
s.Log.Info().Msg("Getting all Rooms")
err = s.Database.All(&rooms)
if err != nil {
s.Log.Error().Msgf("Unable to fetch all room with error: %s", err)
return rooms, err
}
return rooms, nil
}
// GetRoom gets a room based on id
func (s *Server) GetRoom(roomID int) (room Room, err error) {
s.Log.Info().Msgf("Fetching room with id: %d", roomID)
err = s.Database.One("ID", roomID, &room)
if err != nil {
s.Log.Error().Msgf("Unable to fetch room with id: %d with error: %s", roomID, err)
return room, err
}
return room, nil
}
// GetRoomsAtLocation fetches all the rooms assigned to a location by location ID
func (s *Server) GetRoomsAtLocation(locID int) (rooms []Room, err error) {
err = s.Database.Find("LocationID", locID, &rooms)
if err != nil {
s.Log.Error().Msgf("Unable to fetch all rooms at location: %s", err)
return nil, err
}
return rooms, nil
}
// Cabinet ROUTES
// AddCabinet adds a new cabinet
func (s *Server) AddCabinet(cabinet Cabinet) error {
s.Log.Info().Msgf("Adding new cabinet to room: %s", cabinet)
err := s.Database.Save(&cabinet)
if err != nil {
s.Log.Error().Msgf("Unable to add new cabinet to database: %s error: %s", cabinet, err)
return err
}
return nil
}
// DeleteCabinet deletes a cabinet
func (s *Server) DeleteCabinet(cabinet Cabinet) error {
s.Log.Info().Msgf("Deleting cabinet from database: %s", cabinet)
err := s.Database.DeleteStruct(&cabinet)
if err != nil {
s.Log.Error().Msgf("Unable to delete room from database: %s error: %s ", cabinet, err)
return err
}
return nil
}
// GetAllCabinets gets all of the cabinets
func (s *Server) GetAllCabinets() (cabinets []Cabinet, err error) {
s.Log.Info().Msg("Getting all Cabinets")
err = s.Database.All(&cabinets)
if err != nil {
s.Log.Error().Msgf("Unable to fetch all cabinet with error: %s", err)
return cabinets, err
}
return cabinets, nil
}
// GetCabinet gets a cabinet based on id
func (s *Server) GetCabinet(cabinetID int) (cabinet Cabinet, err error) {
s.Log.Info().Msgf("Fetching cabinet with id: %d", cabinetID)
err = s.Database.One("ID", cabinetID, &cabinet)
if err != nil {
s.Log.Error().Msgf("Unable to fetch cabinet with id: %d with error: %s", cabinetID, err)
return cabinet, err
}
return cabinet, nil
}
// GetCabinetsInRoom fetches all of the cabinets assigned to a room
func (s *Server) GetCabinetsAtRoom(roomID int) (cabinets []Cabinet, err error) {
err = s.Database.Find("Room", roomID, &cabinets)
if err != nil {
s.Log.Error().Msgf("Unable to fetch all cabinets in room: %s", err)
return nil, err
}
return cabinets, nil
}
// ITEM ROUTES
// GetItem gets an item by id
func (s *Server) GetItem(itemID int) (item Item, err error) {
s.Log.Info().Msgf("Fetching item with id: %d", itemID)
err = s.Database.One("ID", itemID, &item)
if err != nil {
s.Log.Error().Msgf("Unable to fetch item with id: %d with error: %s", itemID, err)
return item, err
}
return item, nil
}
// GetAllItems gets all of the items
func (s *Server) GetAllItems() (items []Item, err error) {
s.Log.Info().Msg("Getting all Items")
err = s.Database.All(&items)
if err != nil {
s.Log.Error().Msgf("Unable to fetch all items with error: %s", err)
return items, err
}
return items, nil
}
// GetItemsInRoom fetches all of the items assigned to a room
func (s *Server) GetItemsInRoom(roomID int) (items []Item, err error) {
err = s.Database.Find("Room", roomID, &items)
if err != nil {
s.Log.Error().Msgf("Unable to fetch all items in room: %s", err)
return nil, err
}
return items, nil
}
// GetItemsInRoom fetches all of the items assigned to a room
func (s *Server) GetItemsInCabinet(cabinetID int) (items []Item, err error) {
err = s.Database.Find("Cabinet", cabinetID, &items)
if err != nil {
s.Log.Error().Msgf("Unable to fetch all items in cabinet: %s", err)
return nil, err
}
return items, 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
}
// DeleteItem deletes an item
func (s *Server) DeleteItem(item Item) error {
s.Log.Info().Msgf("Deleting Item from database: %s", item)
err := s.Database.DeleteStruct(&item)
if err != nil {
s.Log.Error().Msgf("Unable to delete location from database: %s error: %s ", item, err)
return err
}
return nil
}