| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package controllers
- import (
- "errors"
- "github.com/gin-gonic/gin"
- "gorm.io/gorm"
- "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, "添加成功")
- }
- // UpdateText
- // @Tags 文字库
- // @Summary 修改文字
- // @Produce application/json
- // @Param object body request.AddTextReq true "参数"
- // @Success 200 {object} util.Response "成功"
- // @Router /api-sa/v1/text/update [post]
- func (slf TextCtl) UpdateText(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{
- Model: gorm.Model{
- ID: req.Id,
- },
- Content: req.Content,
- LocomotiveNumber: req.LocomotiveNumber,
- }
- if err := slf.paramsCheck(text); err != nil {
- util.ResponseFormat(c, code.RequestParamError, err.Error())
- return
- }
- if req.Id == 0 {
- util.ResponseFormat(c, code.RequestParamError, errors.New("ID不能为空"))
- return
- }
- if err := models.NewWordSearch().Save(&text); err != nil {
- util.ResponseFormat(c, code.SaveFail, "修改失败,请检查是否重复")
- return
- }
- util.ResponseFormat(c, code.Success, "修改成功")
- }
- // DeleteText
- // @Tags 文字库
- // @Summary 修改文字
- // @Produce application/json
- // @Param object body request.AddTextReq true "参数"
- // @Success 200 {object} util.Response "成功"
- // @Router /api-sa/v1/text/delete [delete]
- func (slf TextCtl) DeleteText(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
- }
- if err := models.NewWordSearch().SetID(req.Id).Delete(); 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)
- }
|