83 lines
2.3 KiB
Go
83 lines
2.3 KiB
Go
package keyword
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"mini-chat/internal/code"
|
|
"mini-chat/internal/pkg/core"
|
|
"mini-chat/internal/pkg/validation"
|
|
"mini-chat/internal/repository/mysql/model"
|
|
)
|
|
|
|
type createKeywordRequest struct {
|
|
AppID string `json:"app_id" binding:"required"` // 小程序ID
|
|
Keyword string `json:"keyword" binding:"required"` // 关键字
|
|
}
|
|
|
|
type createKeywordResponse struct {
|
|
Message string `json:"message"` // 提示信息
|
|
ID int32 `json:"id"` // 关键字ID
|
|
}
|
|
|
|
// CreateKeyword 添加意图关键字
|
|
// @Summary 添加意图关键字
|
|
// @Description 添加意图关键字
|
|
// @Tags 管理端.意图关键字
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param RequestBody body createKeywordRequest true "请求参数"
|
|
// @Success 200 {object} createKeywordResponse
|
|
// @Failure 400 {object} code.Failure
|
|
// @Router /admin/app/keyword [post]
|
|
// @Security LoginVerifyToken
|
|
func (h *handler) CreateKeyword() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
req := new(createKeywordRequest)
|
|
res := new(createKeywordResponse)
|
|
if err := ctx.ShouldBindJSON(req); err != nil {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.ParamBindError,
|
|
validation.Error(err)),
|
|
)
|
|
return
|
|
}
|
|
|
|
req.Keyword = strings.TrimSpace(req.Keyword)
|
|
|
|
// 验证关键字是否已存在
|
|
if _, err := h.readDB.AppKeyword.WithContext(ctx.RequestContext()).
|
|
Where(h.readDB.AppKeyword.AppID.Eq(req.AppID)).
|
|
Where(h.readDB.AppKeyword.Keyword.Eq(req.Keyword)).
|
|
First(); err == nil {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.CreateKeywordError,
|
|
fmt.Sprintf("%s: 该关键字(%s)已存在", code.Text(code.CreateKeywordError), req.Keyword)),
|
|
)
|
|
return
|
|
}
|
|
|
|
createData := new(model.AppKeyword)
|
|
createData.AppID = req.AppID
|
|
createData.Keyword = req.Keyword
|
|
createData.CreatedUser = ctx.SessionUserInfo().UserName
|
|
createData.CreatedAt = time.Now()
|
|
|
|
if err := h.writeDB.AppKeyword.WithContext(ctx.RequestContext()).Create(createData); err != nil {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.CreateKeywordError,
|
|
fmt.Sprintf("%s: %s", code.Text(code.CreateKeywordError), err.Error()),
|
|
))
|
|
}
|
|
|
|
res.Message = "操作成功"
|
|
res.ID = createData.ID
|
|
ctx.Payload(res)
|
|
}
|
|
}
|