/
router.go
148 lines (124 loc) · 3.12 KB
/
router.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package app
import (
"net/http"
"sort"
"strings"
"github.com/google/uuid"
"github.com/gocopper/copper/cerrors"
"github.com/gocopper/copper/chttp"
"github.com/gocopper/copper/clogger"
"github.com/gocopper/examples/hackernews/pkg/posts"
)
type NewRouterParams struct {
Posts *posts.Queries
RW *chttp.ReaderWriter
Logger clogger.Logger
}
func NewRouter(p NewRouterParams) *Router {
return &Router{
posts: p.Posts,
rw: p.RW,
logger: p.Logger,
}
}
type Router struct {
posts *posts.Queries
rw *chttp.ReaderWriter
logger clogger.Logger
}
func (ro *Router) Routes() []chttp.Route {
return []chttp.Route{
{
Path: "/",
Methods: []string{http.MethodGet},
Handler: ro.HandleIndexPage,
},
{
Path: "/submit",
Methods: []string{http.MethodGet},
Handler: ro.HandleSubmitPage,
},
{
Path: "/submit",
Methods: []string{http.MethodPost},
Handler: ro.HandleSubmitPost,
},
{
Path: "/posts/{id}/vote",
Methods: []string{http.MethodPost},
Handler: ro.HandleSubmitVote,
},
}
}
func (ro *Router) HandleIndexPage(w http.ResponseWriter, r *http.Request) {
allPosts, err := ro.posts.ListPosts(r.Context())
if err != nil {
ro.rw.WriteHTMLError(w, r, cerrors.New(err, "failed to list posts", nil))
return
}
voteCountByPostIDs, err := ro.posts.VoteCountByPostIDs(r.Context(), posts.PostIDs(allPosts))
if err != nil {
ro.rw.WriteHTMLError(w, r, cerrors.New(err, "failed to count votes for all posts", nil))
return
}
for i := range allPosts {
allPosts[i].Votes = voteCountByPostIDs[allPosts[i].ID]
}
sort.Slice(allPosts, func(i, j int) bool {
return allPosts[i].Votes > allPosts[j].Votes
})
for i := range allPosts {
allPosts[i].Rank = int64(i + 1)
}
ro.rw.WriteHTML(w, r, chttp.WriteHTMLParams{
PageTemplate: "index.html",
Data: map[string][]posts.Post{
"Posts": allPosts,
},
})
}
func (ro *Router) HandleSubmitPage(w http.ResponseWriter, r *http.Request) {
ro.rw.WriteHTML(w, r, chttp.WriteHTMLParams{
PageTemplate: "submit.html",
})
}
func (ro *Router) HandleSubmitPost(w http.ResponseWriter, r *http.Request) {
var (
title = strings.TrimSpace(r.PostFormValue("title"))
url = strings.TrimSpace(r.PostFormValue("url"))
)
if title == "" || url == "" {
ro.rw.WriteHTMLError(w, r, cerrors.New(nil, "invalid post", map[string]interface{}{
"form": r.Form,
}))
return
}
err := ro.posts.SavePost(r.Context(), &posts.Post{
ID: uuid.New().String(),
Title: title,
URL: url,
Poster: posts.User,
})
if err != nil {
ro.rw.WriteHTMLError(w, r, cerrors.New(err, "failed to save post", map[string]interface{}{
"form": r.Form,
}))
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func (ro *Router) HandleSubmitVote(w http.ResponseWriter, r *http.Request) {
var postID = chttp.URLParams(r)["id"]
err := ro.posts.SaveVote(r.Context(), &posts.Vote{
ID: uuid.New().String(),
PostID: postID,
User: posts.User,
})
if err != nil {
ro.rw.WriteHTMLError(w, r, cerrors.New(err, "failed to save vote", map[string]interface{}{
"postID": postID,
}))
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}