@@ -19,17 +19,25 @@ func NewRouter() *mux.Router {
19
19
r .HandleFunc ("/register" , registerPostHandler ).Methods ("POST" )
20
20
fs := http .FileServer (http .Dir ("./static/" ))
21
21
r .PathPrefix ("/static/" ).Handler (http .StripPrefix ("/static/" , fs ))
22
+ r .HandleFunc ("/{username}" ,
23
+ middleware .AuthRequired (userGetHandler )).Methods ("GET" )
22
24
return r
23
25
}
24
26
25
27
func indexGetHandler (w http.ResponseWriter , r * http.Request ) {
26
- updates , err := models .GetUpdates ()
28
+ updates , err := models .GetAllUpdates ()
27
29
if err != nil {
28
30
w .WriteHeader (http .StatusInternalServerError )
29
31
w .Write ([]byte ("Internal server error" ))
30
32
return
31
33
}
32
- utils .ExecuteTemplate (w , "index.html" , updates )
34
+ utils .ExecuteTemplate (w , "index.html" , struct {
35
+ Title string
36
+ Updates []* models.Update
37
+ } {
38
+ Title : "All updates" ,
39
+ Updates : updates ,
40
+ })
33
41
}
34
42
35
43
func indexPostHandler (w http.ResponseWriter , r * http.Request ) {
@@ -52,6 +60,36 @@ func indexPostHandler(w http.ResponseWriter, r *http.Request) {
52
60
http .Redirect (w , r , "/" , 302 )
53
61
}
54
62
63
+ func userGetHandler (w http.ResponseWriter , r * http.Request ) {
64
+ vars := mux .Vars (r )
65
+ username := vars ["username" ]
66
+ user , err := models .GetUserByUsername (username )
67
+ if err != nil {
68
+ w .WriteHeader (http .StatusInternalServerError )
69
+ w .Write ([]byte ("Internal server error" ))
70
+ return
71
+ }
72
+ userId , err := user .GetId ()
73
+ if err != nil {
74
+ w .WriteHeader (http .StatusInternalServerError )
75
+ w .Write ([]byte ("Internal server error" ))
76
+ return
77
+ }
78
+ updates , err := models .GetUpdates (userId )
79
+ if err != nil {
80
+ w .WriteHeader (http .StatusInternalServerError )
81
+ w .Write ([]byte ("Internal server error" ))
82
+ return
83
+ }
84
+ utils .ExecuteTemplate (w , "index.html" , struct {
85
+ Title string
86
+ Updates []* models.Update
87
+ } {
88
+ Title : username ,
89
+ Updates : updates ,
90
+ })
91
+ }
92
+
55
93
func loginGetHandler (w http.ResponseWriter , r * http.Request ) {
56
94
utils .ExecuteTemplate (w , "login.html" , nil )
57
95
}
0 commit comments