working on sending the new location form data, and getting it from the backend

This commit is contained in:
2022-04-08 22:51:19 -04:00
parent 5a71db469a
commit 25deaf43c8
24 changed files with 170 additions and 68 deletions

View File

@@ -2,7 +2,6 @@ package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
@@ -23,61 +22,98 @@ func (s *Server) AddNewLocationHandler(c *fiber.Ctx) error {
s.Log.Err(err).Msgf("Unable to fetch locations: ", err)
return err
}
// Both of these should be required on frontend, so not checking them because I'm lazy
// TODO: Check to make sure these exist
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.MkdirAll(s.Config.Server.LocationPhotoDir+locName, 0755)
err = os.MkdirAll(s.Config.Server.LocationFilesDir+locName, 0755)
if err != nil {
s.Log.Err(err).Msgf("Unable to create photos dir path: Attempted path: %s error: %s", s.Config.Server.LocationFilesDir+locName, err)
return err
}
var photoURLsList []string
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 + "/" + locName + "_" + strconv.Itoa(i) + filepath.Ext(photo.Filename))
photoURL := locName + "_" + 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)
// saving the uploaded cover image to our images dir
coverImagePath := filepath.ToSlash(s.Config.Server.LocationFilesDir + locName + "/" + locName + "_cover" + filepath.Ext(locPhoto.Filename))
err = c.SaveFile(locPhoto, coverImagePath)
if err != nil {
s.Log.Err(err).Msgf("Unable to write file: %s to path: %s, encountered err: %s", locPhoto.Filename, coverImagePath, err)
return err
}
// 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 + "/" + locName + "_cover" + filepath.Ext(locPhoto.Filename))
// Creating the URL to retrieve photo
coverImageURL := locName + "_cover" + filepath.Ext(locPhoto.Filename)
src, err := locPhoto.Open()
if err != nil {
return err
// Creating the empty list to store all of our additional photo URLS
var photoURLsList []string
// If we have additional files, handle those
if locAdditionalPhotos := form.File["AdditionalPhotos"]; len(locAdditionalPhotos) > 0 {
fmt.Println("Additional Photos: ", locAdditionalPhotos)
for i, photo := range locAdditionalPhotos {
photoPath := filepath.ToSlash(s.Config.Server.LocationFilesDir + locName + "/" + locName + "_" + strconv.Itoa(i) + filepath.Ext(photo.Filename))
photoURL := locName + "_" + strconv.Itoa(i) + filepath.Ext(photo.Filename)
photoURLsList = append(photoURLsList, photoURL)
err = c.SaveFile(photo, photoPath)
if err != nil {
s.Log.Err(err).Msgf("Unable to write photo: %s to path: %s, encountered err: %s", photo.Filename, photoPath, err)
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
var fileURLsList []string
if additionalFiles := form.File["Files"]; len(additionalFiles) > 0 {
for i, file := range additionalFiles {
filePath := filepath.ToSlash(s.Config.Server.LocationFilesDir + locName + "/" + locName + "_" + strconv.Itoa(i) + filepath.Ext(file.Filename))
fileUrl := locName + "_" + strconv.Itoa(i) + filepath.Ext(file.Filename)
fileURLsList = append(fileURLsList, fileUrl)
err = c.SaveFile(file, filePath)
if err != nil {
s.Log.Err(err).Msgf("Unable to write file: %s to path: %s, encountered err: %s", file.Filename, filePath, err)
return err
}
}
}
err = os.WriteFile(coverImagePath, coverImageData, 0644)
// Parse all the other fields
locNotes := ""
locNotes = form.Value["Notes"][0]
address := ""
address = form.Value["Address"][0]
squareFeet := ""
squareFeet = form.Value["SquareFeet"][0]
squareFeetInt, err := strconv.ParseInt(squareFeet, 10, 64)
if err != nil {
return err
s.Log.Err(err).Msgf("Unable to parse Square feet, input: %s error: %s", squareFeet, err)
}
latitude := ""
latitude = form.Value["Latitude"][0]
longitude := ""
longitude = form.Value["Latitude"][0]
datePurchased := ""
datePurchased = form.Value["DatePurchased"][0]
purchasePrice := ""
purchasePrice = form.Value["PurchasePrice"][0]
newLocation := Location{
Name: locName,
CoverPhoto: coverImageURL,
Photos: photoURLsList,
Notes: locNotes,
Name: locName,
CoverPhoto: coverImageURL,
Address: address,
Latitude: latitude,
Longitude: longitude,
DatePurchased: datePurchased,
PurchasePrice: purchasePrice,
SquareFeet: int(squareFeetInt),
Photos: photoURLsList,
Files: fileURLsList,
Notes: locNotes,
}
err = s.AddLocation(newLocation)
if err != nil {