prefill_group.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package controller
  2. import (
  3. "strconv"
  4. "github.com/QuantumNous/new-api/common"
  5. "github.com/QuantumNous/new-api/model"
  6. "github.com/gin-gonic/gin"
  7. )
  8. // GetPrefillGroups 获取预填组列表,可通过 ?type=xxx 过滤
  9. func GetPrefillGroups(c *gin.Context) {
  10. groupType := c.Query("type")
  11. groups, err := model.GetAllPrefillGroups(groupType)
  12. if err != nil {
  13. common.ApiError(c, err)
  14. return
  15. }
  16. common.ApiSuccess(c, groups)
  17. }
  18. // CreatePrefillGroup 创建新的预填组
  19. func CreatePrefillGroup(c *gin.Context) {
  20. var g model.PrefillGroup
  21. if err := c.ShouldBindJSON(&g); err != nil {
  22. common.ApiError(c, err)
  23. return
  24. }
  25. if g.Name == "" || g.Type == "" {
  26. common.ApiErrorMsg(c, "组名称和类型不能为空")
  27. return
  28. }
  29. // 创建前检查名称
  30. if dup, err := model.IsPrefillGroupNameDuplicated(0, g.Name); err != nil {
  31. common.ApiError(c, err)
  32. return
  33. } else if dup {
  34. common.ApiErrorMsg(c, "组名称已存在")
  35. return
  36. }
  37. if err := g.Insert(); err != nil {
  38. common.ApiError(c, err)
  39. return
  40. }
  41. common.ApiSuccess(c, &g)
  42. }
  43. // UpdatePrefillGroup 更新预填组
  44. func UpdatePrefillGroup(c *gin.Context) {
  45. var g model.PrefillGroup
  46. if err := c.ShouldBindJSON(&g); err != nil {
  47. common.ApiError(c, err)
  48. return
  49. }
  50. if g.Id == 0 {
  51. common.ApiErrorMsg(c, "缺少组 ID")
  52. return
  53. }
  54. // 名称冲突检查
  55. if dup, err := model.IsPrefillGroupNameDuplicated(g.Id, g.Name); err != nil {
  56. common.ApiError(c, err)
  57. return
  58. } else if dup {
  59. common.ApiErrorMsg(c, "组名称已存在")
  60. return
  61. }
  62. if err := g.Update(); err != nil {
  63. common.ApiError(c, err)
  64. return
  65. }
  66. common.ApiSuccess(c, &g)
  67. }
  68. // DeletePrefillGroup 删除预填组
  69. func DeletePrefillGroup(c *gin.Context) {
  70. idStr := c.Param("id")
  71. id, err := strconv.Atoi(idStr)
  72. if err != nil {
  73. common.ApiError(c, err)
  74. return
  75. }
  76. if err := model.DeletePrefillGroupByID(id); err != nil {
  77. common.ApiError(c, err)
  78. return
  79. }
  80. common.ApiSuccess(c, nil)
  81. }