Skip to content

Commit e49eb96

Browse files
committed
Added user path
1 parent 50522d6 commit e49eb96

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

Diff for: models/update.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func NewUpdate(userId int64, body string) (*Update, error) {
1919
pipe.HSet(key, "user_id", userId)
2020
pipe.HSet(key, "body", body)
2121
pipe.LPush("updates", id)
22+
pipe.LPush(fmt.Sprintf("user:%d:updates", userId), id)
2223
_, err = pipe.Exec()
2324
if err != nil {
2425
return nil, err
@@ -38,8 +39,7 @@ func (update *Update) GetUser() (*User, error) {
3839
return GetUserById(userId)
3940
}
4041

41-
42-
func GetUpdates() ([]*Update, error) {
42+
func GetAllUpdates() ([]*Update, error) {
4343
updateIds, err := client.LRange("updates", 0, 10).Result()
4444
if err != nil {
4545
return nil, err
@@ -52,6 +52,20 @@ func GetUpdates() ([]*Update, error) {
5252
return updates, nil
5353
}
5454

55+
func GetUpdates(userId int64) ([]*Update, error) {
56+
key := fmt.Sprintf("user:%d:updates", userId)
57+
updateIds, err := client.LRange(key, 0, 10).Result()
58+
if err != nil {
59+
return nil, err
60+
}
61+
updates := make([]*Update, len(updateIds))
62+
for i, id := range updateIds {
63+
key := "update:" + id
64+
updates[i] = &Update{key}
65+
}
66+
return updates, nil
67+
}
68+
5569
func PostUpdate(userId int64, body string) error {
5670
_, err := NewUpdate(userId, body)
5771
return err

Diff for: routes/routes.go

+40-2
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,25 @@ func NewRouter() *mux.Router {
1919
r.HandleFunc("/register", registerPostHandler).Methods("POST")
2020
fs := http.FileServer(http.Dir("./static/"))
2121
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
22+
r.HandleFunc("/{username}",
23+
middleware.AuthRequired(userGetHandler)).Methods("GET")
2224
return r
2325
}
2426

2527
func indexGetHandler(w http.ResponseWriter, r *http.Request) {
26-
updates, err := models.GetUpdates()
28+
updates, err := models.GetAllUpdates()
2729
if err != nil {
2830
w.WriteHeader(http.StatusInternalServerError)
2931
w.Write([]byte("Internal server error"))
3032
return
3133
}
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+
})
3341
}
3442

3543
func indexPostHandler(w http.ResponseWriter, r *http.Request) {
@@ -52,6 +60,36 @@ func indexPostHandler(w http.ResponseWriter, r *http.Request) {
5260
http.Redirect(w, r, "/", 302)
5361
}
5462

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+
5593
func loginGetHandler(w http.ResponseWriter, r *http.Request) {
5694
utils.ExecuteTemplate(w, "login.html", nil)
5795
}

Diff for: templates/index.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<html>
22
<head>
3-
<title>Updates</title>
3+
<title>{{ .Title }}</title>
44
<link rel="stylesheet" type="text/css" href="/static/index.css">
55
</head>
66
<body>
7-
<h1>Updates</h1>
7+
<h1>{{ .Title }}</h1>
88
<form method="POST">
99
<textarea name="update"></textarea>
1010
<div>
1111
<button type="submit">Post Update</button>
1212
</div>
1313
</form>
14-
{{ range . }}
14+
{{ range .Updates }}
1515
<div>
1616
<div>
1717
<strong>{{ .GetUser.GetUsername }} wrote:</strong>

0 commit comments

Comments
 (0)