text.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package controllers
  2. import (
  3. "errors"
  4. "github.com/gin-gonic/gin"
  5. "speechAnalysis/extend/code"
  6. "speechAnalysis/extend/util"
  7. "speechAnalysis/models"
  8. "speechAnalysis/pkg/logx"
  9. "speechAnalysis/request"
  10. )
  11. type TextCtl struct{}
  12. // AddText
  13. // @Tags 文字库
  14. // @Summary 新增文字
  15. // @Produce application/json
  16. // @Param object body request.AddTextReq true "参数"
  17. // @Success 200 {object} util.Response "成功"
  18. // @Router /api-sa/v1/text/add [post]
  19. func (slf TextCtl) AddText(c *gin.Context) {
  20. var req request.AddTextReq
  21. if err := c.BindJSON(&req); err != nil {
  22. logx.Errorf("add text params err:%v", err)
  23. util.ResponseFormat(c, code.RequestParamError, err.Error())
  24. return
  25. }
  26. text := models.Word{
  27. Content: req.Content,
  28. LocomotiveNumber: req.LocomotiveNumber,
  29. }
  30. if err := slf.paramsCheck(text); err != nil {
  31. util.ResponseFormat(c, code.RequestParamError, err.Error())
  32. return
  33. }
  34. if err := models.NewWordSearch().Create(&text); err != nil {
  35. util.ResponseFormat(c, code.SaveFail, "添加失败,请检查是否重复")
  36. return
  37. }
  38. util.ResponseFormat(c, code.Success, "添加成功")
  39. }
  40. func (slf TextCtl) paramsCheck(text models.Word) (err error) {
  41. if text.Content == "" || text.LocomotiveNumber == "" {
  42. return errors.New("参数缺失")
  43. }
  44. _, err = models.NewWordSearch().SetLocomotiveNumber(text.LocomotiveNumber).SetContent(text.Content).First()
  45. if err == nil {
  46. return errors.New("文字重复")
  47. }
  48. return nil
  49. }
  50. // List
  51. // @Tags 文字库
  52. // @Summary 文字库列表
  53. // @Produce application/json
  54. // @Param object query request.GetTextList true "参数"
  55. // @Success 200 {object} util.ResponseList{data=[]models.Word} "成功"
  56. // @Router /api-sa/v1/text/list [get]
  57. func (slf TextCtl) List(c *gin.Context) {
  58. var params request.GetTextList
  59. if err := c.ShouldBindQuery(&params); err != nil {
  60. util.ResponseFormat(c, code.RequestParamError, err.Error())
  61. return
  62. }
  63. if !params.PageInfo.Check() {
  64. util.ResponseFormat(c, code.RequestParamError, "分页参数错误")
  65. return
  66. }
  67. list, total, err := models.NewWordSearch().
  68. SetPage(params.Page, params.PageSize).
  69. SetKeyword(params.Keyword).
  70. Find()
  71. if err != nil {
  72. util.ResponseFormat(c, code.RequestParamError, "查找失败")
  73. return
  74. }
  75. util.ResponseFormatList(c, code.Success, list, total)
  76. }