@@ -35,6 +35,12 @@ type PageQuery struct {
35
35
CategoryName string `uri:"category_name"`
36
36
}
37
37
38
+ type SearchQuery struct {
39
+ Keyword string `form:"keyword,omitempty"`
40
+ Label string `form:"label,omitempty"`
41
+ Categories []string `form:"categories,omitempty"`
42
+ }
43
+
38
44
type PostQuery struct {
39
45
Id uint64 `uri:"id" binding:"required"`
40
46
Title string `uri:"title" binding:"required"`
@@ -135,9 +141,46 @@ func FetchPosts(c *gin.Context) {
135
141
}
136
142
137
143
c .HTML (http .StatusOK , "index.html" , map [string ]any {
138
- "Posts" : discussions ,
139
- "Navbars" : config .Categories ,
140
- "About" : config .About ,
144
+ "Title" : pageQuery .CategoryName ,
145
+ "Nodes" : discussions .Nodes ,
146
+ "PageInfo" : discussions .PageInfo ,
147
+ "Navbars" : config .Categories ,
148
+ "About" : config .About ,
149
+ })
150
+ }
151
+
152
+ func SearchPosts (c * gin.Context ) {
153
+ var searchQuery SearchQuery
154
+
155
+ if err := c .ShouldBind (& searchQuery ); err != nil {
156
+ c .HTML (http .StatusBadRequest , "error.html" , map [string ]any {
157
+ "Message" : err .Error (),
158
+ })
159
+ return
160
+ }
161
+
162
+ if searchQuery .Label == "" && len (searchQuery .Categories ) == 0 && searchQuery .Keyword == "" {
163
+ c .HTML (http .StatusBadRequest , "error.html" , map [string ]any {
164
+ "Message" : "Invalid Params" ,
165
+ })
166
+ return
167
+ }
168
+
169
+ result , err := api .QueryPosts (searchQuery .Keyword , searchQuery .Label , searchQuery .Categories )
170
+
171
+ if err != nil {
172
+ c .HTML (http .StatusBadRequest , "error.html" , map [string ]any {
173
+ "Message" : err .Error (),
174
+ })
175
+ return
176
+ }
177
+
178
+ c .HTML (http .StatusOK , "index.html" , map [string ]any {
179
+ "Title" : "Search Result" ,
180
+ "Nodes" : result .Nodes ,
181
+ "PageInfo" : result .PageInfo ,
182
+ "Navbars" : config .Categories ,
183
+ "About" : config .About ,
141
184
})
142
185
}
143
186
@@ -167,6 +210,21 @@ func FetchPost(c *gin.Context) {
167
210
})
168
211
}
169
212
213
+ func TagPage (c * gin.Context ) {
214
+ labels , err := api .FetchAllLabels ()
215
+ if err != nil {
216
+ c .HTML (http .StatusBadRequest , "error.html" , map [string ]any {
217
+ "Message" : err .Error (),
218
+ })
219
+ return
220
+ }
221
+ c .HTML (http .StatusOK , "tags.html" , map [string ]any {
222
+ "Labels" : labels ,
223
+ "Navbars" : config .Categories ,
224
+ "About" : config .About ,
225
+ })
226
+ }
227
+
170
228
func AboutPage (c * gin.Context ) {
171
229
discussion , err := api .FetchPost (config .About )
172
230
if err != nil {
@@ -226,6 +284,8 @@ func main() {
226
284
r .GET ("/" , cache .CacheByRequestURI (memoryCache , 30 * time .Second ), FetchPosts )
227
285
r .GET ("/category/:category_id/:category_name" , cache .CacheByRequestURI (memoryCache , 30 * time .Second ), FetchPosts )
228
286
r .GET ("/post/:id/:title" , cache .CacheByRequestURI (memoryCache , 1 * time .Hour ), FetchPost )
287
+ r .GET ("/tags" , cache .CacheByRequestURI (memoryCache , 24 * time .Hour ), TagPage )
288
+ r .GET ("/search" , cache .CacheByRequestURI (memoryCache , 24 * time .Hour ), SearchPosts )
229
289
r .GET ("/atom.xml" , cache .CacheByRequestURI (memoryCache , 24 * time .Hour ), GenerateFeed )
230
290
r .GET ("/404" , func (ctx * gin.Context ) {
231
291
ctx .HTML (http .StatusOK , "error.html" , nil )
0 commit comments