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/user | |
parent | df4c2d2cd5bf222e664167abc8c91e286e5b2c3a (diff) |
simple user view (bsp. /user/1 bzw /user/1/Admin)
Diffstat (limited to 'modules/user')
-rw-r--r-- | modules/user/user.go | 71 |
1 files changed, 71 insertions, 0 deletions
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 | ||