react-router frontend, fixing backend issues, adding /api
This commit is contained in:
102
database.go
102
database.go
@@ -28,12 +28,22 @@ type Room struct {
|
||||
Notes string
|
||||
}
|
||||
|
||||
// Item is contained inside a room
|
||||
// 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
|
||||
CoverPhoto string // A "Cover photo" for this item
|
||||
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 room
|
||||
Count int // If item has duplicates
|
||||
Name string
|
||||
Product string
|
||||
Serial string
|
||||
@@ -61,23 +71,12 @@ func (s *Server) AddLocation(loc Location) error {
|
||||
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)
|
||||
// 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 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)
|
||||
s.Log.Error().Msgf("Unable to delete location from database: %s error: %s ", loc, err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -93,24 +92,75 @@ func (s *Server) GetLocations() (locations []Location, err error) {
|
||||
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)
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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)
|
||||
// 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 rooms at location: %s", err)
|
||||
s.Log.Error().Msgf("Unable to fetch all room with error: %s", err)
|
||||
return rooms, err
|
||||
}
|
||||
return rooms, 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
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// GetCabinetsInRoom fetches all of the cabinets assigned to a room
|
||||
func (s *Server) GetCabinetsInRoom(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
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
Reference in New Issue
Block a user