Files
go-api-template/handlers.go

116 lines
3.2 KiB
Go

package main
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/gofiber/fiber/v2"
)
//Get Server Config
func (s *Server) GetServerConfig(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(fiber.Map{"BasicAuth": s.Config.Authentication.BasicAuth, "Timezone": s.Config.Timezone})
}
// Add a new location
func (s *Server) AddNewLocationHandler(c *fiber.Ctx) error {
form, err := c.MultipartForm()
if err != nil {
s.Log.Err(err).Msgf("Unable to fetch locations: ", err)
return err
}
locName := form.Value["Name"][0]
locPhoto := form.File["CoverPhoto"][0]
if err != nil {
s.Log.Err(err).Msgf("Unable to get CoverPhoto for location: ", err)
return err
}
locAdditionalPhotos := form.File["AdditionalPhotos"]
// Create the directory for this location to store the photos
err = os.Mkdir(s.Config.Server.LocationPhotoDir+locName, 0755)
if err != nil {
return err
}
var photoPathsList []string
for _, 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)
photoPathsList = append(photoPathsList, photoPath)
photoData, err := io.ReadAll(src)
if err != nil {
return err
}
os.WriteFile(photoPath, photoData, 0644)
}
// create files on server for all uploads
locNotes := form.Value["Notes"][0]
// saving the uploaded image to our images dir
coverImagePath := filepath.ToSlash(s.Config.Server.LocationPhotoDir + locName + "/" + locPhoto.Filename)
src, err := locPhoto.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
}
newLocation := Location{
Name: locName,
CoverPhoto: coverImagePath,
Photos: photoPathsList,
Notes: locNotes,
}
err = s.AddLocation(newLocation)
if err != nil {
return err
}
return c.Status(fiber.StatusOK).SendString(fmt.Sprintf("New Location Created! %s", newLocation.Name))
}
func (s *Server) GetAllLocationsHandler(c *fiber.Ctx) error {
locations, err := s.GetLocations()
if err != nil {
s.Log.Err(err).Msgf("Unable to fetch locations: ", err)
return err
}
s.Log.Debug().Msgf("Returning Locations: %+v", locations)
testLoc := Location{
ID: 1,
Description: "This is a description of the location",
Name: "testName",
CoverPhoto: "testString", // A "cover photo" for the location
Notes: "Notes String",
}
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",
}
var locArray [6]Location
locArray[0] = testLoc
locArray[1] = testLoc2
fmt.Println("Returning TestLoc: ", locArray)
return c.Status(fiber.StatusOK).JSON(locArray)
}
func (s *Server) DeleteLocationHandler(c *fiber.Ctx) error {
locID := c.Params("locID")
fmt.Println("LocID: ", locID)
return c.Status(fiber.StatusOK).JSON(locID)
}