Skip to content

Commit 3ad06df

Browse files
committed
feat: tags pages
1 parent 6795eca commit 3ad06df

File tree

6 files changed

+81
-27
lines changed

6 files changed

+81
-27
lines changed

core/api.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7+
"strings"
78

89
"github.com/shurcooL/githubv4"
910
"golang.org/x/oauth2"
@@ -129,27 +130,28 @@ func (api *BlogApi) FetchCategories(before string, after string) (Categories, er
129130
}
130131

131132
// FetchPostsByLabel 根据 label 和 category 获取对应的 posts
132-
func (api *BlogApi) QueryPosts(keyword *string, label *string, categories *[]string) ([]Node, error) {
133+
func (api *BlogApi) QueryPosts(keyword string, label string, categories []string) (SearchResults, error) {
133134
var q struct {
134135
Search struct {
135-
Nodes []struct {
136+
PageInfo PageInfo
137+
Nodes []struct {
136138
Node `graphql:"... on Discussion"`
137139
}
138140
} `graphql:"search(query: $query first: $first, type: $type)"`
139141
}
140142

141-
var query = fmt.Sprintf("repo:%s ", api.repo)
143+
var query = fmt.Sprintf("repo:%s/%s ", api.owner, api.repo)
142144

143-
if keyword != nil {
144-
query = fmt.Sprintf("%s %s ", query, *keyword)
145+
if len(strings.Trim(keyword, "")) != 0 {
146+
query = fmt.Sprintf("%s %s ", query, keyword)
145147
}
146148

147-
if label != nil {
148-
query = fmt.Sprintf("%s label:\"%s\" ", query, *label)
149+
if len(strings.Trim(label, "")) != 0 {
150+
query = fmt.Sprintf("%s label:\"%s\" ", query, label)
149151
}
150152

151-
if categories != nil {
152-
for _, category := range *categories {
153+
if len(categories) != 0 {
154+
for _, category := range categories {
153155
query = fmt.Sprintf("%s category:\"%s\" ", query, category)
154156
}
155157
}
@@ -163,13 +165,16 @@ func (api *BlogApi) QueryPosts(keyword *string, label *string, categories *[]str
163165

164166
err := api.client.Query(context.Background(), &q, binds)
165167
if err != nil {
166-
return []Node{}, err
168+
return SearchResults{}, err
167169
}
168170
var posts []Node
169171
for _, node := range q.Search.Nodes {
170172
posts = append(posts, node.Node)
171173
}
172-
return posts, nil
174+
return SearchResults{
175+
PageInfo: q.Search.PageInfo,
176+
Nodes: posts,
177+
}, nil
173178
}
174179

175180
// fetch all labels from discussion 获取所有的 labels

core/api_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@ func TestApi(t *testing.T) {
1919
var label = "python"
2020
var keyword = "django"
2121
queryTestCases := []struct {
22-
keyword *string
23-
label *string
24-
categories *[]string
22+
keyword string
23+
label string
24+
categories []string
2525
resultCount int
2626
}{
27-
{&keyword, nil, &[]string{"随笔"}, 3},
28-
{nil, &label, &[]string{"随笔", "历史存档"}, 10},
29-
{&keyword, &label, &[]string{"随笔", "历史存档"}, 4},
27+
{keyword, "", []string{"随笔"}, 3},
28+
{"", label, []string{"随笔", "历史存档"}, 10},
29+
{keyword, label, []string{"随笔", "历史存档"}, 4},
3030
}
3131

3232
for _, queryTestCase := range queryTestCases {
33-
posts, err := api.QueryPosts(queryTestCase.keyword, queryTestCase.label, queryTestCase.categories)
33+
result, err := api.QueryPosts(queryTestCase.keyword, queryTestCase.label, queryTestCase.categories)
34+
posts := result.Nodes
3435
if err != nil {
3536
t.Errorf("QueryPosts error: %v", err)
3637
}

core/model.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,8 @@ type Repies struct {
7171
PageInfo PageInfo `json:"page_info,omitempty"`
7272
Nodes []Comment `json:"nodes,omitempty"`
7373
}
74+
75+
type SearchResults struct {
76+
PageInfo PageInfo `json:"page_info,omitempty"`
77+
Nodes []Node `json:"nodes,omitempty"`
78+
}

main

-15 MB
Binary file not shown.

main.go

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
3844
type PostQuery struct {
3945
Id uint64 `uri:"id" binding:"required"`
4046
Title string `uri:"title" binding:"required"`
@@ -135,10 +141,46 @@ func FetchPosts(c *gin.Context) {
135141
}
136142

137143
c.HTML(http.StatusOK, "index.html", map[string]any{
138-
"Title": pageQuery.CategoryName,
139-
"Posts": discussions,
140-
"Navbars": config.Categories,
141-
"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,
142184
})
143185
}
144186

@@ -243,6 +285,7 @@ func main() {
243285
r.GET("/category/:category_id/:category_name", cache.CacheByRequestURI(memoryCache, 30*time.Second), FetchPosts)
244286
r.GET("/post/:id/:title", cache.CacheByRequestURI(memoryCache, 1*time.Hour), FetchPost)
245287
r.GET("/tags", cache.CacheByRequestURI(memoryCache, 24*time.Hour), TagPage)
288+
r.GET("/search", cache.CacheByRequestURI(memoryCache, 24*time.Hour), SearchPosts)
246289
r.GET("/atom.xml", cache.CacheByRequestURI(memoryCache, 24*time.Hour), GenerateFeed)
247290
r.GET("/404", func(ctx *gin.Context) {
248291
ctx.HTML(http.StatusOK, "error.html", nil)

templates/pages/index.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ <h1 class="text-4xl font-bold tracking-tight text-zinc-800 dark:text-zinc-100 sm
6161
<div class="mx-auto max-w-2xl lg:max-w-5xl">
6262
<div class="mx-auto grid max-w-xl grid-cols-1 gap-y-20 lg:max-w-none lg:grid-cols-1">
6363
<div class="flex flex-col gap-8">
64-
{{range .Posts.Nodes}}
64+
{{range .Nodes}}
6565
{{ if .Category.Id | isExisted }}
6666
<article
6767
class="group relative flex flex-col items-start rounded-xl p-5 bg-white dark:bg-slate-800">
@@ -112,14 +112,14 @@ <h2 class="text-base font-semibold tracking-tight text-zinc-800 dark:text-zinc-1
112112

113113
<div class='flex items-center justify-center mt-10'>
114114
<div class="flex justify-center items-center space-x-4">
115-
{{ if .Posts.PageInfo.HasPreviousPage }}
115+
{{ if .PageInfo.HasPreviousPage }}
116116
<a class="px-2 py-1 text-3xl leading-6 text-slate-400 transition cursor-pointer "
117-
href="/?pre={{.Posts.PageInfo.StartCursor}}">
117+
href="/?pre={{.PageInfo.StartCursor}}">
118118
</a>
119119
{{ end }}
120-
{{ if .Posts.PageInfo.HasNextPage}}
120+
{{ if .PageInfo.HasNextPage}}
121121
<a class="px-2 py-1 text-3xl leading-6 text-slate-400 transition cursor-pointer"
122-
href="/?next={{.Posts.PageInfo.EndCursor}}"> > </a>
122+
href="/?next={{.PageInfo.EndCursor}}"> > </a>
123123
{{ end }}
124124
</div>
125125
</div>

0 commit comments

Comments
 (0)