option.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package controller
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/labring/aiproxy/core/common"
  6. "github.com/labring/aiproxy/core/middleware"
  7. "github.com/labring/aiproxy/core/model"
  8. )
  9. // GetOptions godoc
  10. //
  11. // @Summary Get options
  12. // @Description Returns a list of options
  13. // @Tags option
  14. // @Produce json
  15. // @Security ApiKeyAuth
  16. // @Success 200 {object} middleware.APIResponse{data=map[string]string}
  17. // @Router /api/option/ [get]
  18. func GetOptions(c *gin.Context) {
  19. dbOptions, err := model.GetAllOption()
  20. if err != nil {
  21. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  22. return
  23. }
  24. options := make(map[string]string, len(dbOptions))
  25. for _, option := range dbOptions {
  26. options[option.Key] = option.Value
  27. }
  28. middleware.SuccessResponse(c, options)
  29. }
  30. // GetOption godoc
  31. //
  32. // @Summary Get option
  33. // @Description Returns a single option
  34. // @Tags option
  35. // @Produce json
  36. // @Security ApiKeyAuth
  37. // @Param key path string true "Option key"
  38. // @Success 200 {object} middleware.APIResponse{data=model.Option}
  39. // @Router /api/option/{key} [get]
  40. func GetOption(c *gin.Context) {
  41. key := c.Param("key")
  42. if key == "" {
  43. middleware.ErrorResponse(c, http.StatusBadRequest, "key is required")
  44. return
  45. }
  46. option, err := model.GetOption(key)
  47. if err != nil {
  48. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  49. return
  50. }
  51. middleware.SuccessResponse(c, option)
  52. }
  53. // UpdateOption godoc
  54. //
  55. // @Summary Update option
  56. // @Description Updates a single option
  57. // @Tags option
  58. // @Produce json
  59. // @Security ApiKeyAuth
  60. // @Param value body model.Option true "Option value"
  61. // @Success 200 {object} middleware.APIResponse
  62. // @Router /api/option/ [put]
  63. // @Router /api/option/ [post]
  64. func UpdateOption(c *gin.Context) {
  65. var option model.Option
  66. err := c.BindJSON(&option)
  67. if err != nil {
  68. middleware.ErrorResponse(c, http.StatusBadRequest, err.Error())
  69. return
  70. }
  71. err = model.UpdateOption(option.Key, option.Value)
  72. if err != nil {
  73. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  74. return
  75. }
  76. middleware.SuccessResponse(c, nil)
  77. }
  78. // UpdateOptionByKey godoc
  79. //
  80. // @Summary Update option by key
  81. // @Description Updates a single option by key
  82. // @Tags option
  83. // @Produce json
  84. // @Security ApiKeyAuth
  85. // @Param key path string true "Option key"
  86. // @Param value body string true "Option value"
  87. // @Success 200 {object} middleware.APIResponse
  88. // @Router /api/option/{key} [put]
  89. func UpdateOptionByKey(c *gin.Context) {
  90. key := c.Param("key")
  91. body, err := common.GetRequestBody(c.Request)
  92. if err != nil {
  93. middleware.ErrorResponse(c, http.StatusBadRequest, err.Error())
  94. return
  95. }
  96. err = model.UpdateOption(key, string(body))
  97. if err != nil {
  98. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  99. return
  100. }
  101. middleware.SuccessResponse(c, nil)
  102. }
  103. // UpdateOptions godoc
  104. //
  105. // @Summary Update options
  106. // @Description Updates multiple options
  107. // @Tags option
  108. // @Produce json
  109. // @Security ApiKeyAuth
  110. // @Param options body map[string]string true "Options"
  111. // @Success 200 {object} middleware.APIResponse
  112. // @Router /api/option/batch [post]
  113. func UpdateOptions(c *gin.Context) {
  114. var options map[string]string
  115. err := c.BindJSON(&options)
  116. if err != nil {
  117. middleware.ErrorResponse(c, http.StatusBadRequest, err.Error())
  118. return
  119. }
  120. err = model.UpdateOptions(options)
  121. if err != nil {
  122. middleware.ErrorResponse(c, http.StatusInternalServerError, err.Error())
  123. return
  124. }
  125. middleware.SuccessResponse(c, nil)
  126. }