bindbox-game/internal/api/keyword/keyword_delete.go
2025-10-21 10:30:53 +08:00

87 lines
2.2 KiB
Go

package keyword
import (
"fmt"
"net/http"
"strconv"
"mini-chat/internal/code"
"mini-chat/internal/pkg/core"
"gorm.io/gorm"
)
type deleteKeywordResponse struct {
Message string `json:"message"` // 提示信息
}
// DeleteKeyword 删除意图关键字
// @Summary 删除意图关键字
// @Description 删除意图关键字
// @Tags 管理端.意图关键字
// @Accept json
// @Produce json
// @Param id path string true "编号ID"
// @Success 200 {object} deleteKeywordResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/app/keyword/{id} [delete]
// @Security LoginVerifyToken
func (h *handler) DeleteKeyword() core.HandlerFunc {
return func(ctx core.Context) {
ID, err := strconv.Atoi(ctx.Param("id"))
if err != nil {
ctx.AbortWithError(core.Error(
http.StatusBadRequest,
code.ParamBindError,
"未传递编号ID。"),
)
return
}
info, err := h.readDB.AppKeyword.WithContext(ctx.RequestContext()).
Where(h.readDB.AppKeyword.ID.Eq(int32(ID))).
First()
if err != nil && err != gorm.ErrRecordNotFound {
ctx.AbortWithError(core.Error(
http.StatusBadRequest,
code.DeleteKeywordError,
fmt.Sprintf("%s: %s", code.Text(code.DeleteKeywordError), err.Error())),
)
return
}
if info == nil {
ctx.AbortWithError(core.Error(
http.StatusBadRequest,
code.DeleteKeywordError,
fmt.Sprintf("%s: 编号(%d)不存在。", code.Text(code.DeleteKeywordError), ID)),
)
return
}
if _, err := h.writeDB.AppKeyword.WithContext(ctx.RequestContext()).
Where(h.writeDB.AppKeyword.ID.Eq(int32(ID))).
Delete(); err != nil {
ctx.AbortWithError(core.Error(
http.StatusBadRequest,
code.DeleteKeywordError,
fmt.Sprintf("%s: %s", code.Text(code.DeleteKeywordError), err.Error())),
)
}
if _, err := h.writeDB.AppKeywordReply.WithContext(ctx.RequestContext()).
Where(h.writeDB.AppKeywordReply.KeywordID.Eq(int32(ID))).
Delete(); err != nil {
ctx.AbortWithError(core.Error(
http.StatusBadRequest,
code.DeleteKeywordError,
fmt.Sprintf("%s: %s", code.Text(code.DeleteKeywordError), err.Error())),
)
}
res := new(deleteKeywordResponse)
res.Message = "操作成功"
ctx.Payload(res)
}
}