fixing add item route, working on finishing cabinet routes
This commit is contained in:
@@ -902,3 +902,49 @@
|
|||||||
{"level":"info","time":"2022-03-30T22:57:01-04:00","message":"Configuration loaded successfully..."}
|
{"level":"info","time":"2022-03-30T22:57:01-04:00","message":"Configuration loaded successfully..."}
|
||||||
{"level":"debug","time":"2022-03-30T22:57:01-04:00","message":"{Timezone:America/New_York Server:{Port:3500 LocationPhotoDir:./app/photos/locations/} Logger:{Level:debug LoggingFile:./app/log/goInventorize.log} Authentication:{BasicAuth:false UserName:admin Password:password} Development:false}"}
|
{"level":"debug","time":"2022-03-30T22:57:01-04:00","message":"{Timezone:America/New_York Server:{Port:3500 LocationPhotoDir:./app/photos/locations/} Logger:{Level:debug LoggingFile:./app/log/goInventorize.log} Authentication:{BasicAuth:false UserName:admin Password:password} Development:false}"}
|
||||||
{"level":"info","time":"2022-03-30T22:57:01-04:00","message":"Database and Config loaded, starting webserver..."}
|
{"level":"info","time":"2022-03-30T22:57:01-04:00","message":"Database and Config loaded, starting webserver..."}
|
||||||
|
{"level":"info","time":"2022-03-31T20:49:27-04:00","message":"Configuration loaded successfully..."}
|
||||||
|
{"level":"debug","time":"2022-03-31T20:49:27-04:00","message":"{Timezone:America/New_York Server:{Port:3500 LocationPhotoDir:./app/photos/locations/} Logger:{Level:debug LoggingFile:./app/log/goInventorize.log} Authentication:{BasicAuth:false UserName:admin Password:password} Development:false}"}
|
||||||
|
{"level":"info","time":"2022-03-31T20:49:27-04:00","message":"Database and Config loaded, starting webserver..."}
|
||||||
|
{"level":"info","time":"2022-03-31T20:51:46-04:00","message":"Configuration loaded successfully..."}
|
||||||
|
{"level":"debug","time":"2022-03-31T20:51:46-04:00","message":"{Timezone:America/New_York Server:{Port:3500 LocationPhotoDir:./app/photos/locations/} Logger:{Level:debug LoggingFile:./app/log/goInventorize.log} Authentication:{BasicAuth:false UserName:admin Password:password} Development:false}"}
|
||||||
|
{"level":"info","time":"2022-03-31T20:51:46-04:00","message":"Database and Config loaded, starting webserver..."}
|
||||||
|
{"level":"info","time":"2022-03-31T20:52:46-04:00","message":"Configuration loaded successfully..."}
|
||||||
|
{"level":"debug","time":"2022-03-31T20:52:46-04:00","message":"{Timezone:America/New_York Server:{Port:3500 LocationPhotoDir:./app/photos/locations/} Logger:{Level:debug LoggingFile:./app/log/goInventorize.log} Authentication:{BasicAuth:false UserName:admin Password:password} Development:false}"}
|
||||||
|
{"level":"info","time":"2022-03-31T20:52:46-04:00","message":"Database and Config loaded, starting webserver..."}
|
||||||
|
{"level":"info","time":"2022-03-31T20:54:33-04:00","message":"Configuration loaded successfully..."}
|
||||||
|
{"level":"debug","time":"2022-03-31T20:54:33-04:00","message":"{Timezone:America/New_York Server:{Port:3500 LocationPhotoDir:./app/photos/locations/} Logger:{Level:debug LoggingFile:./app/log/goInventorize.log} Authentication:{BasicAuth:false UserName:admin Password:password} Development:false}"}
|
||||||
|
{"level":"info","time":"2022-03-31T20:54:33-04:00","message":"Database and Config loaded, starting webserver..."}
|
||||||
|
{"level":"info","time":"2022-03-31T20:59:41-04:00","message":"Configuration loaded successfully..."}
|
||||||
|
{"level":"debug","time":"2022-03-31T20:59:41-04:00","message":"{Timezone:America/New_York Server:{Port:3500 LocationPhotoDir:./app/photos/locations/} Logger:{Level:debug LoggingFile:./app/log/goInventorize.log} Authentication:{BasicAuth:false UserName:admin Password:password} Development:false}"}
|
||||||
|
{"level":"info","time":"2022-03-31T20:59:41-04:00","message":"Database and Config loaded, starting webserver..."}
|
||||||
|
{"level":"info","time":"2022-03-31T21:00:13-04:00","message":"Configuration loaded successfully..."}
|
||||||
|
{"level":"debug","time":"2022-03-31T21:00:13-04:00","message":"{Timezone:America/New_York Server:{Port:3500 LocationPhotoDir:./app/photos/locations/} Logger:{Level:debug LoggingFile:./app/log/goInventorize.log} Authentication:{BasicAuth:false UserName:admin Password:password} Development:false}"}
|
||||||
|
{"level":"info","time":"2022-03-31T21:00:13-04:00","message":"Database and Config loaded, starting webserver..."}
|
||||||
|
{"level":"info","time":"2022-03-31T21:01:31-04:00","message":"Configuration loaded successfully..."}
|
||||||
|
{"level":"debug","time":"2022-03-31T21:01:31-04:00","message":"{Timezone:America/New_York Server:{Port:3500 LocationPhotoDir:./app/photos/locations/} Logger:{Level:debug LoggingFile:./app/log/goInventorize.log} Authentication:{BasicAuth:false UserName:admin Password:password} Development:false}"}
|
||||||
|
{"level":"info","time":"2022-03-31T21:01:31-04:00","message":"Database and Config loaded, starting webserver..."}
|
||||||
|
{"level":"info","time":"2022-03-31T21:09:03-04:00","message":"Configuration loaded successfully..."}
|
||||||
|
{"level":"debug","time":"2022-03-31T21:09:03-04:00","message":"{Timezone:America/New_York Server:{Port:3500 LocationPhotoDir:./app/photos/locations/} Logger:{Level:debug LoggingFile:./app/log/goInventorize.log} Authentication:{BasicAuth:false UserName:admin Password:password} Development:false}"}
|
||||||
|
{"level":"info","time":"2022-03-31T21:09:03-04:00","message":"Database and Config loaded, starting webserver..."}
|
||||||
|
{"level":"info","time":"2022-03-31T21:21:22-04:00","message":"Configuration loaded successfully..."}
|
||||||
|
{"level":"debug","time":"2022-03-31T21:21:22-04:00","message":"{Timezone:America/New_York Server:{Port:3500 LocationPhotoDir:./app/photos/locations/} Logger:{Level:debug LoggingFile:./app/log/goInventorize.log} Authentication:{BasicAuth:false UserName:admin Password:password} Development:false}"}
|
||||||
|
{"level":"info","time":"2022-03-31T21:21:22-04:00","message":"Database and Config loaded, starting webserver..."}
|
||||||
|
{"level":"info","time":"2022-03-31T21:21:42-04:00","message":"Configuration loaded successfully..."}
|
||||||
|
{"level":"debug","time":"2022-03-31T21:21:42-04:00","message":"{Timezone:America/New_York Server:{Port:3500 LocationPhotoDir:./app/photos/locations/} Logger:{Level:debug LoggingFile:./app/log/goInventorize.log} Authentication:{BasicAuth:false UserName:admin Password:password} Development:false}"}
|
||||||
|
{"level":"info","time":"2022-03-31T21:21:42-04:00","message":"Database and Config loaded, starting webserver..."}
|
||||||
|
{"level":"info","time":"2022-03-31T21:21:54-04:00","message":"Configuration loaded successfully..."}
|
||||||
|
{"level":"debug","time":"2022-03-31T21:21:54-04:00","message":"{Timezone:America/New_York Server:{Port:3500 LocationPhotoDir:./app/photos/locations/} Logger:{Level:debug LoggingFile:./app/log/goInventorize.log} Authentication:{BasicAuth:false UserName:admin Password:password} Development:false}"}
|
||||||
|
{"level":"info","time":"2022-03-31T21:21:54-04:00","message":"Database and Config loaded, starting webserver..."}
|
||||||
|
{"level":"info","time":"2022-04-01T21:46:46-04:00","message":"Configuration loaded successfully..."}
|
||||||
|
{"level":"debug","time":"2022-04-01T21:46:46-04:00","message":"{Timezone:America/New_York Server:{Port:3500 LocationPhotoDir:./app/photos/locations/} Logger:{Level:debug LoggingFile:./app/log/goInventorize.log} Authentication:{BasicAuth:false UserName:admin Password:password} Development:false}"}
|
||||||
|
{"level":"info","time":"2022-04-01T21:46:46-04:00","message":"Database and Config loaded, starting webserver..."}
|
||||||
|
{"logtype":"webserver", "pid":"29344", "requestid":"", "status":"200", "method":"GET", "path":"/api/config"}
|
||||||
|
{"level":"debug","time":"2022-04-01T21:47:48-04:00","message":"Returning Locations: [{ID:1 Name:Location4 Description: CoverPhoto:Location4_cover.png Photos:[Location4_0.png Location4_1.png] Rooms:[{ID:0 Name:Kitchen Description: CoverPhoto:Location4/Kitchen/Kitchen_cover.png Photos:[Location4/Kitchen/Kitchen_0.png] Paint: Notes:Notes For My Room! Cabinets:[] Items:[] SquareFeet:0 LocationID:1 LocationName:Location4}] Notes:Notes for my location! Address: SquareFeet:0 Latitude: Longitude: DatePurchased: PurchasePrice: CurrentValue:} {ID:2 Name:Location6 Description: CoverPhoto:Location6_cover.png Photos:[Location6_0.png Location6_1.png] Rooms:[] Notes:Notes for my location!6 Address: SquareFeet:0 Latitude: Longitude: DatePurchased: PurchasePrice: CurrentValue:} {ID:3 Name:Location10 Description: CoverPhoto:Location10_cover.png Photos:[] Rooms:[] Notes:Notes for my location!10 Address: SquareFeet:0 Latitude: Longitude: DatePurchased: PurchasePrice: CurrentValue:}]"}
|
||||||
|
{"logtype":"webserver", "pid":"29344", "requestid":"", "status":"200", "method":"GET", "path":"/api/locations"}
|
||||||
|
{"logtype":"webserver", "pid":"29344", "requestid":"", "status":"200", "method":"GET", "path":"/photos/locations/Location6/Location6_cover.png"}
|
||||||
|
{"logtype":"webserver", "pid":"29344", "requestid":"", "status":"200", "method":"GET", "path":"/photos/locations/Location10/Location10_cover.png"}
|
||||||
|
{"logtype":"webserver", "pid":"29344", "requestid":"", "status":"200", "method":"GET", "path":"/api/rooms/1"}
|
||||||
|
{"logtype":"webserver", "pid":"29344", "requestid":"", "status":"304", "method":"GET", "path":"/photos/locations/Location4/Kitchen/Kitchen_cover.png"}
|
||||||
|
{"level":"info","time":"2022-04-01T21:57:09-04:00","message":"Configuration loaded successfully..."}
|
||||||
|
{"level":"debug","time":"2022-04-01T21:57:09-04:00","message":"{Timezone:America/New_York Server:{Port:3500 LocationPhotoDir:./app/photos/locations/} Logger:{Level:debug LoggingFile:./app/log/goInventorize.log} Authentication:{BasicAuth:false UserName:admin Password:password} Development:false}"}
|
||||||
|
{"level":"info","time":"2022-04-01T21:57:09-04:00","message":"Database and Config loaded, starting webserver..."}
|
||||||
|
103
database.go
103
database.go
@@ -26,8 +26,8 @@ type Location struct {
|
|||||||
|
|
||||||
// Room is a containerized area at a location
|
// Room is a containerized area at a location
|
||||||
type Room struct {
|
type Room struct {
|
||||||
ID int `storm:"id,increment,index"`
|
ID int `storm:"id,increment,index"`
|
||||||
Name string `storm:"unique"`
|
Name string
|
||||||
Description string
|
Description string
|
||||||
CoverPhoto string // A "cover photo" for the room
|
CoverPhoto string // A "cover photo" for the room
|
||||||
Photos []string // A list of additional photos for the room
|
Photos []string // A list of additional photos for the room
|
||||||
@@ -45,24 +45,30 @@ type Room struct {
|
|||||||
type Cabinet struct {
|
type Cabinet struct {
|
||||||
ID int `storm:"id, increment"`
|
ID int `storm:"id, increment"`
|
||||||
RoomID int // Room ID that the cabinet is assigned to
|
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
|
CoverPhoto string // A "Cover photo" for this item
|
||||||
|
Items []Item // List of items in the cabinet
|
||||||
Name string
|
Name string
|
||||||
Notes string
|
Notes string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Item is contained inside a room or cabinet
|
// Item is contained inside a room or cabinet
|
||||||
type Item struct {
|
type Item struct {
|
||||||
ID int `storm:"id, increment"`
|
ID int `storm:"id, increment"`
|
||||||
RoomID int // Room ID that item is assigned to
|
LocType string // Can be Room or Cabinet to show where it is stored
|
||||||
CoverPhoto string // A "cover photo" for the item
|
RoomID int // Room ID that item is assigned to (can also be in a cabinet)
|
||||||
Photos []string // A list of additional photos for the item
|
RoomName string // Room name that item is assigned to
|
||||||
Count int // If item has duplicates
|
CabinetID int //The cabinet id that an item is assigned to
|
||||||
Name string
|
CabinetName string // The name of the cabinet it belongs to
|
||||||
Product string
|
CoverPhoto string // A "cover photo" for the item
|
||||||
Serial string
|
Photos []string // A list of additional photos for the item
|
||||||
Warranty string
|
Count int // If item has duplicates
|
||||||
Notes string
|
Name string
|
||||||
Files []string //filepath to any files relevant to this item
|
Product string
|
||||||
|
Serial string
|
||||||
|
Warranty string
|
||||||
|
Notes string
|
||||||
|
Files []string //filepath to any files relevant to this item
|
||||||
}
|
}
|
||||||
|
|
||||||
// Location Routes
|
// Location Routes
|
||||||
@@ -174,8 +180,54 @@ func (s *Server) GetRoomsAtLocation(locID int) (rooms []Room, err error) {
|
|||||||
return rooms, nil
|
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
|
// 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)
|
err = s.Database.Find("Room", roomID, &cabinets)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Log.Error().Msgf("Unable to fetch all cabinets in room: %s", err)
|
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
|
// 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
|
// GetAllItems gets all of the items
|
||||||
func (s *Server) GetAllItems() (items []Item, err error) {
|
func (s *Server) GetAllItems() (items []Item, err error) {
|
||||||
s.Log.Info().Msg("Getting all Items")
|
s.Log.Info().Msg("Getting all Items")
|
||||||
err = s.Database.All(&items)
|
err = s.Database.All(&items)
|
||||||
if err != nil {
|
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, err
|
||||||
}
|
}
|
||||||
return items, nil
|
return items, nil
|
||||||
@@ -207,6 +270,16 @@ func (s *Server) GetItemsInRoom(roomID int) (items []Item, err error) {
|
|||||||
return items, nil
|
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
|
// AddItem adds an item to a room
|
||||||
func (s *Server) AddItem(item Item) error {
|
func (s *Server) AddItem(item Item) error {
|
||||||
s.Log.Info().Msgf("Adding new item to room: ", item)
|
s.Log.Info().Msgf("Adding new item to room: ", item)
|
||||||
|
150
handlers_cabinet.go
Normal file
150
handlers_cabinet.go
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DeleteCabinetHandler deletes a cabinet based on an ID
|
||||||
|
func (s *Server) DeleteCabinetHandler(c *fiber.Ctx) error {
|
||||||
|
cabinetID, err := c.ParamsInt("cabinetID")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cabinet, err := s.GetCabinet(cabinetID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Err(err).Msgf("Unable to fetch cabinet with id: %d", cabinetID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = s.DeleteCabinet(cabinet)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Err(err).Msgf("Unable to delete cabinet with id: %d", cabinetID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.Status(fiber.StatusOK).SendString(fmt.Sprintf("Cabinet Deleted! %d", cabinetID))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCabinetHandler returns a single cabinet from an ID
|
||||||
|
func (s *Server) GetCabinetHandler(c *fiber.Ctx) error {
|
||||||
|
cabinetID, err := c.ParamsInt("cabinetID")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cabinet, err := s.GetCabinet(cabinetID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Err(err).Msgf("Unable to fetch cabinet with id: %d", cabinetID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.Status(fiber.StatusOK).JSON(cabinet)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddNewCabinetHandler adds a new cabinet
|
||||||
|
func (s *Server) AddNewCabinetHandler(c *fiber.Ctx) error {
|
||||||
|
roomID, err := c.ParamsInt("roomID")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
form, err := c.MultipartForm()
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Err(err).Msgf("Unable to fetch cabinet details: ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cabinetName := form.Value["Name"][0]
|
||||||
|
fmt.Println("COverphoto: ", form.File["CoverPhoto"])
|
||||||
|
cabinetPhoto := form.File["CoverPhoto"][0]
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Err(err).Msgf("Unable to get CoverPhoto for Cabinet: ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch the location information
|
||||||
|
room, err := s.GetRoom(roomID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Err(err).Msgf("Unable to fetch room by id: %d with error: %s", roomID, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
loc, err := s.GetLocation(room.LocationID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Err(err).Msgf("Unable to fetch location by id: %d with error: %s", room.LocationID, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// setting the path prepend for the photos
|
||||||
|
urlPrepend := loc.Name + "/" + room.Name + "/" + cabinetName + "/"
|
||||||
|
pathPrepend := s.Config.Server.LocationPhotoDir + "/" + urlPrepend
|
||||||
|
|
||||||
|
// Create the directory for this cabinet to store the photos
|
||||||
|
err = os.MkdirAll(filepath.ToSlash(pathPrepend), 0755)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// create files on server for all uploads
|
||||||
|
cabinetNotes := form.Value["Notes"][0]
|
||||||
|
|
||||||
|
// saving the uploaded image to our images dir
|
||||||
|
coverImagePath := filepath.ToSlash(pathPrepend + cabinetName + "_cover" + filepath.Ext(cabinetPhoto.Filename))
|
||||||
|
// Creating the URL to retrieve photo
|
||||||
|
coverImageURL := urlPrepend + cabinetName + "_cover" + filepath.Ext(cabinetPhoto.Filename)
|
||||||
|
src, err := cabinetPhoto.Open()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer src.Close()
|
||||||
|
coverImageData, err := io.ReadAll(src)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Err(err).Msgf("Unable to read cover photo file: ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = os.WriteFile(coverImagePath, coverImageData, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
newCabinet := Cabinet{
|
||||||
|
Name: cabinetName,
|
||||||
|
CoverPhoto: coverImageURL,
|
||||||
|
Notes: cabinetNotes,
|
||||||
|
RoomID: roomID,
|
||||||
|
RoomName: loc.Name,
|
||||||
|
}
|
||||||
|
err = s.AddCabinet(newCabinet)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Update our room in the db to add the new cabinet as attached to the ID
|
||||||
|
cabinetList := room.Cabinets
|
||||||
|
cabinetList = append(cabinetList, newCabinet)
|
||||||
|
|
||||||
|
err = s.Database.UpdateField(&Room{ID: roomID}, "Cabinets", cabinetList)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
s.Log.Debug().Msgf("Adding new Cabinet: %v", newCabinet)
|
||||||
|
return c.Status(fiber.StatusOK).SendString(fmt.Sprintf("New Cabinet Created! %s", newCabinet.Name))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAllCabinetsHandler gets all of the cabinets in the db
|
||||||
|
func (s *Server) GetAllCabinetsHandler(c *fiber.Ctx) error {
|
||||||
|
cabinets, err := s.GetAllCabinets()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.Status(fiber.StatusOK).JSON(cabinets)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCabinetsAtRoomHandler gets cabinets at a location
|
||||||
|
func (s *Server) GetCabinetsAtRoomHandler(c *fiber.Ctx) error {
|
||||||
|
locID, err := c.ParamsInt("locID")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cabinets, err := s.GetCabinetsAtRoom(locID)
|
||||||
|
fmt.Println("ROOMS: ", cabinets)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.Status(fiber.StatusOK).JSON(cabinets)
|
||||||
|
}
|
169
handlers_item.go
169
handlers_item.go
@@ -10,6 +10,16 @@ import (
|
|||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetAllItemsHandler gets all items
|
||||||
|
func (s *Server) GetAllItemsHandler(c *fiber.Ctx) error {
|
||||||
|
items, err := s.GetAllItems()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.Status(fiber.StatusOK).JSON(items)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAllItemsAtRoomHandler gets all items in a room
|
||||||
func (s *Server) GetAllItemsAtRoomHandler(c *fiber.Ctx) error {
|
func (s *Server) GetAllItemsAtRoomHandler(c *fiber.Ctx) error {
|
||||||
roomID, err := c.ParamsInt("roomID")
|
roomID, err := c.ParamsInt("roomID")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -21,53 +31,102 @@ func (s *Server) GetAllItemsAtRoomHandler(c *fiber.Ctx) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return c.Status(fiber.StatusOK).JSON(items)
|
return c.Status(fiber.StatusOK).JSON(items)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAllItemsAtCabinetHandler gets all items in a cabinet
|
||||||
|
func (s *Server) GetAllItemsAtCabinetHandler(c *fiber.Ctx) error {
|
||||||
|
cabinetID, err := c.ParamsInt("cabinetID")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
items, err := s.GetItemsInCabinet(cabinetID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.Status(fiber.StatusOK).JSON(items)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddNewItemHandler adds a new room
|
// AddNewItemHandler adds a new room
|
||||||
func (s *Server) AddNewItemHandler(c *fiber.Ctx) error {
|
func (s *Server) AddNewItemHandler(c *fiber.Ctx) error {
|
||||||
locID, err := c.ParamsInt("locID")
|
storageType := "room"
|
||||||
|
cabinetID := 0
|
||||||
|
roomID, err := c.ParamsInt("roomID")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if roomID == 0 {
|
||||||
|
storageType = "cabinet"
|
||||||
|
cabinetID, err = c.ParamsInt("cabinetID")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
form, err := c.MultipartForm()
|
form, err := c.MultipartForm()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Log.Err(err).Msgf("Unable to fetch rooms: ", err)
|
s.Log.Err(err).Msgf("Unable to fetch item form details: ", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
roomName := form.Value["Name"][0]
|
itemName := form.Value["Name"][0]
|
||||||
fmt.Println("COverphoto: ", form.File["CoverPhoto"])
|
fmt.Println("COverphoto: ", form.File["CoverPhoto"])
|
||||||
roomPhoto := form.File["CoverPhoto"][0]
|
itemPhoto := form.File["CoverPhoto"][0]
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// s.Log.Err(err).Msgf("Unable to get CoverPhoto for Room: ", err)
|
// s.Log.Err(err).Msgf("Unable to get CoverPhoto for Room: ", err)
|
||||||
// return err
|
// return err
|
||||||
// }
|
// }
|
||||||
roomAdditionalPhotos := form.File["AdditionalPhotos"]
|
itemAdditionalPhotos := form.File["AdditionalPhotos"]
|
||||||
// Fetch the location information
|
|
||||||
loc, err := s.GetLocation(locID)
|
room := Room{}
|
||||||
|
cabinet := Cabinet{}
|
||||||
|
|
||||||
|
if storageType == "room" {
|
||||||
|
room, err = s.GetRoom(roomID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Err(err).Msgf("Unable to fetch room by id: %d with error: %s", roomID, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cabinet, err = s.GetCabinet(cabinetID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Err(err).Msgf("Unable to fetch room by id: %d with error: %s", roomID, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
room, err = s.GetRoom(cabinet.RoomID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Err(err).Msgf("Unable to fetch room by id: %d with error: %s", cabinet.RoomID, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
loc, err := s.GetLocation(room.LocationID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Log.Err(err).Msgf("Unable to fetch location by id: %d with error: %s", locID, err)
|
s.Log.Err(err).Msgf("Unable to fetch location by id: %d with error: %s", room.LocationID, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// setting the path prepend for the photos
|
|
||||||
urlPrepend := loc.Name + "/" + roomName + "/"
|
|
||||||
pathPrepend := s.Config.Server.LocationPhotoDir + "/" + urlPrepend
|
|
||||||
|
|
||||||
// Create the directory for this room to store the photos
|
urlPrepend := ""
|
||||||
err = os.MkdirAll(filepath.ToSlash(s.Config.Server.LocationPhotoDir+"/"+loc.Name+"/"+roomName), 0755)
|
|
||||||
|
// setting the path prepend for the photos
|
||||||
|
if storageType == "room" {
|
||||||
|
urlPrepend = loc.Name + "/" + room.Name + "/" + itemName
|
||||||
|
} else {
|
||||||
|
urlPrepend = loc.Name + "/" + room.Name + "/" + cabinet.Name + "/" + itemName
|
||||||
|
}
|
||||||
|
pathPrepend := s.Config.Server.LocationPhotoDir + "/" + urlPrepend + "/"
|
||||||
|
|
||||||
|
// Create the directory for this item to store the photos
|
||||||
|
err = os.MkdirAll(filepath.ToSlash(pathPrepend), 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var photoURLsList []string
|
var photoURLsList []string
|
||||||
for i, photo := range roomAdditionalPhotos {
|
for i, photo := range itemAdditionalPhotos {
|
||||||
src, err := photo.Open()
|
src, err := photo.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer src.Close()
|
defer src.Close()
|
||||||
|
|
||||||
photoPath := filepath.ToSlash(pathPrepend + roomName + "_" + strconv.Itoa(i) + filepath.Ext(photo.Filename))
|
photoPath := filepath.ToSlash(pathPrepend + itemName + "_" + strconv.Itoa(i) + filepath.Ext(photo.Filename))
|
||||||
photoURL := urlPrepend + roomName + "_" + strconv.Itoa(i) + filepath.Ext(photo.Filename)
|
photoURL := urlPrepend + itemName + "_" + strconv.Itoa(i) + filepath.Ext(photo.Filename)
|
||||||
photoURLsList = append(photoURLsList, photoURL)
|
photoURLsList = append(photoURLsList, photoURL)
|
||||||
photoData, err := io.ReadAll(src)
|
photoData, err := io.ReadAll(src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -76,13 +135,13 @@ func (s *Server) AddNewItemHandler(c *fiber.Ctx) error {
|
|||||||
os.WriteFile(photoPath, photoData, 0644)
|
os.WriteFile(photoPath, photoData, 0644)
|
||||||
}
|
}
|
||||||
// create files on server for all uploads
|
// create files on server for all uploads
|
||||||
roomNotes := form.Value["Notes"][0]
|
itemNotes := form.Value["Notes"][0]
|
||||||
|
|
||||||
// saving the uploaded image to our images dir
|
// saving the uploaded image to our images dir
|
||||||
coverImagePath := filepath.ToSlash(pathPrepend + roomName + "_cover" + filepath.Ext(roomPhoto.Filename))
|
coverImagePath := filepath.ToSlash(pathPrepend + itemName + "_cover" + filepath.Ext(itemPhoto.Filename))
|
||||||
// Creating the URL to retrieve photo
|
// Creating the URL to retrieve photo
|
||||||
coverImageURL := urlPrepend + roomName + "_cover" + filepath.Ext(roomPhoto.Filename)
|
coverImageURL := urlPrepend + itemName + "_cover" + filepath.Ext(itemPhoto.Filename)
|
||||||
src, err := roomPhoto.Open()
|
src, err := itemPhoto.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -96,30 +155,56 @@ func (s *Server) AddNewItemHandler(c *fiber.Ctx) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
newRoom := Room{
|
|
||||||
Name: roomName,
|
|
||||||
CoverPhoto: coverImageURL,
|
|
||||||
Photos: photoURLsList,
|
|
||||||
Notes: roomNotes,
|
|
||||||
LocationID: locID,
|
|
||||||
LocationName: loc.Name,
|
|
||||||
}
|
|
||||||
err = s.AddRoom(newRoom)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// Update our location in the db to add the new room as attached to the ID
|
|
||||||
loc, err = s.GetLocation(newRoom.LocationID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
roomList := loc.Rooms
|
|
||||||
roomList = append(roomList, newRoom)
|
|
||||||
|
|
||||||
err = s.Database.UpdateField(&Location{ID: newRoom.LocationID}, "Rooms", roomList)
|
newItem := Item{
|
||||||
|
Name: itemName,
|
||||||
|
CoverPhoto: coverImageURL,
|
||||||
|
Photos: photoURLsList,
|
||||||
|
Notes: itemNotes,
|
||||||
|
}
|
||||||
|
|
||||||
|
if storageType == "room" {
|
||||||
|
newItem.RoomID = room.ID
|
||||||
|
itemList := room.Items
|
||||||
|
itemList = append(itemList, newItem)
|
||||||
|
err = s.Database.UpdateField(&Room{ID: roomID}, "Items", itemList)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
newItem.CabinetID = cabinet.ID
|
||||||
|
itemList := cabinet.Items
|
||||||
|
itemList = append(itemList, newItem)
|
||||||
|
err = s.Database.UpdateField(&Cabinet{ID: cabinetID}, "Items", itemList)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = s.AddItem(newItem)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.Log.Debug().Msgf("Adding new Room: %v", newRoom)
|
|
||||||
return c.Status(fiber.StatusOK).SendString(fmt.Sprintf("New Room Created! %s", newRoom.Name))
|
s.Log.Debug().Msgf("Adding new Room: %v", newItem)
|
||||||
|
return c.Status(fiber.StatusOK).SendString(fmt.Sprintf("New Room Created! %s", newItem.Name))
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteItemHandler deletes an item based on an ID
|
||||||
|
func (s *Server) DeleteItemHandler(c *fiber.Ctx) error {
|
||||||
|
itemID, err := c.ParamsInt("itemID")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
item, err := s.GetItem(itemID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Err(err).Msgf("Unable to fetch item with id: %d", itemID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = s.DeleteItem(item)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Err(err).Msgf("Unable to delete item with id: %d", itemID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.Status(fiber.StatusOK).SendString(fmt.Sprintf("Item Deleted! %d", itemID))
|
||||||
}
|
}
|
||||||
|
@@ -29,6 +29,20 @@ func (s *Server) DeleteRoomHandler(c *fiber.Ctx) error {
|
|||||||
return c.Status(fiber.StatusOK).SendString(fmt.Sprintf("Room Deleted! %d", roomID))
|
return c.Status(fiber.StatusOK).SendString(fmt.Sprintf("Room Deleted! %d", roomID))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetRoomHandler returns a single room from an ID
|
||||||
|
func (s *Server) GetRoomHandler(c *fiber.Ctx) error {
|
||||||
|
roomID, err := c.ParamsInt("roomID")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
room, err := s.GetRoom(roomID)
|
||||||
|
if err != nil {
|
||||||
|
s.Log.Err(err).Msgf("Unable to fetch room with id: %d", roomID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.Status(fiber.StatusOK).JSON(room)
|
||||||
|
}
|
||||||
|
|
||||||
// AddNewRoomHandler adds a new room
|
// AddNewRoomHandler adds a new room
|
||||||
func (s *Server) AddNewRoomHandler(c *fiber.Ctx) error {
|
func (s *Server) AddNewRoomHandler(c *fiber.Ctx) error {
|
||||||
locID, err := c.ParamsInt("locID")
|
locID, err := c.ParamsInt("locID")
|
||||||
@@ -113,15 +127,15 @@ func (s *Server) AddNewRoomHandler(c *fiber.Ctx) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Update our location in the db to add the new room as attached to the ID
|
// // Update our location in the db to add the new room as attached to the ID
|
||||||
loc, err = s.GetLocation(newRoom.LocationID)
|
// loc, err = s.GetLocation(newRoom.LocationID)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
roomList := loc.Rooms
|
roomList := loc.Rooms
|
||||||
roomList = append(roomList, newRoom)
|
roomList = append(roomList, newRoom)
|
||||||
|
|
||||||
err = s.Database.UpdateField(&Location{ID: newRoom.LocationID}, "Rooms", roomList)
|
err = s.Database.UpdateField(&Location{ID: locID}, "Rooms", roomList)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
19
main.go
19
main.go
@@ -99,11 +99,28 @@ func main() {
|
|||||||
server.WebServer.Get("/api/locations/:locID", server.GetSingleLocationHandler)
|
server.WebServer.Get("/api/locations/:locID", server.GetSingleLocationHandler)
|
||||||
server.WebServer.Post("/api/locations/new", server.AddNewLocationHandler)
|
server.WebServer.Post("/api/locations/new", server.AddNewLocationHandler)
|
||||||
server.WebServer.Delete("/api/locations/:locID", server.DeleteLocationHandler)
|
server.WebServer.Delete("/api/locations/:locID", server.DeleteLocationHandler)
|
||||||
|
server.WebServer.Get("/api/locations/:locID/rooms", server.GetRoomsAtLocationHandler)
|
||||||
// Room Routes
|
// Room Routes
|
||||||
server.WebServer.Get("/api/rooms", server.GetAllRoomsHandler)
|
server.WebServer.Get("/api/rooms", server.GetAllRoomsHandler)
|
||||||
server.WebServer.Get("/api/rooms/:locID", server.GetRoomsAtLocationHandler)
|
server.WebServer.Get("/api/rooms/:roomID", server.GetRoomHandler)
|
||||||
server.WebServer.Post("/api/rooms/:locID/new", server.AddNewRoomHandler)
|
server.WebServer.Post("/api/rooms/:locID/new", server.AddNewRoomHandler)
|
||||||
server.WebServer.Delete("/api/rooms/:roomID", server.DeleteRoomHandler)
|
server.WebServer.Delete("/api/rooms/:roomID", server.DeleteRoomHandler)
|
||||||
|
server.WebServer.Get("/api/rooms/:roomID/cabinets", server.GetCabinetsAtRoomHandler)
|
||||||
|
server.WebServer.Get("/api/rooms/:roomID/items", server.GetAllItemsAtRoomHandler)
|
||||||
|
// Cabinet Routes
|
||||||
|
server.WebServer.Get("/api/cabinets", server.GetAllCabinetsHandler)
|
||||||
|
server.WebServer.Get("/api/cabinets/:cabinetID", server.GetCabinetHandler)
|
||||||
|
server.WebServer.Get("/api/cabinets/:cabinetID/items", server.GetAllItemsAtCabinetHandler)
|
||||||
|
server.WebServer.Delete("/api/cabinets/:cabinetID", server.DeleteCabinetHandler)
|
||||||
|
|
||||||
|
// Item Routes
|
||||||
|
server.WebServer.Get("/api/items", server.GetAllItemsHandler)
|
||||||
|
server.WebServer.Get("/api/items/room/:roomID", server.GetAllItemsAtRoomHandler)
|
||||||
|
server.WebServer.Get("/api/items/cabinet/:cabinetID", server.GetAllItemsAtCabinetHandler)
|
||||||
|
server.WebServer.Post("/api/items/cabinet/:cabinetID/new", server.AddNewItemHandler)
|
||||||
|
server.WebServer.Post("/api/items/room/:roomID/new", server.AddNewItemHandler)
|
||||||
|
server.WebServer.Delete("/api/items/:itemID", server.DeleteItemHandler)
|
||||||
|
|
||||||
fmt.Println("Everything ready, starting server! ", fmt.Sprintf(":%s", server.Config.Server.Port))
|
fmt.Println("Everything ready, starting server! ", fmt.Sprintf(":%s", server.Config.Server.Port))
|
||||||
server.WebServer.Listen(fmt.Sprintf(":%s", server.Config.Server.Port))
|
server.WebServer.Listen(fmt.Sprintf(":%s", server.Config.Server.Port))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user