diff options
author | jan <jan@ruken.pw> | 2016-01-16 21:28:59 (UTC) |
---|---|---|
committer | jan <jan@ruken.pw> | 2016-01-16 21:28:59 (UTC) |
commit | 5812371696b716417f75eb0ba98ff14a03a5d0ad (patch) | |
tree | 50645b266d71bf45f885c44a695115462b25c969 /modules | |
parent | df4c2d2cd5bf222e664167abc8c91e286e5b2c3a (diff) |
simple user view (bsp. /user/1 bzw /user/1/Admin)
Diffstat (limited to 'modules')
-rw-r--r-- | modules/lists/lists.go | 6 | ||||
-rw-r--r-- | modules/user/user.go | 71 |
2 files changed, 74 insertions, 3 deletions
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) { | |||
159 | return lists[0], nil | 159 | return lists[0], nil |
160 | } | 160 | } |
161 | 161 | ||
162 | func listsToCards(lists []*List) []frontend.Card { | 162 | func ListsToCards(lists []*List) []frontend.Card { |
163 | var cards []frontend.Card | 163 | var cards []frontend.Card |
164 | 164 | ||
165 | for _, list := range lists { | 165 | for _, list := range lists { |
@@ -200,7 +200,7 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar | |||
200 | 200 | ||
201 | categories = append(categories, grilist.DashboardCategory{ | 201 | categories = append(categories, grilist.DashboardCategory{ |
202 | Title: "Neueste Listen", | 202 | Title: "Neueste Listen", |
203 | Cards: listsToCards(lists), | 203 | Cards: ListsToCards(lists), |
204 | }) | 204 | }) |
205 | 205 | ||
206 | if user == nil { | 206 | if user == nil { |
@@ -216,7 +216,7 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar | |||
216 | 216 | ||
217 | categories = append(categories, grilist.DashboardCategory{ | 217 | categories = append(categories, grilist.DashboardCategory{ |
218 | Title: "Meine Listen", | 218 | Title: "Meine Listen", |
219 | Cards: listsToCards(lists), | 219 | Cards: ListsToCards(lists), |
220 | }) | 220 | }) |
221 | 221 | ||
222 | return categories | 222 | 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 @@ | |||
1 | package user | ||
2 | |||
3 | import ( | ||
4 | "log" | ||
5 | "net/http" | ||
6 | "strconv" | ||
7 | |||
8 | "fagott.pw/charakterin" | ||
9 | "fagott.pw/grilist/grilist" | ||
10 | "fagott.pw/grilist/modules/lists" | ||
11 | |||
12 | "github.com/julienschmidt/httprouter" | ||
13 | ) | ||
14 | |||
15 | type Module struct { | ||
16 | g *grilist.Grilist | ||
17 | lists *lists.Module | ||
18 | } | ||
19 | |||
20 | func New() *Module { | ||
21 | return &Module{} | ||
22 | } | ||
23 | |||
24 | func (m *Module) Name() string { | ||
25 | return "User" | ||
26 | } | ||
27 | |||
28 | func (m *Module) Init(g *grilist.Grilist) { | ||
29 | m.g = g | ||
30 | |||
31 | gm, ok := g.Modules["Lists"] | ||
32 | if !ok { | ||
33 | log.Fatal("tags: lists module not found") | ||
34 | } | ||
35 | m.lists = gm.(*lists.Module) | ||
36 | |||
37 | m.g.Router.GET("/user/:id", m.viewUser) | ||
38 | m.g.Router.GET("/user/:id/*rest", m.viewUser) | ||
39 | } | ||
40 | |||
41 | func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { | ||
42 | return make([]grilist.DashboardCategory, 0) | ||
43 | } | ||
44 | |||
45 | func (m *Module) viewUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | ||
46 | currentUser, _ := m.g.Charakterin.GetUserFromRequest(r) | ||
47 | |||
48 | userID, err := strconv.Atoi(p.ByName("id")) | ||
49 | if err != nil { | ||
50 | http.Redirect(w, r, "/", 302) | ||
51 | return | ||
52 | } | ||
53 | |||
54 | user := currentUser | ||
55 | if currentUser.ID != userID { | ||
56 | user, err = m.g.Charakterin.GetUserByID(userID) | ||
57 | if err != nil { | ||
58 | http.Redirect(w, r, "/", 302) | ||
59 | return | ||
60 | } | ||
61 | } | ||
62 | |||
63 | userLists := m.lists.GetUserLists(user) | ||
64 | |||
65 | data := m.g.Renderer.DefaultData() | ||
66 | data["user"] = currentUser | ||
67 | data["DisplayUser"] = user | ||
68 | data["Lists"] = lists.ListsToCards(userLists) | ||
69 | |||
70 | m.g.Renderer.RenderPage("user", w, data) | ||
71 | } \ No newline at end of file | ||