fixing add item route, working on finishing cabinet routes
This commit is contained in:
103
database.go
103
database.go
@@ -26,8 +26,8 @@ type Location struct {
|
||||
|
||||
// Room is a containerized area at a location
|
||||
type Room struct {
|
||||
ID int `storm:"id,increment,index"`
|
||||
Name string `storm:"unique"`
|
||||
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
|
||||
@@ -45,24 +45,30 @@ type Room struct {
|
||||
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"`
|
||||
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 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
|
||||
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
|
||||
@@ -174,8 +180,54 @@ func (s *Server) GetRoomsAtLocation(locID int) (rooms []Room, err error) {
|
||||
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) GetCabinetsInRoom(roomID int) (cabinets []Cabinet, err error) {
|
||||
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)
|
||||
@@ -186,12 +238,23 @@ func (s *Server) GetCabinetsInRoom(roomID int) (cabinets []Cabinet, err error) {
|
||||
|
||||
// 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 room with error: %s", err)
|
||||
s.Log.Error().Msgf("Unable to fetch all items with error: %s", err)
|
||||
return items, err
|
||||
}
|
||||
return items, nil
|
||||
@@ -207,6 +270,16 @@ func (s *Server) GetItemsInRoom(roomID int) (items []Item, err error) {
|
||||
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)
|
||||
|
Reference in New Issue
Block a user