Files
go-gin-api/internal/api/controller/admin_handler/func_delete.go
T
新亮 62367a041a feature(参数验证): 优化 gin 参数验证的错误信息
- 新增 internal/pkg/validation 包,详情:https://github.com/xinliangnote/go-gin-api/issues/44
- 调整 code 包位置,移动至 internal/pkg/code 目录。

Closes #44
2021-07-10 22:38:30 +08:00

68 lines
1.4 KiB
Go
Executable File

package admin_handler
import (
"net/http"
"github.com/xinliangnote/go-gin-api/internal/pkg/code"
"github.com/xinliangnote/go-gin-api/internal/pkg/core"
"github.com/xinliangnote/go-gin-api/pkg/errno"
)
type deleteRequest struct {
Id string `uri:"id"` // HashID
}
type deleteResponse struct {
Id int32 `json:"id"` // 主键ID
}
// Delete 删除管理员
// @Summary 删除管理员
// @Description 删除管理员
// @Tags API.admin
// @Accept json
// @Produce json
// @Param id path string true "hashId"
// @Success 200 {object} deleteResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/{id} [delete]
func (h *handler) Delete() core.HandlerFunc {
return func(c core.Context) {
req := new(deleteRequest)
res := new(deleteResponse)
if err := c.ShouldBindURI(req); err != nil {
c.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.ParamBindError,
code.Text(code.ParamBindError)).WithErr(err),
)
return
}
ids, err := h.hashids.HashidsDecode(req.Id)
if err != nil {
c.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.HashIdsDecodeError,
code.Text(code.HashIdsDecodeError)).WithErr(err),
)
return
}
id := int32(ids[0])
err = h.adminService.Delete(c, id)
if err != nil {
c.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.AdminDeleteError,
code.Text(code.AdminDeleteError)).WithErr(err),
)
return
}
res.Id = id
c.Payload(res)
}
}