1+ // GetBlogsByTagsHandler 分页获取包含所有指定标签的文章
2+ func (h * BlogHandler ) GetBlogsByTagsHandler (w http.ResponseWriter , r * http.Request ) {
3+ tags := r .URL .Query ()["tag" ] // 支持多个 tag=xxx
4+ pageStr := r .URL .Query ().Get ("page" )
5+ limitStr := r .URL .Query ().Get ("limit" )
6+ page := int64 (1 )
7+ limit := int64 (10 )
8+ if pageStr != "" {
9+ if p , err := strconv .ParseInt (pageStr , 10 , 64 ); err == nil && p > 0 {
10+ page = p
11+ }
12+ }
13+ if limitStr != "" {
14+ if l , err := strconv .ParseInt (limitStr , 10 , 64 ); err == nil && l > 0 && l <= 100 {
15+ limit = l
16+ }
17+ }
18+ blogs , total , err := h .blogService .GetBlogsByTagsWithPagination (tags , page , limit )
19+ if err != nil {
20+ http .Error (w , "获取文章失败" , http .StatusInternalServerError )
21+ return
22+ }
23+ resp := BlogListResponse {
24+ Data : blogs ,
25+ Pagination : Pagination {Page : page , Limit : limit , Total : total },
26+ }
27+ w .Header ().Set ("Content-Type" , "application/json" )
28+ json .NewEncoder (w ).Encode (resp )
29+ }
130package handlers
231
332import (
@@ -11,11 +40,90 @@ import (
1140 "github.com/gorilla/mux"
1241)
1342
43+ // 标签相关请求体
44+ type TagRequest struct {
45+ Tag string `json:"tag"`
46+ }
47+
1448// BlogHandler 处理博客文章的HTTP请求
1549type BlogHandler struct {
1650 blogService * services.BlogService
1751}
1852
53+ // SearchBlogsHandler 支持模糊搜索和标签筛选
54+ func (h * BlogHandler ) SearchBlogsHandler (w http.ResponseWriter , r * http.Request ) {
55+ keyword := r .URL .Query ().Get ("keyword" )
56+ tagsParam := r .URL .Query ()["tag" ] // 支持多个 tag=xxx
57+ pageStr := r .URL .Query ().Get ("page" )
58+ limitStr := r .URL .Query ().Get ("limit" )
59+ page := int64 (1 )
60+ limit := int64 (10 )
61+ if pageStr != "" {
62+ if p , err := strconv .ParseInt (pageStr , 10 , 64 ); err == nil && p > 0 {
63+ page = p
64+ }
65+ }
66+ if limitStr != "" {
67+ if l , err := strconv .ParseInt (limitStr , 10 , 64 ); err == nil && l > 0 && l <= 100 {
68+ limit = l
69+ }
70+ }
71+ blogs , total , err := h .blogService .SearchBlogs (keyword , tagsParam , page , limit )
72+ if err != nil {
73+ http .Error (w , "搜索失败" , http .StatusInternalServerError )
74+ return
75+ }
76+ resp := BlogListResponse {
77+ Data : blogs ,
78+ Pagination : Pagination {Page : page , Limit : limit , Total : total },
79+ }
80+ w .Header ().Set ("Content-Type" , "application/json" )
81+ json .NewEncoder (w ).Encode (resp )
82+ }
83+
84+ // GetAllTagsHandler 获取所有标签
85+ func (h * BlogHandler ) GetAllTagsHandler (w http.ResponseWriter , r * http.Request ) {
86+ tags , err := h .blogService .GetAllTags ()
87+ if err != nil {
88+ http .Error (w , "获取标签失败" , http .StatusInternalServerError )
89+ return
90+ }
91+ w .Header ().Set ("Content-Type" , "application/json" )
92+ json .NewEncoder (w ).Encode (struct {
93+ Data []string `json:"data"`
94+ }{Data : tags })
95+ }
96+
97+ // AddTagHandler 新增标签(如有专门标签集合时用)
98+ func (h * BlogHandler ) AddTagHandler (w http.ResponseWriter , r * http.Request ) {
99+ var req TagRequest
100+ if err := json .NewDecoder (r .Body ).Decode (& req ); err != nil || req .Tag == "" {
101+ http .Error (w , "无效的标签" , http .StatusBadRequest )
102+ return
103+ }
104+ err := h .blogService .AddTag (req .Tag )
105+ if err != nil {
106+ http .Error (w , "添加标签失败" , http .StatusInternalServerError )
107+ return
108+ }
109+ w .WriteHeader (http .StatusCreated )
110+ }
111+
112+ // DeleteTagHandler 删除标签(会从所有博客中移除该标签)
113+ func (h * BlogHandler ) DeleteTagHandler (w http.ResponseWriter , r * http.Request ) {
114+ var req TagRequest
115+ if err := json .NewDecoder (r .Body ).Decode (& req ); err != nil || req .Tag == "" {
116+ http .Error (w , "无效的标签" , http .StatusBadRequest )
117+ return
118+ }
119+ err := h .blogService .DeleteTag (req .Tag )
120+ if err != nil {
121+ http .Error (w , "删除标签失败" , http .StatusInternalServerError )
122+ return
123+ }
124+ w .WriteHeader (http .StatusNoContent )
125+ }
126+
19127// NewBlogHandler 创建新的BlogHandler实例
20128func NewBlogHandler (blogService * services.BlogService ) * BlogHandler {
21129 return & BlogHandler {
0 commit comments