Skip to content

Commit

Permalink
feat(tag): 更新tag router 完成接口
Browse files Browse the repository at this point in the history
  • Loading branch information
minibear2333 committed Jun 17, 2021
1 parent 6ccfc9d commit a344bae
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 23 deletions.
9 changes: 6 additions & 3 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// 2021-06-13 22:31:24.885701 +0800 CST m=+0.021147477
// 2021-06-17 22:15:02.037541 +0800 CST m=+0.017465621

package docs

Expand Down Expand Up @@ -109,7 +109,7 @@ var doc = `{
1
],
"default": 1,
"description": "状态",
"description": "是否启用(0 为禁用、1 为启用)",
"name": "state",
"in": "body",
"schema": {
Expand Down Expand Up @@ -180,7 +180,7 @@ var doc = `{
1
],
"default": 1,
"description": "状态",
"description": "是否启用(0 为禁用、1 为启用)",
"name": "state",
"in": "body",
"schema": {
Expand Down Expand Up @@ -296,9 +296,11 @@ var doc = `{
"type": "string"
},
"created_on": {
"description": "创建时间",
"type": "integer"
},
"deleted_on": {
"description": "删除时间",
"type": "integer"
},
"id": {
Expand All @@ -311,6 +313,7 @@ var doc = `{
"type": "string"
},
"modified_on": {
"description": "更新时间",
"type": "integer"
},
"name": {
Expand Down
7 changes: 5 additions & 2 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
1
],
"default": 1,
"description": "状态",
"description": "是否启用(0 为禁用、1 为启用)",
"name": "state",
"in": "body",
"schema": {
Expand Down Expand Up @@ -162,7 +162,7 @@
1
],
"default": 1,
"description": "状态",
"description": "是否启用(0 为禁用、1 为启用)",
"name": "state",
"in": "body",
"schema": {
Expand Down Expand Up @@ -278,9 +278,11 @@
"type": "string"
},
"created_on": {
"description": "创建时间",
"type": "integer"
},
"deleted_on": {
"description": "删除时间",
"type": "integer"
},
"id": {
Expand All @@ -293,6 +295,7 @@
"type": "string"
},
"modified_on": {
"description": "更新时间",
"type": "integer"
},
"name": {
Expand Down
7 changes: 5 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ definitions:
created_by:
type: string
created_on:
description: 创建时间
type: integer
deleted_on:
description: 删除时间
type: integer
id:
type: integer
Expand All @@ -34,6 +36,7 @@ definitions:
modified_by:
type: string
modified_on:
description: 更新时间
type: integer
name:
type: string
Expand Down Expand Up @@ -109,7 +112,7 @@ paths:
schema:
type: string
- default: 1
description: 状态
description: 是否启用(0 为禁用、1 为启用)
enum:
- 0
- 1
Expand Down Expand Up @@ -180,7 +183,7 @@ paths:
schema:
type: string
- default: 1
description: 状态
description: 是否启用(0 为禁用、1 为启用)
enum:
- 0
- 1
Expand Down
105 changes: 91 additions & 14 deletions internal/routers/api/v1/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package v1
import (
"github.com/gin-gonic/gin"
"github.com/golang-minibear2333/gin-blog/global"
"github.com/golang-minibear2333/gin-blog/internal/service"
"github.com/golang-minibear2333/gin-blog/pkg/app"
"github.com/golang-minibear2333/gin-blog/pkg/convert"
"github.com/golang-minibear2333/gin-blog/pkg/errcode"
)

Expand All @@ -27,48 +29,102 @@ func (t Tag) Get(c *gin.Context) {}
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/tags [get]
func (t Tag) List(c *gin.Context) {
// 报错测试
// curl -X GET "http://localhost:8000/api/v1/tags?state=6" -H "accept: application/json"
param := struct {
Name string `form:"name" binding:"max=100"`
State uint8 `form:"state,default=1" binding:"oneof=0 1"`
}{}
param := service.TagListRequest{}
response := app.NewResponse(c)
// 使用 internal/service/tag.go 内的结构体验证ctx请求带上的参数是否合法,并根据地域进行翻译报错
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.Logger.Errorf("app.BindAndValid errs: %v", errs)
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
return
}

response.ToResponse(gin.H{})
// 获取Service
svc := service.New(c.Request.Context())
// 处理分页参数,防止出现异常
pager := app.Pager{Page: app.GetPage(c), PageSize: app.GetPageSize(c)}
// 获取标签总数,这里不用传name时可以拉到所有标签,State代表禁用启用
// TODO 这里查询列表有必要传入标签名吗?
totalRows, err := svc.CountTag(&service.CountTagRequest{Name: param.Name, State: param.State})
if err != nil {
global.Logger.Errorf("svc.CountTag err: %v", err)
response.ToErrorResponse(errcode.ErrorCountTagFail)
return
}
// 获取标签列表
tags, err := svc.GetTagList(&param, &pager)
if err != nil {
global.Logger.Errorf("svc.GetTagList err: %v", err)
response.ToErrorResponse(errcode.ErrorGetTagListFail)
return
}
// 返回标签列表成规定的格式
response.ToResponseList(tags, totalRows)
return
}

// Create tag create
// @Summary 新增标签
// @Produce json
// @Param name body string true "标签名称" minlength(3) maxlength(100)
// @Param state body int false "状态" Enums(0, 1) default(1)
// @Param state body int false "是否启用(0 为禁用、1 为启用)" Enums(0, 1) default(1)
// @Param created_by body string true "创建者" minlength(3) maxlength(100)
// @Success 200 {object} model.Tag "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/tags [post]
func (t Tag) Create(c *gin.Context) {}
func (t Tag) Create(c *gin.Context) {
param := service.CreateTagRequest{}
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.Logger.Errorf("app.BindAndValid errs: %v", errs)
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
return
}

svc := service.New(c.Request.Context())
err := svc.CreateTag(&param)
if err != nil {
global.Logger.Errorf("svc.CreateTag err: %v", err)
response.ToErrorResponse(errcode.ErrorCreateTagFail)
return
}

response.ToResponse(gin.H{})
return
}

// Update tag update
// @Summary 更新标签
// @Produce json
// @Param id path int true "标签 ID"
// @Param name body string false "标签名称" minlength(3) maxlength(100)
// @Param state body int false "状态" Enums(0, 1) default(1)
// @Param state body int false "是否启用(0 为禁用、1 为启用)" Enums(0, 1) default(1)
// @Param modified_by body string true "修改者" minlength(3) maxlength(100)
// @Success 200 {object} model.Tag "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/tags/{id} [put]
func (t Tag) Update(c *gin.Context) {}
func (t Tag) Update(c *gin.Context) {
param := service.UpdateTagRequest{ID: convert.StrTo(c.Param("id")).MustUInt32()}
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.Logger.Errorf("app.BindAndValid errs: %v", errs)
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
return
}

svc := service.New(c.Request.Context())
err := svc.UpdateTag(&param)
if err != nil {
global.Logger.Errorf("svc.UpdateTag err: %v", err)
response.ToErrorResponse(errcode.ErrorUpdateTagFail)
return
}

response.ToResponse(gin.H{})
return
}

// Delete tag delete
// @Summary 删除标签
Expand All @@ -78,4 +134,25 @@ func (t Tag) Update(c *gin.Context) {}
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/tags/{id} [delete]
func (t Tag) Delete(c *gin.Context) {}
func (t Tag) Delete(c *gin.Context) {
param := service.DeleteTagRequest{ID: convert.StrTo(c.Param("id")).MustUInt32()}
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
// TODO 这些代码重复太多次有办法抽出来吗?
global.Logger.Errorf("app.BindAndValid errs: %v", errs)
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
return
}

svc := service.New(c.Request.Context())
err := svc.DeleteTag(&param)
if err != nil {
global.Logger.Errorf("svc.DeleteTag err: %v", err)
response.ToErrorResponse(errcode.ErrorDeleteTagFail)
return
}

response.ToResponse(gin.H{})
return
}
2 changes: 1 addition & 1 deletion internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Service struct {
ctx context.Context
dao *dao.Dao
}

// New 所有Service都走这里的逻辑
func New(ctx context.Context) Service {
svc := Service{ctx: ctx}
svc.dao = dao.New(global.DBEngine)
Expand Down
9 changes: 9 additions & 0 deletions pkg/errcode/module_code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package errcode

var (
ErrorGetTagListFail = NewError(20010001, "获取标签列表失败")
ErrorCreateTagFail = NewError(20010002, "创建标签失败")
ErrorUpdateTagFail = NewError(20010003, "更新标签失败")
ErrorDeleteTagFail = NewError(20010004, "删除标签失败")
ErrorCountTagFail = NewError(20010005, "统计标签失败")
)
1 change: 1 addition & 0 deletions scripts/build_swagger.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pushd ../ && swag init
1 change: 1 addition & 0 deletions scripts/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pushd ../ && docker-compose up -d
1 change: 0 additions & 1 deletion start.sh

This file was deleted.

0 comments on commit a344bae

Please sign in to comment.