Room joining works (with basic room rendering)

This commit is contained in:
Atridad Lahiji 2024-07-05 22:35:42 -06:00
parent 1758c6884b
commit b1e382420a
No known key found for this signature in database
9 changed files with 117 additions and 4 deletions

View file

@ -36,7 +36,7 @@ func CreateRoomHandler(c echo.Context) error {
// Start building the HTML content for the updated list of rooms
htmlContent := "<tbody id='room-list'>"
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><button class='m-2' hx-delete='/api/room/%s' hx-target='#room-list' hx-swap='outerHTML'>❌</button></td></tr>", room.RoomName, room.ID)
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)
}
htmlContent += "</tbody>"

View file

@ -36,7 +36,7 @@ func DeleteRoomHandler(c echo.Context) error {
// Start building the HTML content for the updated list of rooms
htmlContent := "<tbody id='room-list'>"
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><button class='m-2' hx-delete='/api/room/%s' hx-target='#room-list' hx-swap='outerHTML'>❌</button></td></tr>", room.RoomName, room.ID)
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)
}
htmlContent += "</tbody>"

27
api/getroom.go Normal file
View file

@ -0,0 +1,27 @@
package api
import (
"fmt"
"net/http"
"pollo/lib"
"github.com/labstack/echo/v4"
)
// GetRoomHandler handles the request to get a room by ID.
func GetRoomHandler(c echo.Context) error {
roomID := c.Param("id")
room, err := lib.GetRoomById(lib.GetDBPool(), roomID)
if err != nil {
// Log the error
println("Error retrieving room: ", err.Error())
// If the room is not found, return a 404 error
return c.Render(http.StatusNotFound, "404.html", nil)
}
// Start building the HTML content for the updated list of rooms
htmlContent := fmt.Sprintf("<div>%s", room.ID)
// Return the dynamically generated HTML content
return c.HTML(http.StatusOK, htmlContent)
}

View file

@ -25,7 +25,7 @@ func GetAllRoomsHandler(c echo.Context) error {
// Start building the HTML content for the updated list of rooms
htmlContent := "<tbody id='room-list'>"
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><button class='m-2' hx-delete='/api/room/%s' hx-target='#room-list' hx-swap='outerHTML'>❌</button></td></tr>", room.RoomName, room.ID)
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)
}
htmlContent += "</tbody>"

View file

@ -2,7 +2,9 @@ package lib
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
"github.com/jackc/pgx/v4/pgxpool"
@ -85,3 +87,19 @@ func GetRoomsByUserID(dbPool *pgxpool.Pool, userID string) ([]Room, error) {
return rooms, nil
}
// GetRoomById retrieves a room by its ID from the database.
func GetRoomById(dbPool *pgxpool.Pool, roomId string) (*Room, error) {
var room Room
query := "SELECT id, created_at, userid, roomname, topicname, visible, scale FROM rooms WHERE id = $1"
err := dbPool.QueryRow(context.Background(), query, roomId).Scan(&room.ID, &room.CreatedAt, &room.UserID, &room.RoomName, &room.TopicName, &room.Visible, &room.Scale)
if err != nil {
if err == sql.ErrNoRows {
return nil, fmt.Errorf("no room found with ID %s", roomId)
}
return nil, err
}
return &room, nil
}

View file

@ -77,6 +77,7 @@ func main() {
e.GET("/signin", pages.SignIn)
e.GET("/register", pages.Register)
e.GET("/dashboard", pages.Dashboard, lib.AuthenticatedMiddleware)
e.GET("/room/:id", pages.Room, lib.AuthenticatedMiddleware)
// API Routes:
apiGroup := e.Group("/api")

40
pages/room.go Normal file
View file

@ -0,0 +1,40 @@
package pages
import (
"pollo/lib"
"github.com/labstack/echo/v4"
)
type RoomProps struct {
IsLoggedIn bool
Room lib.Room
}
func Room(c echo.Context) error {
_, error := lib.GetSessionCookie(c.Request(), "session")
if error != nil {
return c.Redirect(302, "/signin")
}
// Get the room ID from the URL
roomID := c.Param("id")
println("Room ID: ", roomID)
// Get the room from the database
room, err := lib.GetRoomById(lib.GetDBPool(), roomID)
if err != nil {
return c.String(404, "Room not found!")
}
props := RoomProps{
IsLoggedIn: lib.IsSignedIn(c),
Room: *room,
}
// Specify the partials used by this page
partials := []string{"header"}
// Render the template
return lib.RenderTemplate(c.Response().Writer, "base", partials, props)
}

27
pages/templates/room.html Normal file
View file

@ -0,0 +1,27 @@
{{define "title"}}
Pollo // Room
{{end}}
{{define "headercontent"}}
Pollo // Room
{{end}}
{{define "head"}}
<link rel="stylesheet" href="/public/css/styles.css" />
{{end}}
{{define "main"}}
<div class="flex flex-col items-center justify-center gap-8">
<div class="text-2xl">{{.Room.RoomName}}</div>
<div class="flex flex-row flex-wrap text-center justify-center items-center gap-1 text-md">
<div>ID:</div>
<div>{{.Room.ID}}</div>
</div>
</div>
{{end}}
{{define "foot"}}
<script src="/public/js/htmx.base.js"></script>
<script src="/public/js/htmx.sse.js"></script>
{{end}}

File diff suppressed because one or more lines are too long