Room CRUD working!

This commit is contained in:
Atridad Lahiji 2024-06-28 15:16:18 -06:00
parent 7cc7226dee
commit 4ad17de80a
No known key found for this signature in database
5 changed files with 34 additions and 18 deletions

View file

@ -36,7 +36,7 @@ func CreateRoomHandler(c echo.Context) error {
// Start building the HTML content for the updated list of rooms // Start building the HTML content for the updated list of rooms
htmlContent := "<div id='room-list'>" htmlContent := "<div id='room-list'>"
for _, room := range rooms { for _, room := range rooms {
htmlContent += fmt.Sprintf("<div class='room-name'>%s</div>", room.RoomName) htmlContent += fmt.Sprintf("<div class='room-name'>%s <button hx-delete='/api/room/%s' hx-target='#room-list' hx-swap='outerHTML'>❌</button></div>", room.RoomName, room.ID)
} }
htmlContent += "</div>" htmlContent += "</div>"

View file

@ -1,6 +1,7 @@
package api package api
import ( import (
"fmt"
"net/http" "net/http"
"pollo/lib" "pollo/lib"
@ -10,14 +11,35 @@ import (
// DeleteRoomHandler handles the deletion of a room by ID. // DeleteRoomHandler handles the deletion of a room by ID.
func DeleteRoomHandler(c echo.Context) error { func DeleteRoomHandler(c echo.Context) error {
roomID := c.Param("id") roomID := c.Param("id")
currentSession, cookieError := lib.GetSessionCookie(c.Request(), "session")
if cookieError != nil {
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "unauthorized"})
}
if roomID == "" { if roomID == "" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "room ID is required"}) return c.JSON(http.StatusBadRequest, map[string]string{"error": "room ID is required"})
} }
err := lib.DeleteRoom(lib.GetDBPool(), roomID) deletionError := lib.DeleteRoom(lib.GetDBPool(), roomID)
if err != nil { if deletionError != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to delete room"}) return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to delete room"})
} }
return c.NoContent(http.StatusNoContent) // Retrieve the updated list of rooms for the user
rooms, fetchError := lib.GetRoomsByUserID(lib.GetDBPool(), currentSession.UserID)
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
htmlContent := "<div id='room-list'>"
for _, room := range rooms {
htmlContent += fmt.Sprintf("<div class='room-name'>%s <button hx-delete='/api/room/%s' hx-target='#room-list' hx-swap='outerHTML'>❌</button></div>", room.RoomName, room.ID)
}
htmlContent += "</div>"
// Return the dynamically generated HTML content
return c.HTML(http.StatusOK, htmlContent)
} }

View file

@ -22,12 +22,10 @@ func GetAllRoomsHandler(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to retrieve rooms"}) return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to retrieve rooms"})
} }
// Start building the HTML content // Start building the HTML content for the updated list of rooms
htmlContent := "<div id='room-list'>" htmlContent := "<div id='room-list'>"
for _, room := range rooms { for _, room := range rooms {
// For each room, append an HTML element to htmlContent htmlContent += fmt.Sprintf("<div class='room-name'>%s <button hx-delete='/api/room/%s' hx-target='#room-list' hx-swap='outerHTML'>❌</button></div>", room.RoomName, room.ID)
// Customize this HTML structure as needed to match your desired appearance
htmlContent += fmt.Sprintf("<div class='room-name'>%s</div>", room.RoomName)
} }
htmlContent += "</div>" htmlContent += "</div>"

View file

@ -90,9 +90,9 @@ func main() {
apiGroup.POST("/signin", api.SignInUserHandler) apiGroup.POST("/signin", api.SignInUserHandler)
apiGroup.POST("/signout", api.SignOutUserHandler) apiGroup.POST("/signout", api.SignOutUserHandler)
// Rooms routes // Rooms routes
apiGroup.POST("/room/create", api.CreateRoomHandler) apiGroup.POST("/room", api.CreateRoomHandler)
apiGroup.GET("/room/list", api.GetAllRoomsHandler) apiGroup.GET("/room", api.GetAllRoomsHandler)
apiGroup.POST("/room/delete", api.DeleteRoomHandler) apiGroup.DELETE("/room/:id", api.DeleteRoomHandler)
// Webhook Routes: // Webhook Routes:
webhookGroup := e.Group("/webhook") webhookGroup := e.Group("/webhook")

View file

@ -16,16 +16,12 @@ Pollo // Dashboard
Hi, {{.Name}}! Hi, {{.Name}}!
</h1> </h1>
<!-- Form to create a new room --> <!-- Form to create a new room -->
<form method="POST" hx-post="/api/room/create" hx-target="#room-list" hx-swap="outerHTML"> <form class="flex flex-col gap-2" method="POST" hx-post="/api/room" hx-target="#room-list" hx-swap="outerHTML">
<input type="text" name="roomName" placeholder="Room Name" required> <input class="input input-bordered input-primary w-full max-w-xs" type="text" name="roomName" placeholder="Room Name" required>
<button type="submit" class="btn btn-primary">Create Room</button> <button type="submit" class="btn btn-primary">Create Room</button>
</form> </form>
<!-- Section to list rooms --> <!-- Section to list rooms -->
<div class="flex flex-col gap-2" id="room-list" hx-get="/api/room/list" hx-trigger="load"> <div class="flex flex-col gap-2" id="room-list" hx-get="/api/room" hx-trigger="load">
<!-- Example of how rooms might be loaded -->
<!-- <div class="room-name">Room 1</div>
<div class="room-name">Room 2</div> -->
<!-- Rooms will be loaded here -->
</div> </div>
</div> </div>
{{end}} {{end}}