Skip to content

Commit

Permalink
fix eddycjy#38: swago v1.1 -> v1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
eddycjy committed Feb 2, 2019
1 parent 44b5ef3 commit 82b41dd
Show file tree
Hide file tree
Showing 25 changed files with 1,120 additions and 317 deletions.
436 changes: 350 additions & 86 deletions docs/docs.go

Large diffs are not rendered by default.

407 changes: 323 additions & 84 deletions docs/swagger/swagger.json

Large diffs are not rendered by default.

305 changes: 231 additions & 74 deletions docs/swagger/swagger.yaml

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions pkg/app/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ type Gin struct {
C *gin.Context
}

type Response struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}

func (g *Gin) Response(httpCode, errCode int, data interface{}) {
g.C.JSON(httpCode, gin.H{
"code": httpCode,
"msg": e.GetMsg(errCode),
"data": data,
g.C.JSON(httpCode, Response{
Code: httpCode,
Msg: e.GetMsg(errCode),
Data: data,
})

return
}
7 changes: 4 additions & 3 deletions routers/api/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (
"github.com/EDDYCJY/go-gin-example/pkg/upload"
)

// @Summary 上传图片
// @Summary Import Image
// @Produce json
// @Param image post file true "图片文件"
// @Success 200 {string} json "{"code":200,"data":{"image_save_url":"upload/images/96a.jpg", "image_url": "http://..."}"
// @Param image formData file true "Image File"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /api/v1/tags/import [post]
func UploadImage(c *gin.Context) {
appG := app.Gin{C: c}
Expand Down
71 changes: 37 additions & 34 deletions routers/api/v1/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ import (
"github.com/EDDYCJY/go-gin-example/service/tag_service"
)

// @Summary 获取单个文章
// @Summary Get a single article
// @Produce json
// @Param id param int true "ID"
// @Success 200 {string} json "{"code":200,"data":{"id":3,"created_on":1516937037,"modified_on":0,"tag_id":11,"tag":{"id":11,"created_on":1516851591,"modified_on":0,"name":"312321","created_by":"4555","modified_by":"","state":1},"content":"5555","created_by":"2412","modified_by":"","state":1},"msg":"ok"}"
// @Param id path int true "ID"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /api/v1/articles/{id} [get]
func GetArticle(c *gin.Context) {
appG := app.Gin{C: c}
id := com.StrTo(c.Param("id")).MustInt()
valid := validation.Validation{}
valid.Min(id, 1, "id").Message("ID必须大于0")
valid.Min(id, 1, "id")

if valid.HasErrors() {
app.MarkErrors(valid.Errors)
Expand Down Expand Up @@ -54,12 +55,13 @@ func GetArticle(c *gin.Context) {
appG.Response(http.StatusOK, e.SUCCESS, article)
}

// @Summary 获取多个文章
// @Summary Get multiple articles
// @Produce json
// @Param tag_id query int false "TagID"
// @Param state query int false "State"
// @Param created_by query int false "CreatedBy"
// @Success 200 {string} json "{"code":200,"data":[{"id":3,"created_on":1516937037,"modified_on":0,"tag_id":11,"tag":{"id":11,"created_on":1516851591,"modified_on":0,"name":"312321","created_by":"4555","modified_by":"","state":1},"content":"5555","created_by":"2412","modified_by":"","state":1}],"msg":"ok"}"
// @Param tag_id body int false "TagID"
// @Param state body int false "State"
// @Param created_by body int false "CreatedBy"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /api/v1/articles [get]
func GetArticles(c *gin.Context) {
appG := app.Gin{C: c}
Expand All @@ -68,13 +70,13 @@ func GetArticles(c *gin.Context) {
state := -1
if arg := c.PostForm("state"); arg != "" {
state = com.StrTo(arg).MustInt()
valid.Range(state, 0, 1, "state").Message("状态只允许0或1")
valid.Range(state, 0, 1, "state")
}

tagId := -1
if arg := c.PostForm("tag_id"); arg != "" {
tagId = com.StrTo(arg).MustInt()
valid.Min(tagId, 1, "tag_id").Message("标签ID必须大于0")
valid.Min(tagId, 1, "tag_id")
}

if valid.HasErrors() {
Expand Down Expand Up @@ -119,15 +121,16 @@ type AddArticleForm struct {
State int `form:"state" valid:"Range(0,1)"`
}

// @Summary 新增文章
// @Summary Add article
// @Produce json
// @Param tag_id query int true "TagID"
// @Param title query string true "Title"
// @Param desc query string true "Desc"
// @Param content query string true "Content"
// @Param created_by query string true "CreatedBy"
// @Param state query int true "State"
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
// @Param tag_id body int true "TagID"
// @Param title body string true "Title"
// @Param desc body string true "Desc"
// @Param content body string true "Content"
// @Param created_by body string true "CreatedBy"
// @Param state body int true "State"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /api/v1/articles [post]
func AddArticle(c *gin.Context) {
var (
Expand Down Expand Up @@ -180,17 +183,17 @@ type EditArticleForm struct {
State int `form:"state" valid:"Range(0,1)"`
}

// @Summary 修改文章
// @Summary Update article
// @Produce json
// @Param id param int true "ID"
// @Param tag_id query string false "TagID"
// @Param title query string false "Title"
// @Param desc query string false "Desc"
// @Param content query string false "Content"
// @Param modified_by query string true "ModifiedBy"
// @Param state query int false "State"
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
// @Failure 200 {string} json "{"code":400,"data":{},"msg":"请求参数错误"}"
// @Param id path int true "ID"
// @Param tag_id body string false "TagID"
// @Param title body string false "Title"
// @Param desc body string false "Desc"
// @Param content body string false "Content"
// @Param modified_by body string true "ModifiedBy"
// @Param state body int false "State"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /api/v1/articles/{id} [put]
func EditArticle(c *gin.Context) {
var (
Expand All @@ -205,7 +208,7 @@ func EditArticle(c *gin.Context) {
}

articleService := article_service.Article{
ID: form.ID,
ID: form.ID,
TagID: form.TagID,
Title: form.Title,
Desc: form.Desc,
Expand Down Expand Up @@ -245,11 +248,11 @@ func EditArticle(c *gin.Context) {
appG.Response(http.StatusOK, e.SUCCESS, nil)
}

// @Summary 删除文章
// @Summary Delete article
// @Produce json
// @Param id param int true "ID"
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
// @Failure 200 {string} json "{"code":400,"data":{},"msg":"请求参数错误"}"
// @Param id path int true "ID"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /api/v1/articles/{id} [delete]
func DeleteArticle(c *gin.Context) {
appG := app.Gin{C: c}
Expand Down
52 changes: 29 additions & 23 deletions routers/api/v1/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import (
"github.com/EDDYCJY/go-gin-example/service/tag_service"
)

// @Summary 获取多个文章标签
// @Summary Get multiple article tags
// @Produce json
// @Param name query string false "Name"
// @Param state query int false "State"
// @Success 200 {string} json "{"code":200,"data":{"lists":[{"id":3,"created_on":1516849721,"modified_on":0,"name":"3333","created_by":"4555","modified_by":"","state":0}],"total":29},"msg":"ok"}"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /api/v1/tags [get]
func GetTags(c *gin.Context) {
appG := app.Gin{C: c}
Expand Down Expand Up @@ -60,12 +61,13 @@ type AddTagForm struct {
State int `form:"state" valid:"Range(0,1)"`
}

// @Summary 新增文章标签
// @Summary Add article tag
// @Produce json
// @Param name query string true "Name"
// @Param state query int false "State"
// @Param created_by query int false "CreatedBy"
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
// @Param name body string true "Name"
// @Param state body int false "State"
// @Param created_by body int false "CreatedBy"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /api/v1/tags [post]
func AddTag(c *gin.Context) {
var (
Expand Down Expand Up @@ -110,13 +112,14 @@ type EditTagForm struct {
State int `form:"state" valid:"Range(0,1)"`
}

// @Summary 修改文章标签
// @Summary Update article tag
// @Produce json
// @Param id param int true "ID"
// @Param name query string true "ID"
// @Param state query int false "State"
// @Param modified_by query string true "ModifiedBy"
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
// @Param id path int true "ID"
// @Param name body string true "ID"
// @Param state body int false "State"
// @Param modified_by body string true "ModifiedBy"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /api/v1/tags/{id} [put]
func EditTag(c *gin.Context) {
var (
Expand Down Expand Up @@ -157,10 +160,11 @@ func EditTag(c *gin.Context) {
appG.Response(http.StatusOK, e.SUCCESS, nil)
}

// @Summary 删除文章标签
// @Summary Delete article tag
// @Produce json
// @Param id param int true "ID"
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
// @Param id path int true "ID"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /api/v1/tags/{id} [delete]
func DeleteTag(c *gin.Context) {
appG := app.Gin{C: c}
Expand Down Expand Up @@ -193,11 +197,12 @@ func DeleteTag(c *gin.Context) {
appG.Response(http.StatusOK, e.SUCCESS, nil)
}

// @Summary 导出文章标签
// @Summary Export article tag
// @Produce json
// @Param name post string false "Name"
// @Param state post int false "State"
// @Success 200 {string} json "{"code":200,"data":{"export_save_url":"export/abc.xlsx", "export_url": "http://..."},"msg":"ok"}"
// @Param name body string false "Name"
// @Param state body int false "State"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /api/v1/tags/export [post]
func ExportTag(c *gin.Context) {
appG := app.Gin{C: c}
Expand All @@ -224,10 +229,11 @@ func ExportTag(c *gin.Context) {
})
}

// @Summary 导入文章标签
// @Summary Import article tag
// @Produce json
// @Param file post file true "标签Excel文件"
// @Success 200 {string} json "{"code":200,"data":null,"msg":"ok"}"
// @Param file body file true "Excel File"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /api/v1/tags/import [post]
func ImportTag(c *gin.Context) {
appG := app.Gin{C: c}
Expand Down
45 changes: 43 additions & 2 deletions vendor/github.com/swaggo/gin-swagger/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions vendor/github.com/swaggo/gin-swagger/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 82b41dd

Please sign in to comment.