You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

117 lines
2.1 KiB

  1. package controller
  2. import (
  3. "net/http"
  4. "strconv"
  5. "github.com/QuantumNous/new-api/common"
  6. "github.com/QuantumNous/new-api/model"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func GetUserChannelRatios(c *gin.Context) {
  10. userIdStr := c.Param("user_id")
  11. userId, err := strconv.Atoi(userIdStr)
  12. if err != nil {
  13. common.ApiError(c, err)
  14. return
  15. }
  16. ratios, err := model.GetUserChannelRatiosByUserId(userId)
  17. if err != nil {
  18. common.ApiError(c, err)
  19. return
  20. }
  21. c.JSON(http.StatusOK, gin.H{
  22. "success": true,
  23. "data": ratios,
  24. })
  25. }
  26. type createUserChannelRatioRequest struct {
  27. UserId int `json:"user_id"`
  28. ModelName string `json:"model_name"`
  29. ChannelId int `json:"channel_id"`
  30. Ratio float64 `json:"ratio"`
  31. }
  32. func CreateUserChannelRatio(c *gin.Context) {
  33. var req createUserChannelRatioRequest
  34. if err := c.ShouldBindJSON(&req); err != nil {
  35. common.ApiError(c, err)
  36. return
  37. }
  38. if req.Ratio <= 0 {
  39. common.ApiErrorMsg(c, "ratio must be positive")
  40. return
  41. }
  42. ucr := &model.UserChannelRatio{
  43. UserId: req.UserId,
  44. ModelName: req.ModelName,
  45. ChannelId: req.ChannelId,
  46. Ratio: req.Ratio,
  47. }
  48. if err := ucr.Insert(); err != nil {
  49. common.ApiError(c, err)
  50. return
  51. }
  52. c.JSON(http.StatusOK, gin.H{
  53. "success": true,
  54. "data": ucr,
  55. })
  56. }
  57. func UpdateUserChannelRatio(c *gin.Context) {
  58. idStr := c.Param("id")
  59. id, err := strconv.Atoi(idStr)
  60. if err != nil {
  61. common.ApiError(c, err)
  62. return
  63. }
  64. var req struct {
  65. Ratio float64 `json:"ratio"`
  66. }
  67. if err := c.ShouldBindJSON(&req); err != nil {
  68. common.ApiError(c, err)
  69. return
  70. }
  71. if req.Ratio <= 0 {
  72. common.ApiErrorMsg(c, "ratio must be positive")
  73. return
  74. }
  75. ucr := &model.UserChannelRatio{
  76. Id: id,
  77. Ratio: req.Ratio,
  78. }
  79. if err := ucr.Update(); err != nil {
  80. common.ApiError(c, err)
  81. return
  82. }
  83. c.JSON(http.StatusOK, gin.H{
  84. "success": true,
  85. "data": ucr,
  86. })
  87. }
  88. func DeleteUserChannelRatio(c *gin.Context) {
  89. idStr := c.Param("id")
  90. id, err := strconv.Atoi(idStr)
  91. if err != nil {
  92. common.ApiError(c, err)
  93. return
  94. }
  95. if err := model.DeleteUserChannelRatioById(id); err != nil {
  96. common.ApiError(c, err)
  97. return
  98. }
  99. c.JSON(http.StatusOK, gin.H{
  100. "success": true,
  101. })
  102. }