aboutsummaryrefslogtreecommitdiff
path: root/modules/user
diff options
context:
space:
mode:
Diffstat (limited to 'modules/user')
-rw-r--r--modules/user/user.go71
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 @@
1package user
2
3import (
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
15type Module struct {
16 g *grilist.Grilist
17 lists *lists.Module
18}
19
20func New() *Module {
21 return &Module{}
22}
23
24func (m *Module) Name() string {
25 return "User"
26}
27
28func (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
41func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory {
42 return make([]grilist.DashboardCategory, 0)
43}
44
45func (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