From 01ca54ce6f2e3f7bd75662ec97cbbfb36fdadc7b Mon Sep 17 00:00:00 2001 From: Atridad Lahiji Date: Tue, 28 Apr 2026 15:47:21 -0600 Subject: [PATCH] Delete rooms done --- api/rooms.go | 25 +++++++++++++++++++++++++ api/routes.go | 1 + lib/room.go | 6 ++++++ templates/index.html | 30 ++++++++++++++++++++++++++---- 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/api/rooms.go b/api/rooms.go index 987d3d8..d9984ff 100644 --- a/api/rooms.go +++ b/api/rooms.go @@ -92,3 +92,28 @@ func handlePartialVoteArea(w http.ResponseWriter, r *http.Request) { } renderTemplate(w, "vote-area", data) } + +func handleDeleteRoom(w http.ResponseWriter, r *http.Request) { + roomID := getRoomID(r) + user := r.Context().Value(userKey).(*lib.User) + + room, err := lib.GetRoomByID(roomID) + if err != nil { + http.Error(w, "room not found", http.StatusNotFound) + return + } + + if room.OwnerID != user.ID { + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } + + err = lib.DeleteRoom(roomID) + if err != nil { + log.Printf("delete room error: %v", err) + http.Error(w, "failed to delete room", http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusOK) +} diff --git a/api/routes.go b/api/routes.go index 5143944..cb308bd 100644 --- a/api/routes.go +++ b/api/routes.go @@ -12,6 +12,7 @@ func SetupRoutes(mux *http.ServeMux) { mux.HandleFunc("/rooms/create", requireAuth(handleCreateRoom)) mux.HandleFunc("/rooms/join", requireAuth(handleJoinRoom)) mux.HandleFunc("/rooms/{id}", requireAuth(handleRoom)) + mux.HandleFunc("/rooms/{id}/delete", requireAuth(handleDeleteRoom)) mux.HandleFunc("/rooms/{id}/partial/stories", requireAuth(handlePartialStories)) mux.HandleFunc("/rooms/{id}/partial/members", requireAuth(handlePartialMembers)) diff --git a/lib/room.go b/lib/room.go index 63308ec..f345b75 100644 --- a/lib/room.go +++ b/lib/room.go @@ -129,3 +129,9 @@ func GetRoomMembers(roomID int) ([]User, error) { } return members, nil } + +// delete room by id +func DeleteRoom(id int) error { + _, err := DB.Exec("DELETE FROM rooms WHERE id = ?", id) + return err +} diff --git a/templates/index.html b/templates/index.html index 392eceb..4024fdb 100644 --- a/templates/index.html +++ b/templates/index.html @@ -50,14 +50,16 @@ {{if .Rooms}}
{{range .Rooms}} - -
+
+ +
{{.Name}}
{{if .IsOwner}} @@ -73,15 +75,35 @@ {{end}} {{.Code}} + {{if .IsOwner}} + + {{end}}
-
+
{{.Scale}} {{.MemberCount}} members
- +
{{end}}
{{else}}