@@ -35,6 +35,12 @@ type PageQuery struct {
3535 CategoryName string `uri:"category_name"`
3636}
3737
38+ type SearchQuery struct {
39+ Keyword string `form:"keyword,omitempty"`
40+ Label string `form:"label,omitempty"`
41+ Categories []string `form:"categories,omitempty"`
42+ }
43+
3844type PostQuery struct {
3945 Id uint64 `uri:"id" binding:"required"`
4046 Title string `uri:"title" binding:"required"`
@@ -135,9 +141,46 @@ func FetchPosts(c *gin.Context) {
135141 }
136142
137143 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 ,
141184 })
142185}
143186
@@ -167,6 +210,21 @@ func FetchPost(c *gin.Context) {
167210 })
168211}
169212
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+
170228func AboutPage (c * gin.Context ) {
171229 discussion , err := api .FetchPost (config .About )
172230 if err != nil {
@@ -226,6 +284,8 @@ func main() {
226284 r .GET ("/" , cache .CacheByRequestURI (memoryCache , 30 * time .Second ), FetchPosts )
227285 r .GET ("/category/:category_id/:category_name" , cache .CacheByRequestURI (memoryCache , 30 * time .Second ), FetchPosts )
228286 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 )
229289 r .GET ("/atom.xml" , cache .CacheByRequestURI (memoryCache , 24 * time .Hour ), GenerateFeed )
230290 r .GET ("/404" , func (ctx * gin.Context ) {
231291 ctx .HTML (http .StatusOK , "error.html" , nil )
0 commit comments