First pass at basic functionality.
This PR introduces the beginnings of Sprint Padawan. Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"sprintpadawan/lib"
|
||||
)
|
||||
|
||||
type RoomView struct {
|
||||
ID int
|
||||
Name string
|
||||
Code string
|
||||
Scale string
|
||||
IsOwner bool
|
||||
MemberCount int
|
||||
}
|
||||
|
||||
func handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
user := r.Context().Value(userKey).(*lib.User)
|
||||
rooms, err := lib.GetRoomSummariesForUser(user.ID)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to load rooms", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data := struct {
|
||||
*lib.User
|
||||
Rooms []RoomView
|
||||
}{
|
||||
User: user,
|
||||
}
|
||||
|
||||
for _, room := range rooms {
|
||||
data.Rooms = append(data.Rooms, RoomView{
|
||||
ID: room.ID,
|
||||
Name: room.Name,
|
||||
Code: room.Code,
|
||||
Scale: room.Scale,
|
||||
IsOwner: room.OwnerID == user.ID,
|
||||
MemberCount: room.MemberCount,
|
||||
})
|
||||
}
|
||||
|
||||
if err := templates.ExecuteTemplate(w, "index.html", data); err != nil {
|
||||
log.Printf("template error: %v", err)
|
||||
http.Error(w, "internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user