Files
goInventorize/handlers_item.go

126 lines
3.5 KiB
Go

package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"github.com/gofiber/fiber/v2"
)
func (s *Server) GetAllItemsAtRoomHandler(c *fiber.Ctx) error {
roomID, err := c.ParamsInt("roomID")
if err != nil {
return err
}
items, err := s.GetItemsInRoom(roomID)
fmt.Println("Items: ", items)
if err != nil {
return err
}
return c.Status(fiber.StatusOK).JSON(items)
}
// AddNewItemHandler adds a new room
func (s *Server) AddNewItemHandler(c *fiber.Ctx) error {
locID, err := c.ParamsInt("locID")
if err != nil {
return err
}
form, err := c.MultipartForm()
if err != nil {
s.Log.Err(err).Msgf("Unable to fetch rooms: ", err)
return err
}
roomName := form.Value["Name"][0]
fmt.Println("COverphoto: ", form.File["CoverPhoto"])
roomPhoto := form.File["CoverPhoto"][0]
// if err != nil {
// s.Log.Err(err).Msgf("Unable to get CoverPhoto for Room: ", err)
// return err
// }
roomAdditionalPhotos := form.File["AdditionalPhotos"]
// Fetch the location information
loc, err := s.GetLocation(locID)
if err != nil {
s.Log.Err(err).Msgf("Unable to fetch location by id: %d with error: %s", locID, 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
err = os.MkdirAll(filepath.ToSlash(s.Config.Server.LocationPhotoDir+"/"+loc.Name+"/"+roomName), 0755)
if err != nil {
return err
}
var photoURLsList []string
for i, photo := range roomAdditionalPhotos {
src, err := photo.Open()
if err != nil {
return err
}
defer src.Close()
photoPath := filepath.ToSlash(pathPrepend + roomName + "_" + strconv.Itoa(i) + filepath.Ext(photo.Filename))
photoURL := urlPrepend + roomName + "_" + strconv.Itoa(i) + filepath.Ext(photo.Filename)
photoURLsList = append(photoURLsList, photoURL)
photoData, err := io.ReadAll(src)
if err != nil {
return err
}
os.WriteFile(photoPath, photoData, 0644)
}
// create files on server for all uploads
roomNotes := form.Value["Notes"][0]
// saving the uploaded image to our images dir
coverImagePath := filepath.ToSlash(pathPrepend + roomName + "_cover" + filepath.Ext(roomPhoto.Filename))
// Creating the URL to retrieve photo
coverImageURL := urlPrepend + roomName + "_cover" + filepath.Ext(roomPhoto.Filename)
src, err := roomPhoto.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
}
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)
if err != nil {
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))
}