Refactor
This commit is contained in:
+45
-9
@@ -35,37 +35,64 @@ func handleJoinRoom(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "room not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
lib.AddUserToRoom(room.ID, user.ID)
|
||||
if err := lib.AddUserToRoom(room.ID, user.ID); err != nil {
|
||||
http.Error(w, "failed to join room", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, fmt.Sprintf("/rooms/%d", room.ID), http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func handleRoom(w http.ResponseWriter, r *http.Request) {
|
||||
roomID := getRoomID(r)
|
||||
roomID, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
room, err := lib.GetRoomByID(roomID)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
lib.AddUserToRoom(room.ID, user.ID)
|
||||
if err := lib.AddUserToRoom(room.ID, user.ID); err != nil {
|
||||
http.Error(w, "failed to join room", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
renderTemplate(w, "room.html", buildRoomData(room, user))
|
||||
data, err := buildRoomData(room, user)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to load room data", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
renderTemplate(w, "room.html", data)
|
||||
}
|
||||
|
||||
func handlePartialStories(w http.ResponseWriter, r *http.Request) {
|
||||
roomID := getRoomID(r)
|
||||
roomID, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
renderRoomStories(w, roomID, user)
|
||||
}
|
||||
|
||||
func handlePartialMembers(w http.ResponseWriter, r *http.Request) {
|
||||
roomID := getRoomID(r)
|
||||
roomID, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
renderRoomMembers(w, roomID, user)
|
||||
}
|
||||
|
||||
func handlePartialVoteArea(w http.ResponseWriter, r *http.Request) {
|
||||
roomID := getRoomID(r)
|
||||
roomID, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
room, err := lib.GetRoomByID(roomID)
|
||||
if err != nil {
|
||||
@@ -83,18 +110,27 @@ func handlePartialVoteArea(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
roomData, err := buildRoomData(room, user)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to load room data", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data := struct {
|
||||
RoomData RoomData
|
||||
Story lib.Story
|
||||
}{
|
||||
RoomData: buildRoomData(room, user),
|
||||
RoomData: roomData,
|
||||
Story: *story,
|
||||
}
|
||||
renderTemplate(w, "vote-area", data)
|
||||
}
|
||||
|
||||
func handleDeleteRoom(w http.ResponseWriter, r *http.Request) {
|
||||
roomID := getRoomID(r)
|
||||
roomID, err := getRoomID(r)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
|
||||
room, err := lib.GetRoomByID(roomID)
|
||||
|
||||
Reference in New Issue
Block a user