package controllers
|
|
import (
|
"errors"
|
"github.com/gin-gonic/gin"
|
"speechAnalysis/extend/code"
|
"speechAnalysis/extend/util"
|
"speechAnalysis/models"
|
"speechAnalysis/pkg/logx"
|
"speechAnalysis/request"
|
)
|
|
type TextCtl struct{}
|
|
// AddText
|
// @Tags 文字库
|
// @Summary 新增文字
|
// @Produce application/json
|
// @Param object body request.AddTextReq true "参数"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-sa/v1/text/add [post]
|
func (slf TextCtl) AddText(c *gin.Context) {
|
var req request.AddTextReq
|
if err := c.BindJSON(&req); err != nil {
|
logx.Errorf("add text params err:%v", err)
|
util.ResponseFormat(c, code.RequestParamError, err.Error())
|
return
|
}
|
|
text := models.Word{
|
Content: req.Content,
|
LocomotiveNumber: req.LocomotiveNumber,
|
}
|
|
if err := slf.paramsCheck(text); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, err.Error())
|
return
|
}
|
|
if err := models.NewWordSearch().Create(&text); err != nil {
|
util.ResponseFormat(c, code.SaveFail, "添加失败,请检查是否重复")
|
return
|
}
|
|
util.ResponseFormat(c, code.Success, "添加成功")
|
}
|
|
func (slf TextCtl) paramsCheck(text models.Word) (err error) {
|
if text.Content == "" || text.LocomotiveNumber == "" {
|
return errors.New("参数缺失")
|
}
|
_, err = models.NewWordSearch().SetLocomotiveNumber(text.LocomotiveNumber).SetContent(text.Content).First()
|
if err == nil {
|
return errors.New("文字重复")
|
}
|
return nil
|
}
|
|
// List
|
// @Tags 文字库
|
// @Summary 文字库列表
|
// @Produce application/json
|
// @Param object query request.GetTextList true "参数"
|
// @Success 200 {object} util.ResponseList{data=[]models.Word} "成功"
|
// @Router /api-sa/v1/text/list [get]
|
func (slf TextCtl) List(c *gin.Context) {
|
var params request.GetTextList
|
if err := c.ShouldBindQuery(¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, err.Error())
|
return
|
}
|
|
if !params.PageInfo.Check() {
|
util.ResponseFormat(c, code.RequestParamError, "分页参数错误")
|
return
|
}
|
|
list, total, err := models.NewWordSearch().
|
SetPage(params.Page, params.PageSize).
|
SetKeyword(params.Keyword).
|
Find()
|
|
if err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "查找失败")
|
return
|
}
|
|
util.ResponseFormatList(c, code.Success, list, total)
|
}
|