react-router frontend, fixing backend issues, adding /api
This commit is contained in:
79
handlers.go
79
handlers.go
@@ -5,6 +5,7 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
@@ -30,18 +31,19 @@ func (s *Server) AddNewLocationHandler(c *fiber.Ctx) error {
|
||||
}
|
||||
locAdditionalPhotos := form.File["AdditionalPhotos"]
|
||||
// Create the directory for this location to store the photos
|
||||
err = os.Mkdir(s.Config.Server.LocationPhotoDir+locName, 0755)
|
||||
err = os.MkdirAll(s.Config.Server.LocationPhotoDir+locName, 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var photoPathsList []string
|
||||
for _, photo := range locAdditionalPhotos {
|
||||
for i, photo := range locAdditionalPhotos {
|
||||
src, err := photo.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer src.Close()
|
||||
photoPath := filepath.ToSlash(s.Config.Server.LocationPhotoDir + locName + "/" + photo.Filename)
|
||||
|
||||
photoPath := filepath.ToSlash(s.Config.Server.LocationPhotoDir + locName + "/" + locName + "_" + strconv.Itoa(i) + filepath.Ext(photo.Filename))
|
||||
photoPathsList = append(photoPathsList, photoPath)
|
||||
photoData, err := io.ReadAll(src)
|
||||
if err != nil {
|
||||
@@ -53,7 +55,7 @@ func (s *Server) AddNewLocationHandler(c *fiber.Ctx) error {
|
||||
locNotes := form.Value["Notes"][0]
|
||||
|
||||
// saving the uploaded image to our images dir
|
||||
coverImagePath := filepath.ToSlash(s.Config.Server.LocationPhotoDir + locName + "/" + locPhoto.Filename)
|
||||
coverImagePath := filepath.ToSlash(s.Config.Server.LocationPhotoDir + locName + "/" + locName + "_cover" + filepath.Ext(locPhoto.Filename))
|
||||
src, err := locPhoto.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -78,6 +80,7 @@ func (s *Server) AddNewLocationHandler(c *fiber.Ctx) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.Log.Debug().Msgf("Adding new location: %v", newLocation)
|
||||
return c.Status(fiber.StatusOK).SendString(fmt.Sprintf("New Location Created! %s", newLocation.Name))
|
||||
}
|
||||
|
||||
@@ -88,29 +91,59 @@ func (s *Server) GetAllLocationsHandler(c *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
s.Log.Debug().Msgf("Returning Locations: %+v", locations)
|
||||
testLoc := Location{
|
||||
ID: 1,
|
||||
Description: "This is a description of the location",
|
||||
Name: "Location1",
|
||||
CoverPhoto: "testCover.png", // A "cover photo" for the location
|
||||
Notes: "Notes String",
|
||||
return c.Status(fiber.StatusOK).JSON(locations)
|
||||
}
|
||||
|
||||
func (s *Server) GetSingleLocationHandler(c *fiber.Ctx) error {
|
||||
locID, err := c.ParamsInt("locID")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
testLoc2 := Location{
|
||||
ID: 2,
|
||||
Description: "This is a description of the location2",
|
||||
Name: "testName2",
|
||||
CoverPhoto: "testString2", // A "cover photo" for the location
|
||||
Notes: "Notes String2",
|
||||
location, err := s.GetLocation(locID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var locArray [6]Location
|
||||
locArray[0] = testLoc
|
||||
locArray[1] = testLoc2
|
||||
fmt.Println("Returning TestLoc: ", locArray)
|
||||
return c.Status(fiber.StatusOK).JSON(locArray)
|
||||
fmt.Println("Returning A single location: ", location)
|
||||
return c.Status(fiber.StatusOK).JSON(location)
|
||||
}
|
||||
|
||||
func (s *Server) DeleteLocationHandler(c *fiber.Ctx) error {
|
||||
locID := c.Params("locID")
|
||||
locID, err := c.ParamsInt("locID")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
location, err := s.GetLocation(locID)
|
||||
if err != nil {
|
||||
s.Log.Err(err).Msgf("Unable to fetch location with id: %d", locID)
|
||||
return err
|
||||
}
|
||||
err = s.DeleteLocation(location)
|
||||
if err != nil {
|
||||
s.Log.Err(err).Msgf("Unable to delete location with id: %d", locID)
|
||||
return err
|
||||
}
|
||||
fmt.Println("LocID: ", locID)
|
||||
return c.Status(fiber.StatusOK).JSON(locID)
|
||||
return c.Status(fiber.StatusOK).SendString(fmt.Sprintf("Location Deleted! %d", locID))
|
||||
}
|
||||
|
||||
// GetAllRoomsHandler gets all of the rooms in the db
|
||||
func (s *Server) GetAllRoomsHandler(c *fiber.Ctx) error {
|
||||
rooms, err := s.GetAllRooms()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Status(fiber.StatusOK).JSON(rooms)
|
||||
}
|
||||
|
||||
func (s *Server) GetRoomsAtLocationHandler(c *fiber.Ctx) error {
|
||||
locID, err := c.ParamsInt("locID")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rooms, err := s.GetRoomsAtLocation(locID)
|
||||
fmt.Println("ROOMS: ", rooms)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Status(fiber.StatusOK).JSON(rooms)
|
||||
}
|
||||
|
Reference in New Issue
Block a user