Skip to content

Commit 50522d6

Browse files
committed
Added update model
1 parent 5987173 commit 50522d6

File tree

6 files changed

+102
-26
lines changed

6 files changed

+102
-26
lines changed

Diff for: middleware/middleware.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
func AuthRequired(handler http.HandlerFunc) http.HandlerFunc {
99
return func(w http.ResponseWriter, r *http.Request) {
1010
session, _ := sessions.Store.Get(r, "session")
11-
_, ok := session.Values["username"]
11+
_, ok := session.Values["user_id"]
1212
if !ok {
1313
http.Redirect(w, r, "/login", 302)
1414
return

Diff for: models/comment.go

-9
This file was deleted.

Diff for: models/update.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package models
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type Update struct {
8+
key string
9+
}
10+
11+
func NewUpdate(userId int64, body string) (*Update, error) {
12+
id, err := client.Incr("update:next-id").Result()
13+
if err != nil {
14+
return nil, err
15+
}
16+
key := fmt.Sprintf("update:%d", id)
17+
pipe := client.Pipeline()
18+
pipe.HSet(key, "id", id)
19+
pipe.HSet(key, "user_id", userId)
20+
pipe.HSet(key, "body", body)
21+
pipe.LPush("updates", id)
22+
_, err = pipe.Exec()
23+
if err != nil {
24+
return nil, err
25+
}
26+
return &Update{key}, nil
27+
}
28+
29+
func (update *Update) GetBody() (string, error) {
30+
return client.HGet(update.key, "body").Result()
31+
}
32+
33+
func (update *Update) GetUser() (*User, error) {
34+
userId, err := client.HGet(update.key, "user_id").Int64()
35+
if err != nil {
36+
return nil, err
37+
}
38+
return GetUserById(userId)
39+
}
40+
41+
42+
func GetUpdates() ([]*Update, error) {
43+
updateIds, err := client.LRange("updates", 0, 10).Result()
44+
if err != nil {
45+
return nil, err
46+
}
47+
updates := make([]*Update, len(updateIds))
48+
for i, id := range updateIds {
49+
key := "update:" + id
50+
updates[i] = &Update{key}
51+
}
52+
return updates, nil
53+
}
54+
55+
func PostUpdate(userId int64, body string) error {
56+
_, err := NewUpdate(userId, body)
57+
return err
58+
}

Diff for: models/user.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ func NewUser(username string, hash []byte) (*User, error) {
3434
return &User{key}, nil
3535
}
3636

37+
func (user *User) GetId() (int64, error) {
38+
return client.HGet(user.key, "id").Int64()
39+
}
40+
3741
func (user *User) GetUsername() (string, error) {
3842
return client.HGet(user.key, "username").Result()
3943
}
@@ -54,23 +58,27 @@ func (user *User) Authenticate(password string) error {
5458
return err
5559
}
5660

61+
func GetUserById(id int64) (*User, error) {
62+
key := fmt.Sprintf("user:%d", id)
63+
return &User{key}, nil
64+
}
65+
5766
func GetUserByUsername(username string) (*User, error) {
5867
id, err := client.HGet("user:by-username", username).Int64()
5968
if err == redis.Nil {
6069
return nil, ErrUserNotFound
6170
} else if err != nil {
6271
return nil, err
6372
}
64-
key := fmt.Sprintf("user:%d", id)
65-
return &User{key}, nil
73+
return GetUserById(id)
6674
}
6775

68-
func AuthenticateUser(username, password string) error {
76+
func AuthenticateUser(username, password string) (*User, error) {
6977
user, err := GetUserByUsername(username)
7078
if err != nil {
71-
return err
79+
return nil, err
7280
}
73-
return user.Authenticate(password)
81+
return user, user.Authenticate(password)
7482
}
7583

7684
func RegisterUser(username, password string) error {

Diff for: routes/routes.go

+20-6
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,27 @@ func NewRouter() *mux.Router {
2323
}
2424

2525
func indexGetHandler(w http.ResponseWriter, r *http.Request) {
26-
comments, err := models.GetComments()
26+
updates, err := models.GetUpdates()
2727
if err != nil {
2828
w.WriteHeader(http.StatusInternalServerError)
2929
w.Write([]byte("Internal server error"))
3030
return
3131
}
32-
utils.ExecuteTemplate(w, "index.html", comments)
32+
utils.ExecuteTemplate(w, "index.html", updates)
3333
}
3434

3535
func indexPostHandler(w http.ResponseWriter, r *http.Request) {
36+
session, _ := sessions.Store.Get(r, "session")
37+
untypedUserId := session.Values["user_id"]
38+
userId, ok := untypedUserId.(int64)
39+
if !ok {
40+
w.WriteHeader(http.StatusInternalServerError)
41+
w.Write([]byte("Internal server error"))
42+
return
43+
}
3644
r.ParseForm()
37-
comment := r.PostForm.Get("comment")
38-
err := models.PostComment(comment)
45+
body := r.PostForm.Get("update")
46+
err := models.PostUpdate(userId, body)
3947
if err != nil {
4048
w.WriteHeader(http.StatusInternalServerError)
4149
w.Write([]byte("Internal server error"))
@@ -52,7 +60,7 @@ func loginPostHandler(w http.ResponseWriter, r *http.Request) {
5260
r.ParseForm()
5361
username := r.PostForm.Get("username")
5462
password := r.PostForm.Get("password")
55-
err := models.AuthenticateUser(username, password)
63+
user, err := models.AuthenticateUser(username, password)
5664
if err != nil {
5765
switch err {
5866
case models.ErrUserNotFound:
@@ -65,8 +73,14 @@ func loginPostHandler(w http.ResponseWriter, r *http.Request) {
6573
}
6674
return
6775
}
76+
userId, err := user.GetId()
77+
if err != nil {
78+
w.WriteHeader(http.StatusInternalServerError)
79+
w.Write([]byte("Internal server error"))
80+
return
81+
}
6882
session, _ := sessions.Store.Get(r, "session")
69-
session.Values["username"] = username
83+
session.Values["user_id"] = userId
7084
session.Save(r, w)
7185
http.Redirect(w, r, "/", 302)
7286
}

Diff for: templates/index.html

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
<html>
22
<head>
3-
<title>Comments</title>
3+
<title>Updates</title>
44
<link rel="stylesheet" type="text/css" href="/static/index.css">
55
</head>
66
<body>
7-
<h1>Comments</h1>
7+
<h1>Updates</h1>
88
<form method="POST">
9-
<textarea name="comment"></textarea>
9+
<textarea name="update"></textarea>
1010
<div>
11-
<button type="submit">Post Comment</button>
11+
<button type="submit">Post Update</button>
1212
</div>
1313
</form>
1414
{{ range . }}
15-
<div>{{ . }}</div>
15+
<div>
16+
<div>
17+
<strong>{{ .GetUser.GetUsername }} wrote:</strong>
18+
</div>
19+
<div>{{ .GetBody }}</div>
20+
</div>
1621
{{ end }}
1722
</body>
1823
</html>

0 commit comments

Comments
 (0)