package api import ( "fmt" "log" "net/http" "sprintpadawan/lib" ) func handleNewStoryForm(w http.ResponseWriter, r *http.Request) { roomID := getRoomID(r) room, _ := lib.GetRoomByID(roomID) renderTemplate(w, "story_form.html", room) } func handleAddStory(w http.ResponseWriter, r *http.Request) { id := getRoomID(r) user := r.Context().Value(userKey).(*lib.User) title := r.FormValue("title") if _, err := lib.CreateStory(id, title); err != nil { log.Printf("create story error: %v", err) http.Error(w, "failed to create story", http.StatusInternalServerError) return } broadcast(id, "stories") if isHTMX(r) { renderRoomStories(w, id, user) return } http.Redirect(w, r, fmt.Sprintf("/rooms/%d", id), http.StatusSeeOther) } func handleSetActiveStory(w http.ResponseWriter, r *http.Request) { id := getRoomID(r) user := r.Context().Value(userKey).(*lib.User) var sid int fmt.Sscanf(r.FormValue("story_id"), "%d", &sid) lib.SetActiveStory(id, sid) broadcast(id, "stories") broadcast(id, "members") if isHTMX(r) { renderRoomStories(w, id, user) return } http.Redirect(w, r, fmt.Sprintf("/rooms/%d", id), http.StatusSeeOther) } func handleResetStory(w http.ResponseWriter, r *http.Request) { id := getRoomID(r) user := r.Context().Value(userKey).(*lib.User) var sid int fmt.Sscanf(r.PathValue("story_id"), "%d", &sid) lib.UnrevealStory(sid) lib.ClearVotesForStory(sid) broadcast(id, "stories") broadcast(id, "members") if isHTMX(r) { renderRoomStories(w, id, user) return } http.Redirect(w, r, fmt.Sprintf("/rooms/%d", id), http.StatusSeeOther) } func handleVote(w http.ResponseWriter, r *http.Request) { id := getRoomID(r) user := r.Context().Value(userKey).(*lib.User) value := r.FormValue("value") var sid int fmt.Sscanf(r.FormValue("story_id"), "%d", &sid) lib.VoteOnStory(sid, user.ID, value) broadcast(id, "members") story, storyErr := lib.GetStoryByID(sid) if storyErr == nil && story.Voted { broadcast(id, "stories") } if isHTMX(r) { room, _ := lib.GetRoomByID(id) if storyErr != nil { http.Error(w, "story not found", http.StatusNotFound) return } tmplData := struct { RoomID int Story lib.Story Scale string UserVote string }{ RoomID: id, Story: *story, Scale: room.Scale, UserVote: value, } renderTemplate(w, "vote_form.html", tmplData) return } http.Redirect(w, r, fmt.Sprintf("/rooms/%d", id), http.StatusSeeOther) } func handleReveal(w http.ResponseWriter, r *http.Request) { id := getRoomID(r) user := r.Context().Value(userKey).(*lib.User) var sid int fmt.Sscanf(r.FormValue("story_id"), "%d", &sid) lib.RevealVotes(sid) broadcast(id, "stories") if isHTMX(r) { renderRoomStories(w, id, user) return } http.Redirect(w, r, fmt.Sprintf("/rooms/%d", id), http.StatusSeeOther) } func handleUnrevealStory(w http.ResponseWriter, r *http.Request) { id := getRoomID(r) user := r.Context().Value(userKey).(*lib.User) var sid int fmt.Sscanf(r.PathValue("story_id"), "%d", &sid) lib.UnrevealStory(sid) broadcast(id, "stories") if isHTMX(r) { renderRoomStories(w, id, user) return } http.Redirect(w, r, fmt.Sprintf("/rooms/%d", id), http.StatusSeeOther) } func handleEditStoryForm(w http.ResponseWriter, r *http.Request) { roomID := getRoomID(r) var storyID int fmt.Sscanf(r.PathValue("story_id"), "%d", &storyID) story, _ := lib.GetStoryByID(storyID) data := struct { RoomID int Story lib.Story }{ RoomID: roomID, Story: *story, } renderTemplate(w, "story_edit.html", data) } func handleRenameStory(w http.ResponseWriter, r *http.Request) { id := getRoomID(r) user := r.Context().Value(userKey).(*lib.User) var sid int fmt.Sscanf(r.PathValue("story_id"), "%d", &sid) title := r.FormValue("title") lib.RenameStory(sid, title) broadcast(id, "stories") if isHTMX(r) { renderRoomStories(w, id, user) return } http.Redirect(w, r, fmt.Sprintf("/rooms/%d", id), http.StatusSeeOther) } func handleDeleteStory(w http.ResponseWriter, r *http.Request) { id := getRoomID(r) user := r.Context().Value(userKey).(*lib.User) var sid int fmt.Sscanf(r.PathValue("story_id"), "%d", &sid) lib.DeleteStory(sid) broadcast(id, "stories") if isHTMX(r) { renderRoomStories(w, id, user) return } http.Redirect(w, r, fmt.Sprintf("/rooms/%d", id), http.StatusSeeOther) }