Refactor
This commit is contained in:
+137
-37
@@ -9,13 +9,25 @@ import (
|
||||
)
|
||||
|
||||
func handleNewStoryForm(w http.ResponseWriter, r *http.Request) {
|
||||
roomID := getRoomID(r)
|
||||
room, _ := lib.GetRoomByID(roomID)
|
||||
roomID, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
room, err := lib.GetRoomByID(roomID)
|
||||
if err != nil {
|
||||
http.Error(w, "room not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
renderTemplate(w, "story_form.html", room)
|
||||
}
|
||||
|
||||
func handleAddStory(w http.ResponseWriter, r *http.Request) {
|
||||
id := getRoomID(r)
|
||||
id, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
title := r.FormValue("title")
|
||||
if _, err := lib.CreateStory(id, title); err != nil {
|
||||
@@ -32,12 +44,22 @@ func handleAddStory(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handleSetActiveStory(w http.ResponseWriter, r *http.Request) {
|
||||
id := getRoomID(r)
|
||||
id, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
var sid int
|
||||
fmt.Sscanf(r.FormValue("story_id"), "%d", &sid)
|
||||
sid, err := getFormInt(r, "story_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid story id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
lib.SetActiveStory(id, sid)
|
||||
if err := lib.SetActiveStory(id, sid); err != nil {
|
||||
http.Error(w, "failed to set active story", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
broadcast(id, "stories")
|
||||
broadcast(id, "members")
|
||||
@@ -49,13 +71,26 @@ func handleSetActiveStory(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handleResetStory(w http.ResponseWriter, r *http.Request) {
|
||||
id := getRoomID(r)
|
||||
id, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
var sid int
|
||||
fmt.Sscanf(r.PathValue("story_id"), "%d", &sid)
|
||||
sid, err := getPathInt(r, "story_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid story id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
lib.UnrevealStory(sid)
|
||||
lib.ClearVotesForStory(sid)
|
||||
if err := lib.UnrevealStory(sid); err != nil {
|
||||
http.Error(w, "failed to reset story", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if err := lib.ClearVotesForStory(sid); err != nil {
|
||||
http.Error(w, "failed to clear votes", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
broadcast(id, "stories")
|
||||
broadcast(id, "members")
|
||||
@@ -67,13 +102,23 @@ func handleResetStory(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handleVote(w http.ResponseWriter, r *http.Request) {
|
||||
id := getRoomID(r)
|
||||
id, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
value := r.FormValue("value")
|
||||
var sid int
|
||||
fmt.Sscanf(r.FormValue("story_id"), "%d", &sid)
|
||||
sid, err := getFormInt(r, "story_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid story id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
lib.VoteOnStory(sid, user.ID, value)
|
||||
if err := lib.VoteOnStory(sid, user.ID, value); err != nil {
|
||||
http.Error(w, "failed to vote", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
broadcast(id, "members")
|
||||
story, storyErr := lib.GetStoryByID(sid)
|
||||
if storyErr == nil && story.Voted {
|
||||
@@ -81,11 +126,15 @@ func handleVote(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if isHTMX(r) {
|
||||
room, _ := lib.GetRoomByID(id)
|
||||
if storyErr != nil {
|
||||
http.Error(w, "story not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
room, err := lib.GetRoomByID(id)
|
||||
if err != nil {
|
||||
http.Error(w, "room not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
tmplData := struct {
|
||||
RoomID int
|
||||
Story lib.Story
|
||||
@@ -105,11 +154,21 @@ func handleVote(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handleReveal(w http.ResponseWriter, r *http.Request) {
|
||||
id := getRoomID(r)
|
||||
id, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
var sid int
|
||||
fmt.Sscanf(r.FormValue("story_id"), "%d", &sid)
|
||||
lib.RevealVotes(sid)
|
||||
sid, err := getFormInt(r, "story_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid story id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := lib.RevealVotes(sid); err != nil {
|
||||
http.Error(w, "failed to reveal votes", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
broadcast(id, "stories")
|
||||
if isHTMX(r) {
|
||||
renderRoomStories(w, id, user)
|
||||
@@ -119,11 +178,21 @@ func handleReveal(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handleUnrevealStory(w http.ResponseWriter, r *http.Request) {
|
||||
id := getRoomID(r)
|
||||
id, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
var sid int
|
||||
fmt.Sscanf(r.PathValue("story_id"), "%d", &sid)
|
||||
lib.UnrevealStory(sid)
|
||||
sid, err := getPathInt(r, "story_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid story id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := lib.UnrevealStory(sid); err != nil {
|
||||
http.Error(w, "failed to hide votes", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
broadcast(id, "stories")
|
||||
if isHTMX(r) {
|
||||
renderRoomStories(w, id, user)
|
||||
@@ -133,10 +202,21 @@ func handleUnrevealStory(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
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)
|
||||
roomID, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
storyID, err := getPathInt(r, "story_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid story id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
story, err := lib.GetStoryByID(storyID)
|
||||
if err != nil {
|
||||
http.Error(w, "story not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
data := struct {
|
||||
RoomID int
|
||||
Story lib.Story
|
||||
@@ -148,12 +228,22 @@ func handleEditStoryForm(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handleRenameStory(w http.ResponseWriter, r *http.Request) {
|
||||
id := getRoomID(r)
|
||||
id, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
var sid int
|
||||
fmt.Sscanf(r.PathValue("story_id"), "%d", &sid)
|
||||
sid, err := getPathInt(r, "story_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid story id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
title := r.FormValue("title")
|
||||
lib.RenameStory(sid, title)
|
||||
if err := lib.RenameStory(sid, title); err != nil {
|
||||
http.Error(w, "failed to rename story", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
broadcast(id, "stories")
|
||||
if isHTMX(r) {
|
||||
renderRoomStories(w, id, user)
|
||||
@@ -163,11 +253,21 @@ func handleRenameStory(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func handleDeleteStory(w http.ResponseWriter, r *http.Request) {
|
||||
id := getRoomID(r)
|
||||
id, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
var sid int
|
||||
fmt.Sscanf(r.PathValue("story_id"), "%d", &sid)
|
||||
lib.DeleteStory(sid)
|
||||
sid, err := getPathInt(r, "story_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid story id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := lib.DeleteStory(sid); err != nil {
|
||||
http.Error(w, "failed to delete story", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
broadcast(id, "stories")
|
||||
if isHTMX(r) {
|
||||
renderRoomStories(w, id, user)
|
||||
|
||||
Reference in New Issue
Block a user