95 lines
2.4 KiB
Go
95 lines
2.4 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"sprintpadawan/lib"
|
|
)
|
|
|
|
func handleNewRoom(w http.ResponseWriter, r *http.Request) {
|
|
if err := templates.ExecuteTemplate(w, "room_form.html", nil); err != nil {
|
|
log.Printf("template error: %v", err)
|
|
}
|
|
}
|
|
|
|
func handleCreateRoom(w http.ResponseWriter, r *http.Request) {
|
|
user := r.Context().Value(userKey).(*lib.User)
|
|
name := r.FormValue("name")
|
|
scale := r.FormValue("scale")
|
|
room, err := lib.CreateRoom(name, user.ID, scale)
|
|
if err != nil {
|
|
log.Printf("create room error: %v", err)
|
|
http.Error(w, "failed to create room", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
http.Redirect(w, r, fmt.Sprintf("/rooms/%d", room.ID), http.StatusSeeOther)
|
|
}
|
|
|
|
func handleJoinRoom(w http.ResponseWriter, r *http.Request) {
|
|
user := r.Context().Value(userKey).(*lib.User)
|
|
code := r.FormValue("code")
|
|
room, err := lib.GetRoomByCode(code)
|
|
if err != nil {
|
|
http.Error(w, "room not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
lib.AddUserToRoom(room.ID, user.ID)
|
|
http.Redirect(w, r, fmt.Sprintf("/rooms/%d", room.ID), http.StatusSeeOther)
|
|
}
|
|
|
|
func handleRoom(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.NotFound(w, r)
|
|
return
|
|
}
|
|
lib.AddUserToRoom(room.ID, user.ID)
|
|
|
|
renderTemplate(w, "room.html", buildRoomData(room, user))
|
|
}
|
|
|
|
func handlePartialStories(w http.ResponseWriter, r *http.Request) {
|
|
roomID := getRoomID(r)
|
|
user := r.Context().Value(userKey).(*lib.User)
|
|
renderRoomStories(w, roomID, user)
|
|
}
|
|
|
|
func handlePartialMembers(w http.ResponseWriter, r *http.Request) {
|
|
roomID := getRoomID(r)
|
|
user := r.Context().Value(userKey).(*lib.User)
|
|
renderRoomMembers(w, roomID, user)
|
|
}
|
|
|
|
func handlePartialVoteArea(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.ActiveStoryID == nil {
|
|
w.WriteHeader(http.StatusNoContent)
|
|
return
|
|
}
|
|
|
|
story, err := lib.GetStoryByID(*room.ActiveStoryID)
|
|
if err != nil {
|
|
http.Error(w, "story not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
data := struct {
|
|
RoomData RoomData
|
|
Story lib.Story
|
|
}{
|
|
RoomData: buildRoomData(room, user),
|
|
Story: *story,
|
|
}
|
|
renderTemplate(w, "vote-area", data)
|
|
}
|