Skip to content

Commit a344bae

Browse files
committed
feat(tag): 更新tag router 完成接口
1 parent 6ccfc9d commit a344bae

File tree

9 files changed

+119
-23
lines changed

9 files changed

+119
-23
lines changed

docs/docs.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
22
// This file was generated by swaggo/swag at
3-
// 2021-06-13 22:31:24.885701 +0800 CST m=+0.021147477
3+
// 2021-06-17 22:15:02.037541 +0800 CST m=+0.017465621
44

55
package docs
66

@@ -109,7 +109,7 @@ var doc = `{
109109
1
110110
],
111111
"default": 1,
112-
"description": "状态",
112+
"description": "是否启用(0 为禁用、1 为启用)",
113113
"name": "state",
114114
"in": "body",
115115
"schema": {
@@ -180,7 +180,7 @@ var doc = `{
180180
1
181181
],
182182
"default": 1,
183-
"description": "状态",
183+
"description": "是否启用(0 为禁用、1 为启用)",
184184
"name": "state",
185185
"in": "body",
186186
"schema": {
@@ -296,9 +296,11 @@ var doc = `{
296296
"type": "string"
297297
},
298298
"created_on": {
299+
"description": "创建时间",
299300
"type": "integer"
300301
},
301302
"deleted_on": {
303+
"description": "删除时间",
302304
"type": "integer"
303305
},
304306
"id": {
@@ -311,6 +313,7 @@ var doc = `{
311313
"type": "string"
312314
},
313315
"modified_on": {
316+
"description": "更新时间",
314317
"type": "integer"
315318
},
316319
"name": {

docs/swagger.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
1
9292
],
9393
"default": 1,
94-
"description": "状态",
94+
"description": "是否启用(0 为禁用、1 为启用)",
9595
"name": "state",
9696
"in": "body",
9797
"schema": {
@@ -162,7 +162,7 @@
162162
1
163163
],
164164
"default": 1,
165-
"description": "状态",
165+
"description": "是否启用(0 为禁用、1 为启用)",
166166
"name": "state",
167167
"in": "body",
168168
"schema": {
@@ -278,9 +278,11 @@
278278
"type": "string"
279279
},
280280
"created_on": {
281+
"description": "创建时间",
281282
"type": "integer"
282283
},
283284
"deleted_on": {
285+
"description": "删除时间",
284286
"type": "integer"
285287
},
286288
"id": {
@@ -293,6 +295,7 @@
293295
"type": "string"
294296
},
295297
"modified_on": {
298+
"description": "更新时间",
296299
"type": "integer"
297300
},
298301
"name": {

docs/swagger.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ definitions:
2424
created_by:
2525
type: string
2626
created_on:
27+
description: 创建时间
2728
type: integer
2829
deleted_on:
30+
description: 删除时间
2931
type: integer
3032
id:
3133
type: integer
@@ -34,6 +36,7 @@ definitions:
3436
modified_by:
3537
type: string
3638
modified_on:
39+
description: 更新时间
3740
type: integer
3841
name:
3942
type: string
@@ -109,7 +112,7 @@ paths:
109112
schema:
110113
type: string
111114
- default: 1
112-
description: 状态
115+
description: 是否启用(0 为禁用、1 为启用)
113116
enum:
114117
- 0
115118
- 1
@@ -180,7 +183,7 @@ paths:
180183
schema:
181184
type: string
182185
- default: 1
183-
description: 状态
186+
description: 是否启用(0 为禁用、1 为启用)
184187
enum:
185188
- 0
186189
- 1

internal/routers/api/v1/tag.go

Lines changed: 91 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package v1
33
import (
44
"github.com/gin-gonic/gin"
55
"github.com/golang-minibear2333/gin-blog/global"
6+
"github.com/golang-minibear2333/gin-blog/internal/service"
67
"github.com/golang-minibear2333/gin-blog/pkg/app"
8+
"github.com/golang-minibear2333/gin-blog/pkg/convert"
79
"github.com/golang-minibear2333/gin-blog/pkg/errcode"
810
)
911

@@ -27,48 +29,102 @@ func (t Tag) Get(c *gin.Context) {}
2729
// @Failure 500 {object} errcode.Error "内部错误"
2830
// @Router /api/v1/tags [get]
2931
func (t Tag) List(c *gin.Context) {
30-
// 报错测试
31-
// curl -X GET "http://localhost:8000/api/v1/tags?state=6" -H "accept: application/json"
32-
param := struct {
33-
Name string `form:"name" binding:"max=100"`
34-
State uint8 `form:"state,default=1" binding:"oneof=0 1"`
35-
}{}
32+
param := service.TagListRequest{}
3633
response := app.NewResponse(c)
34+
// 使用 internal/service/tag.go 内的结构体验证ctx请求带上的参数是否合法,并根据地域进行翻译报错
3735
valid, errs := app.BindAndValid(c, &param)
3836
if !valid {
3937
global.Logger.Errorf("app.BindAndValid errs: %v", errs)
4038
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
4139
return
4240
}
43-
44-
response.ToResponse(gin.H{})
41+
// 获取Service
42+
svc := service.New(c.Request.Context())
43+
// 处理分页参数,防止出现异常
44+
pager := app.Pager{Page: app.GetPage(c), PageSize: app.GetPageSize(c)}
45+
// 获取标签总数,这里不用传name时可以拉到所有标签,State代表禁用启用
46+
// TODO 这里查询列表有必要传入标签名吗?
47+
totalRows, err := svc.CountTag(&service.CountTagRequest{Name: param.Name, State: param.State})
48+
if err != nil {
49+
global.Logger.Errorf("svc.CountTag err: %v", err)
50+
response.ToErrorResponse(errcode.ErrorCountTagFail)
51+
return
52+
}
53+
// 获取标签列表
54+
tags, err := svc.GetTagList(&param, &pager)
55+
if err != nil {
56+
global.Logger.Errorf("svc.GetTagList err: %v", err)
57+
response.ToErrorResponse(errcode.ErrorGetTagListFail)
58+
return
59+
}
60+
// 返回标签列表成规定的格式
61+
response.ToResponseList(tags, totalRows)
4562
return
4663
}
4764

48-
// Create tag create
4965
// @Summary 新增标签
5066
// @Produce json
5167
// @Param name body string true "标签名称" minlength(3) maxlength(100)
52-
// @Param state body int false "状态" Enums(0, 1) default(1)
68+
// @Param state body int false "是否启用(0 为禁用、1 为启用)" Enums(0, 1) default(1)
5369
// @Param created_by body string true "创建者" minlength(3) maxlength(100)
5470
// @Success 200 {object} model.Tag "成功"
5571
// @Failure 400 {object} errcode.Error "请求错误"
5672
// @Failure 500 {object} errcode.Error "内部错误"
5773
// @Router /api/v1/tags [post]
58-
func (t Tag) Create(c *gin.Context) {}
74+
func (t Tag) Create(c *gin.Context) {
75+
param := service.CreateTagRequest{}
76+
response := app.NewResponse(c)
77+
valid, errs := app.BindAndValid(c, &param)
78+
if !valid {
79+
global.Logger.Errorf("app.BindAndValid errs: %v", errs)
80+
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
81+
return
82+
}
83+
84+
svc := service.New(c.Request.Context())
85+
err := svc.CreateTag(&param)
86+
if err != nil {
87+
global.Logger.Errorf("svc.CreateTag err: %v", err)
88+
response.ToErrorResponse(errcode.ErrorCreateTagFail)
89+
return
90+
}
91+
92+
response.ToResponse(gin.H{})
93+
return
94+
}
5995

6096
// Update tag update
6197
// @Summary 更新标签
6298
// @Produce json
6399
// @Param id path int true "标签 ID"
64100
// @Param name body string false "标签名称" minlength(3) maxlength(100)
65-
// @Param state body int false "状态" Enums(0, 1) default(1)
101+
// @Param state body int false "是否启用(0 为禁用、1 为启用)" Enums(0, 1) default(1)
66102
// @Param modified_by body string true "修改者" minlength(3) maxlength(100)
67103
// @Success 200 {object} model.Tag "成功"
68104
// @Failure 400 {object} errcode.Error "请求错误"
69105
// @Failure 500 {object} errcode.Error "内部错误"
70106
// @Router /api/v1/tags/{id} [put]
71-
func (t Tag) Update(c *gin.Context) {}
107+
func (t Tag) Update(c *gin.Context) {
108+
param := service.UpdateTagRequest{ID: convert.StrTo(c.Param("id")).MustUInt32()}
109+
response := app.NewResponse(c)
110+
valid, errs := app.BindAndValid(c, &param)
111+
if !valid {
112+
global.Logger.Errorf("app.BindAndValid errs: %v", errs)
113+
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
114+
return
115+
}
116+
117+
svc := service.New(c.Request.Context())
118+
err := svc.UpdateTag(&param)
119+
if err != nil {
120+
global.Logger.Errorf("svc.UpdateTag err: %v", err)
121+
response.ToErrorResponse(errcode.ErrorUpdateTagFail)
122+
return
123+
}
124+
125+
response.ToResponse(gin.H{})
126+
return
127+
}
72128

73129
// Delete tag delete
74130
// @Summary 删除标签
@@ -78,4 +134,25 @@ func (t Tag) Update(c *gin.Context) {}
78134
// @Failure 400 {object} errcode.Error "请求错误"
79135
// @Failure 500 {object} errcode.Error "内部错误"
80136
// @Router /api/v1/tags/{id} [delete]
81-
func (t Tag) Delete(c *gin.Context) {}
137+
func (t Tag) Delete(c *gin.Context) {
138+
param := service.DeleteTagRequest{ID: convert.StrTo(c.Param("id")).MustUInt32()}
139+
response := app.NewResponse(c)
140+
valid, errs := app.BindAndValid(c, &param)
141+
if !valid {
142+
// TODO 这些代码重复太多次有办法抽出来吗?
143+
global.Logger.Errorf("app.BindAndValid errs: %v", errs)
144+
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
145+
return
146+
}
147+
148+
svc := service.New(c.Request.Context())
149+
err := svc.DeleteTag(&param)
150+
if err != nil {
151+
global.Logger.Errorf("svc.DeleteTag err: %v", err)
152+
response.ToErrorResponse(errcode.ErrorDeleteTagFail)
153+
return
154+
}
155+
156+
response.ToResponse(gin.H{})
157+
return
158+
}

internal/service/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type Service struct {
1010
ctx context.Context
1111
dao *dao.Dao
1212
}
13-
13+
// New 所有Service都走这里的逻辑
1414
func New(ctx context.Context) Service {
1515
svc := Service{ctx: ctx}
1616
svc.dao = dao.New(global.DBEngine)

pkg/errcode/module_code.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package errcode
2+
3+
var (
4+
ErrorGetTagListFail = NewError(20010001, "获取标签列表失败")
5+
ErrorCreateTagFail = NewError(20010002, "创建标签失败")
6+
ErrorUpdateTagFail = NewError(20010003, "更新标签失败")
7+
ErrorDeleteTagFail = NewError(20010004, "删除标签失败")
8+
ErrorCountTagFail = NewError(20010005, "统计标签失败")
9+
)

scripts/build_swagger.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pushd ../ && swag init

scripts/start.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pushd ../ && docker-compose up -d

start.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)