prefill_group.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package controller
  2. import (
  3. "strconv"
  4. "one-api/common"
  5. "one-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. if err := g.Insert(); err != nil {
  30. common.ApiError(c, err)
  31. return
  32. }
  33. common.ApiSuccess(c, &g)
  34. }
  35. // UpdatePrefillGroup 更新预填组
  36. func UpdatePrefillGroup(c *gin.Context) {
  37. var g model.PrefillGroup
  38. if err := c.ShouldBindJSON(&g); err != nil {
  39. common.ApiError(c, err)
  40. return
  41. }
  42. if g.Id == 0 {
  43. common.ApiErrorMsg(c, "缺少组 ID")
  44. return
  45. }
  46. if err := g.Update(); err != nil {
  47. common.ApiError(c, err)
  48. return
  49. }
  50. common.ApiSuccess(c, &g)
  51. }
  52. // DeletePrefillGroup 删除预填组
  53. func DeletePrefillGroup(c *gin.Context) {
  54. idStr := c.Param("id")
  55. id, err := strconv.Atoi(idStr)
  56. if err != nil {
  57. common.ApiError(c, err)
  58. return
  59. }
  60. if err := model.DeletePrefillGroupByID(id); err != nil {
  61. common.ApiError(c, err)
  62. return
  63. }
  64. common.ApiSuccess(c, nil)
  65. }