mirror of
https://github.com/xinliangnote/go-gin-api.git
synced 2024-04-21 12:31:46 +00:00
- 将 middleware 命名为 interceptor - 将 deploy 命名为 deployments - 移除 pkg/errno - 使用 proposal 目录 - 优化代码
70 lines
1.9 KiB
Go
Executable File
70 lines
1.9 KiB
Go
Executable File
package admin
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/xinliangnote/go-gin-api/internal/code"
|
|
"github.com/xinliangnote/go-gin-api/internal/pkg/core"
|
|
"github.com/xinliangnote/go-gin-api/internal/pkg/validation"
|
|
"github.com/xinliangnote/go-gin-api/internal/services/admin"
|
|
)
|
|
|
|
type createRequest struct {
|
|
Username string `form:"username" binding:"required"` // 用户名
|
|
Nickname string `form:"nickname" binding:"required"` // 昵称
|
|
Mobile string `form:"mobile" binding:"required"` // 手机号
|
|
Password string `form:"password" binding:"required"` // MD5后的密码
|
|
}
|
|
|
|
type createResponse struct {
|
|
Id int32 `json:"id"` // 主键ID
|
|
}
|
|
|
|
// Create 新增管理员
|
|
// @Summary 新增管理员
|
|
// @Description 新增管理员
|
|
// @Tags API.admin
|
|
// @Accept application/x-www-form-urlencoded
|
|
// @Produce json
|
|
// @Param username formData string true "用户名"
|
|
// @Param nickname formData string true "昵称"
|
|
// @Param mobile formData string true "手机号"
|
|
// @Param password formData string true "MD5后的密码"
|
|
// @Success 200 {object} createResponse
|
|
// @Failure 400 {object} code.Failure
|
|
// @Router /api/admin [post]
|
|
// @Security LoginToken
|
|
func (h *handler) Create() core.HandlerFunc {
|
|
return func(c core.Context) {
|
|
req := new(createRequest)
|
|
res := new(createResponse)
|
|
if err := c.ShouldBindForm(req); err != nil {
|
|
c.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.ParamBindError,
|
|
validation.Error(err)).WithError(err),
|
|
)
|
|
return
|
|
}
|
|
|
|
createData := new(admin.CreateAdminData)
|
|
createData.Nickname = req.Nickname
|
|
createData.Username = req.Username
|
|
createData.Mobile = req.Mobile
|
|
createData.Password = req.Password
|
|
|
|
id, err := h.adminService.Create(c, createData)
|
|
if err != nil {
|
|
c.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.AdminCreateError,
|
|
code.Text(code.AdminCreateError)).WithError(err),
|
|
)
|
|
return
|
|
}
|
|
|
|
res.Id = id
|
|
c.Payload(res)
|
|
}
|
|
}
|