pollo/api/deleteroom.go

46 lines
1.6 KiB
Go
Raw Permalink Normal View History

2024-06-28 14:23:35 -06:00
package api
import (
2024-06-28 15:16:18 -06:00
"fmt"
2024-06-28 14:23:35 -06:00
"net/http"
"pollo/lib"
"github.com/labstack/echo/v4"
)
// DeleteRoomHandler handles the deletion of a room by ID.
func DeleteRoomHandler(c echo.Context) error {
roomID := c.Param("id")
2024-06-28 15:16:18 -06:00
currentSession, cookieError := lib.GetSessionCookie(c.Request(), "session")
if cookieError != nil {
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "unauthorized"})
}
2024-06-28 14:23:35 -06:00
if roomID == "" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "room ID is required"})
}
2024-11-21 00:48:45 -06:00
deletionError := lib.DeleteRoom(lib.GetDB(), roomID)
2024-06-28 15:16:18 -06:00
if deletionError != nil {
2024-06-28 14:23:35 -06:00
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to delete room"})
}
2024-06-28 15:16:18 -06:00
// Retrieve the updated list of rooms for the user
2024-11-21 00:48:45 -06:00
rooms, fetchError := lib.GetRoomsByUserID(lib.GetDB(), currentSession.UserID)
2024-06-28 15:16:18 -06:00
if fetchError != nil {
println("Error retrieving rooms: ", fetchError.Error())
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to retrieve rooms"})
}
// Start building the HTML content for the updated list of rooms
2024-07-05 13:51:48 -06:00
htmlContent := "<tbody id='room-list'>"
2024-06-28 15:16:18 -06:00
for _, room := range rooms {
htmlContent += fmt.Sprintf("<tr class='hover border-white'><td class='break-all max-w-[200px] md:max-w-[400px]'>%s</td> <td><a class='m-2' href='/room/%s'>➡️</a> <button class='m-2' hx-delete='/api/room/%s' hx-target='#room-list' hx-swap='outerHTML'>❌</button></td></tr>", room.RoomName, room.ID, room.ID)
2024-06-28 15:16:18 -06:00
}
2024-07-05 13:51:48 -06:00
htmlContent += "</tbody>"
2024-06-28 15:16:18 -06:00
// Return the dynamically generated HTML content
return c.HTML(http.StatusOK, htmlContent)
2024-06-28 14:23:35 -06:00
}