more room routes
This commit is contained in:
70
handlers.go
70
handlers.go
@@ -125,26 +125,59 @@ func (s *Server) DeleteLocationHandler(c *fiber.Ctx) error {
|
||||
s.Log.Err(err).Msgf("Unable to delete location with id: %d", locID)
|
||||
return err
|
||||
}
|
||||
fmt.Println("LocID: ", locID)
|
||||
return c.Status(fiber.StatusOK).SendString(fmt.Sprintf("Location Deleted! %d", locID))
|
||||
}
|
||||
|
||||
// Add a new location
|
||||
// DeleteRoomHandler deletes a room based on an ID
|
||||
func (s *Server) DeleteRoomHandler(c *fiber.Ctx) error {
|
||||
roomID, err := c.ParamsInt("roomID")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
room, err := s.GetRoom(roomID)
|
||||
if err != nil {
|
||||
s.Log.Err(err).Msgf("Unable to fetch room with id: %d", roomID)
|
||||
return err
|
||||
}
|
||||
err = s.DeleteRoom(room)
|
||||
if err != nil {
|
||||
s.Log.Err(err).Msgf("Unable to delete room with id: %d", roomID)
|
||||
return err
|
||||
}
|
||||
return c.Status(fiber.StatusOK).SendString(fmt.Sprintf("Room Deleted! %d", roomID))
|
||||
}
|
||||
|
||||
// AddNewRoomHandler adds a new room
|
||||
func (s *Server) AddNewRoomHandler(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"]
|
||||
// Create the directory for this location to store the photos
|
||||
err = os.MkdirAll(s.Config.Server.LocationPhotoDir+roomName, 0755)
|
||||
// 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
|
||||
}
|
||||
@@ -156,8 +189,8 @@ func (s *Server) AddNewRoomHandler(c *fiber.Ctx) error {
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
photoPath := filepath.ToSlash(s.Config.Server.LocationPhotoDir + roomName + "/" + roomName + "_" + strconv.Itoa(i) + filepath.Ext(photo.Filename))
|
||||
photoURL := roomName + "_" + strconv.Itoa(i) + filepath.Ext(photo.Filename)
|
||||
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 {
|
||||
@@ -166,12 +199,12 @@ func (s *Server) AddNewRoomHandler(c *fiber.Ctx) error {
|
||||
os.WriteFile(photoPath, photoData, 0644)
|
||||
}
|
||||
// create files on server for all uploads
|
||||
locNotes := form.Value["Notes"][0]
|
||||
roomNotes := form.Value["Notes"][0]
|
||||
|
||||
// saving the uploaded image to our images dir
|
||||
coverImagePath := filepath.ToSlash(s.Config.Server.LocationPhotoDir + roomName + "/" + roomName + "_cover" + filepath.Ext(roomPhoto.Filename))
|
||||
coverImagePath := filepath.ToSlash(pathPrepend + roomName + "_cover" + filepath.Ext(roomPhoto.Filename))
|
||||
// Creating the URL to retrieve photo
|
||||
coverImageURL := roomName + "_cover" + filepath.Ext(roomPhoto.Filename)
|
||||
coverImageURL := urlPrepend + roomName + "_cover" + filepath.Ext(roomPhoto.Filename)
|
||||
src, err := roomPhoto.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -186,18 +219,20 @@ func (s *Server) AddNewRoomHandler(c *fiber.Ctx) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newLocation := Location{
|
||||
Name: roomName,
|
||||
CoverPhoto: coverImageURL,
|
||||
Photos: photoURLsList,
|
||||
Notes: locNotes,
|
||||
newRoom := Room{
|
||||
Name: roomName,
|
||||
CoverPhoto: coverImageURL,
|
||||
Photos: photoURLsList,
|
||||
Notes: roomNotes,
|
||||
LocationID: locID,
|
||||
LocationName: loc.Name,
|
||||
}
|
||||
err = s.AddLocation(newLocation)
|
||||
err = s.AddRoom(newRoom)
|
||||
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))
|
||||
s.Log.Debug().Msgf("Adding new Room: %v", newRoom)
|
||||
return c.Status(fiber.StatusOK).SendString(fmt.Sprintf("New Room Created! %s", newRoom.Name))
|
||||
}
|
||||
|
||||
// GetAllRoomsHandler gets all of the rooms in the db
|
||||
@@ -209,6 +244,7 @@ func (s *Server) GetAllRoomsHandler(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusOK).JSON(rooms)
|
||||
}
|
||||
|
||||
// GetRoomsAtLocationHandler gets rooms at a location
|
||||
func (s *Server) GetRoomsAtLocationHandler(c *fiber.Ctx) error {
|
||||
locID, err := c.ParamsInt("locID")
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user