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 | |
parent | df4c2d2cd5bf222e664167abc8c91e286e5b2c3a (diff) |
simple user view (bsp. /user/1 bzw /user/1/Admin)
-rw-r--r-- | main.go | 2 | ||||
-rw-r--r-- | modules/lists/lists.go | 6 | ||||
-rw-r--r-- | modules/user/user.go | 71 | ||||
-rw-r--r-- | views/navbar.html | 2 | ||||
-rw-r--r-- | views/user.html | 22 |
5 files changed, 99 insertions, 4 deletions
@@ -13,6 +13,7 @@ import ( | |||
13 | "fagott.pw/grilist/modules/lists" | 13 | "fagott.pw/grilist/modules/lists" |
14 | "fagott.pw/grilist/modules/search" | 14 | "fagott.pw/grilist/modules/search" |
15 | "fagott.pw/grilist/modules/tags" | 15 | "fagott.pw/grilist/modules/tags" |
16 | "fagott.pw/grilist/modules/user" | ||
16 | 17 | ||
17 | "github.com/julienschmidt/httprouter" | 18 | "github.com/julienschmidt/httprouter" |
18 | _ "github.com/lib/pq" | 19 | _ "github.com/lib/pq" |
@@ -77,6 +78,7 @@ func main() { | |||
77 | loadModule(lists.New()) | 78 | loadModule(lists.New()) |
78 | loadModule(tags.New()) | 79 | loadModule(tags.New()) |
79 | loadModule(search.New()) | 80 | loadModule(search.New()) |
81 | loadModule(user.New()) | ||
80 | 82 | ||
81 | log.Fatal(http.ListenAndServe(":8080", nil)) | 83 | log.Fatal(http.ListenAndServe(":8080", nil)) |
82 | } | 84 | } |
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 | ||
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 @@ | |||
5 | <ul id="nav-mobile" class="right hide-on-med-and-down"> | 5 | <ul id="nav-mobile" class="right hide-on-med-and-down"> |
6 | {{ if .user }} | 6 | {{ if .user }} |
7 | <li><a href="/new/list">Liste erstellen</a></li> | 7 | <li><a href="/new/list">Liste erstellen</a></li> |
8 | <li><a href="/">{{ .user.GetName }}</a></li> | 8 | <li><a href="/user/{{ .user.ID }}/{{ .user.GetName }}">{{ .user.GetName }}</a></li> |
9 | <li><a href="/logout">Ausloggen</a></li> | 9 | <li><a href="/logout">Ausloggen</a></li> |
10 | {{ else }} | 10 | {{ else }} |
11 | <li><a href="/login">Einloggen</a></li> | 11 | <li><a href="/login">Einloggen</a></li> |
diff --git a/views/user.html b/views/user.html new file mode 100644 index 0000000..19ffff6 --- /dev/null +++ b/views/user.html | |||
@@ -0,0 +1,22 @@ | |||
1 | {{ define "user" }} | ||
2 | <html> | ||
3 | <head> | ||
4 | {{ template "materialize" }} | ||
5 | <title>{{ .DisplayUser.Name }} - grilist</title> | ||
6 | <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> | ||
7 | </head> | ||
8 | <body> | ||
9 | {{ template "navbar" . }} | ||
10 | <div class="container"> | ||
11 | <h1>{{ .DisplayUser.Name }}</h1> | ||
12 | <blockquote>zuletzt gesehen: {{ .DisplayUser.LastActivity.Format "Mon Jan _2 15:04:05 2006" }}</blockquote><br /> | ||
13 | <div class="row"> | ||
14 | <h3>Listen von {{ .DisplayUser.Name }}</h3> | ||
15 | {{ range .Lists }} | ||
16 | {{ template "card" . }} | ||
17 | {{ end }} | ||
18 | </div> | ||
19 | </div> | ||
20 | </body> | ||
21 | </html> | ||
22 | {{ end }} | ||