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.
 
 
 

129 line
2.6 KiB

  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. func GetAllEmailQuotaRules(c *gin.Context) {
  9. list, err := model.GetAllEmailQuotaRules()
  10. if err != nil {
  11. common.ApiError(c, err)
  12. return
  13. }
  14. common.ApiSuccess(c, list)
  15. }
  16. type CreateEmailQuotaRuleRequest struct {
  17. EmailSuffix string `json:"email_suffix" binding:"required"`
  18. Quota int64 `json:"quota" binding:"required"`
  19. Enabled *bool `json:"enabled"`
  20. Description string `json:"description"`
  21. }
  22. func CreateEmailQuotaRule(c *gin.Context) {
  23. var req CreateEmailQuotaRuleRequest
  24. if err := c.ShouldBindJSON(&req); err != nil {
  25. common.ApiError(c, err)
  26. return
  27. }
  28. existing, _ := model.GetEmailQuotaRuleBySuffix(req.EmailSuffix)
  29. if existing != nil {
  30. common.ApiErrorMsg(c, "该邮箱后缀已存在")
  31. return
  32. }
  33. enabled := true
  34. if req.Enabled != nil {
  35. enabled = *req.Enabled
  36. }
  37. rule := &model.EmailQuotaRule{
  38. EmailSuffix: req.EmailSuffix,
  39. Quota: req.Quota,
  40. Enabled: enabled,
  41. Description: req.Description,
  42. }
  43. if err := rule.Insert(); err != nil {
  44. common.ApiError(c, err)
  45. return
  46. }
  47. common.ApiSuccess(c, rule)
  48. }
  49. type UpdateEmailQuotaRuleRequest struct {
  50. EmailSuffix string `json:"email_suffix"`
  51. Quota *int64 `json:"quota"`
  52. Enabled *bool `json:"enabled"`
  53. Description *string `json:"description"`
  54. }
  55. func UpdateEmailQuotaRule(c *gin.Context) {
  56. idStr := c.Param("id")
  57. id, err := strconv.Atoi(idStr)
  58. if err != nil {
  59. common.ApiError(c, err)
  60. return
  61. }
  62. var req UpdateEmailQuotaRuleRequest
  63. if err := c.ShouldBindJSON(&req); err != nil {
  64. common.ApiError(c, err)
  65. return
  66. }
  67. rule, err := model.GetEmailQuotaRuleById(id)
  68. if err != nil {
  69. common.ApiError(c, err)
  70. return
  71. }
  72. if req.EmailSuffix != "" && req.EmailSuffix != rule.EmailSuffix {
  73. existing, _ := model.GetEmailQuotaRuleBySuffix(req.EmailSuffix)
  74. if existing != nil && existing.Id != id {
  75. common.ApiErrorMsg(c, "该邮箱后缀已存在")
  76. return
  77. }
  78. rule.EmailSuffix = req.EmailSuffix
  79. }
  80. if req.Quota != nil {
  81. rule.Quota = *req.Quota
  82. }
  83. if req.Enabled != nil {
  84. rule.Enabled = *req.Enabled
  85. }
  86. if req.Description != nil {
  87. rule.Description = *req.Description
  88. }
  89. if err := rule.Update(); err != nil {
  90. common.ApiError(c, err)
  91. return
  92. }
  93. common.ApiSuccess(c, rule)
  94. }
  95. func DeleteEmailQuotaRule(c *gin.Context) {
  96. idStr := c.Param("id")
  97. id, err := strconv.Atoi(idStr)
  98. if err != nil {
  99. common.ApiError(c, err)
  100. return
  101. }
  102. rule := &model.EmailQuotaRule{Id: id}
  103. if err := rule.Delete(); err != nil {
  104. common.ApiError(c, err)
  105. return
  106. }
  107. common.ApiSuccess(c, nil)
  108. }