16bed1b8c0
This PR introduces the beginnings of Sprint Padawan. Reviewed-on: #1
156 lines
3.8 KiB
Go
156 lines
3.8 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
|
|
}
|
|
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, 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
|
|
}
|
|
if err := lib.AddUserToRoom(room.ID, user.ID); err != nil {
|
|
http.Error(w, "failed to join room", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
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, 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, 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, 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.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
|
|
}
|
|
|
|
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: roomData,
|
|
Story: *story,
|
|
}
|
|
renderTemplate(w, "vote-area", data)
|
|
}
|
|
|
|
func handleDeleteRoom(w http.ResponseWriter, r *http.Request) {
|
|
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.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)
|
|
}
|