From 5812371696b716417f75eb0ba98ff14a03a5d0ad Mon Sep 17 00:00:00 2001 From: jan Date: Sat, 16 Jan 2016 22:28:59 +0100 Subject: simple user view (bsp. /user/1 bzw /user/1/Admin) diff --git a/main.go b/main.go index 92cba1f..d657a4c 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "fagott.pw/grilist/modules/lists" "fagott.pw/grilist/modules/search" "fagott.pw/grilist/modules/tags" + "fagott.pw/grilist/modules/user" "github.com/julienschmidt/httprouter" _ "github.com/lib/pq" @@ -77,6 +78,7 @@ func main() { loadModule(lists.New()) loadModule(tags.New()) loadModule(search.New()) + loadModule(user.New()) log.Fatal(http.ListenAndServe(":8080", nil)) } diff --git a/modules/lists/lists.go b/modules/lists/lists.go index 85e6ce5..ec29ea8 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go @@ -159,7 +159,7 @@ func (m *Module) FromID(id int) (*List, error) { return lists[0], nil } -func listsToCards(lists []*List) []frontend.Card { +func ListsToCards(lists []*List) []frontend.Card { var cards []frontend.Card for _, list := range lists { @@ -200,7 +200,7 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar categories = append(categories, grilist.DashboardCategory{ Title: "Neueste Listen", - Cards: listsToCards(lists), + Cards: ListsToCards(lists), }) if user == nil { @@ -216,7 +216,7 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar categories = append(categories, grilist.DashboardCategory{ Title: "Meine Listen", - Cards: listsToCards(lists), + Cards: ListsToCards(lists), }) return categories diff --git a/modules/user/user.go b/modules/user/user.go new file mode 100644 index 0000000..2645919 --- /dev/null +++ b/modules/user/user.go @@ -0,0 +1,71 @@ +package user + +import ( + "log" + "net/http" + "strconv" + + "fagott.pw/charakterin" + "fagott.pw/grilist/grilist" + "fagott.pw/grilist/modules/lists" + + "github.com/julienschmidt/httprouter" +) + +type Module struct { + g *grilist.Grilist + lists *lists.Module +} + +func New() *Module { + return &Module{} +} + +func (m *Module) Name() string { + return "User" +} + +func (m *Module) Init(g *grilist.Grilist) { + m.g = g + + gm, ok := g.Modules["Lists"] + if !ok { + log.Fatal("tags: lists module not found") + } + m.lists = gm.(*lists.Module) + + m.g.Router.GET("/user/:id", m.viewUser) + m.g.Router.GET("/user/:id/*rest", m.viewUser) +} + +func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { + return make([]grilist.DashboardCategory, 0) +} + +func (m *Module) viewUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + currentUser, _ := m.g.Charakterin.GetUserFromRequest(r) + + userID, err := strconv.Atoi(p.ByName("id")) + if err != nil { + http.Redirect(w, r, "/", 302) + return + } + + user := currentUser + if currentUser.ID != userID { + user, err = m.g.Charakterin.GetUserByID(userID) + if err != nil { + http.Redirect(w, r, "/", 302) + return + } + } + + userLists := m.lists.GetUserLists(user) + + data := m.g.Renderer.DefaultData() + data["user"] = currentUser + data["DisplayUser"] = user + data["Lists"] = lists.ListsToCards(userLists) + + m.g.Renderer.RenderPage("user", w, data) +} \ No newline at end of file diff --git a/views/navbar.html b/views/navbar.html index c456112..a0db664 100644 --- a/views/navbar.html +++ b/views/navbar.html @@ -5,7 +5,7 @@