adding dboverview route, fixing add new location and add new room

This commit is contained in:
2022-04-09 22:27:26 -04:00
parent 25deaf43c8
commit c79dc69bb0
10 changed files with 274 additions and 53 deletions

View File

@@ -2,7 +2,6 @@ package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
@@ -51,72 +50,79 @@ func (s *Server) AddNewRoomHandler(c *fiber.Ctx) error {
}
form, err := c.MultipartForm()
if err != nil {
s.Log.Err(err).Msgf("Unable to fetch rooms: ", err)
s.Log.Err(err).Msgf("Unable to fetch new room form details: ", 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
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.LocationFilesDir + "/" + urlPrepend
// Create the directory for this room to store the photos
err = os.MkdirAll(filepath.ToSlash(s.Config.Server.LocationFilesDir+"/"+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]
// setting the path prepend for the photos
urlPrepend := loc.Name + "/" + roomName + "/"
pathPrepend := s.Config.Server.LocationFilesDir + "/" + urlPrepend
// saving the uploaded image to our images dir
coverImagePath := filepath.ToSlash(pathPrepend + roomName + "_cover" + filepath.Ext(roomPhoto.Filename))
err = c.SaveFile(roomPhoto, coverImagePath)
if err != nil {
s.Log.Err(err).Msgf("Unable to write file: %s to path: %s, encountered err: %s", roomPhoto.Filename, coverImagePath, err)
return err
}
// Creating the URL to retrieve photo
coverImageURL := urlPrepend + roomName + "_cover" + filepath.Ext(roomPhoto.Filename)
src, err := roomPhoto.Open()
if err != nil {
return err
var photoURLsList []string
if roomAdditionalPhotos := form.File["AdditionalPhotos"]; len(roomAdditionalPhotos) > 0 {
for i, photo := range roomAdditionalPhotos {
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)
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)
// Parse all the other fields
roomNotes := ""
roomNotes = form.Value["Notes"][0]
description := ""
description = form.Value["Description"][0]
squareFeet := ""
squareFeet = form.Value["SquareFeet"][0]
squareFeetInt, err := strconv.ParseInt(squareFeet, 10, 64)
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
s.Log.Err(err).Msgf("Unable to parse Square feet, input: %s error: %s", squareFeet, err)
}
paint := ""
paint = form.Value["Paint"][0]
newRoom := Room{
Name: roomName,
Description: description,
SquareFeet: int(squareFeetInt),
Paint: paint,
CoverPhoto: coverImageURL,
Photos: photoURLsList,
Notes: roomNotes,
@@ -127,11 +133,7 @@ func (s *Server) AddNewRoomHandler(c *fiber.Ctx) error {
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)