error_passthrough_repo.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package repository
  2. import (
  3. "context"
  4. "github.com/Wei-Shaw/sub2api/ent"
  5. "github.com/Wei-Shaw/sub2api/ent/errorpassthroughrule"
  6. "github.com/Wei-Shaw/sub2api/internal/model"
  7. "github.com/Wei-Shaw/sub2api/internal/service"
  8. )
  9. type errorPassthroughRepository struct {
  10. client *ent.Client
  11. }
  12. // NewErrorPassthroughRepository 创建错误透传规则仓库
  13. func NewErrorPassthroughRepository(client *ent.Client) service.ErrorPassthroughRepository {
  14. return &errorPassthroughRepository{client: client}
  15. }
  16. // List 获取所有规则
  17. func (r *errorPassthroughRepository) List(ctx context.Context) ([]*model.ErrorPassthroughRule, error) {
  18. rules, err := r.client.ErrorPassthroughRule.Query().
  19. Order(ent.Asc(errorpassthroughrule.FieldPriority)).
  20. All(ctx)
  21. if err != nil {
  22. return nil, err
  23. }
  24. result := make([]*model.ErrorPassthroughRule, len(rules))
  25. for i, rule := range rules {
  26. result[i] = r.toModel(rule)
  27. }
  28. return result, nil
  29. }
  30. // GetByID 根据 ID 获取规则
  31. func (r *errorPassthroughRepository) GetByID(ctx context.Context, id int64) (*model.ErrorPassthroughRule, error) {
  32. rule, err := r.client.ErrorPassthroughRule.Get(ctx, id)
  33. if err != nil {
  34. if ent.IsNotFound(err) {
  35. return nil, nil
  36. }
  37. return nil, err
  38. }
  39. return r.toModel(rule), nil
  40. }
  41. // Create 创建规则
  42. func (r *errorPassthroughRepository) Create(ctx context.Context, rule *model.ErrorPassthroughRule) (*model.ErrorPassthroughRule, error) {
  43. builder := r.client.ErrorPassthroughRule.Create().
  44. SetName(rule.Name).
  45. SetEnabled(rule.Enabled).
  46. SetPriority(rule.Priority).
  47. SetMatchMode(rule.MatchMode).
  48. SetPassthroughCode(rule.PassthroughCode).
  49. SetPassthroughBody(rule.PassthroughBody).
  50. SetSkipMonitoring(rule.SkipMonitoring)
  51. if len(rule.ErrorCodes) > 0 {
  52. builder.SetErrorCodes(rule.ErrorCodes)
  53. }
  54. if len(rule.Keywords) > 0 {
  55. builder.SetKeywords(rule.Keywords)
  56. }
  57. if len(rule.Platforms) > 0 {
  58. builder.SetPlatforms(rule.Platforms)
  59. }
  60. if rule.ResponseCode != nil {
  61. builder.SetResponseCode(*rule.ResponseCode)
  62. }
  63. if rule.CustomMessage != nil {
  64. builder.SetCustomMessage(*rule.CustomMessage)
  65. }
  66. if rule.Description != nil {
  67. builder.SetDescription(*rule.Description)
  68. }
  69. created, err := builder.Save(ctx)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return r.toModel(created), nil
  74. }
  75. // Update 更新规则
  76. func (r *errorPassthroughRepository) Update(ctx context.Context, rule *model.ErrorPassthroughRule) (*model.ErrorPassthroughRule, error) {
  77. builder := r.client.ErrorPassthroughRule.UpdateOneID(rule.ID).
  78. SetName(rule.Name).
  79. SetEnabled(rule.Enabled).
  80. SetPriority(rule.Priority).
  81. SetMatchMode(rule.MatchMode).
  82. SetPassthroughCode(rule.PassthroughCode).
  83. SetPassthroughBody(rule.PassthroughBody).
  84. SetSkipMonitoring(rule.SkipMonitoring)
  85. // 处理可选字段
  86. if len(rule.ErrorCodes) > 0 {
  87. builder.SetErrorCodes(rule.ErrorCodes)
  88. } else {
  89. builder.ClearErrorCodes()
  90. }
  91. if len(rule.Keywords) > 0 {
  92. builder.SetKeywords(rule.Keywords)
  93. } else {
  94. builder.ClearKeywords()
  95. }
  96. if len(rule.Platforms) > 0 {
  97. builder.SetPlatforms(rule.Platforms)
  98. } else {
  99. builder.ClearPlatforms()
  100. }
  101. if rule.ResponseCode != nil {
  102. builder.SetResponseCode(*rule.ResponseCode)
  103. } else {
  104. builder.ClearResponseCode()
  105. }
  106. if rule.CustomMessage != nil {
  107. builder.SetCustomMessage(*rule.CustomMessage)
  108. } else {
  109. builder.ClearCustomMessage()
  110. }
  111. if rule.Description != nil {
  112. builder.SetDescription(*rule.Description)
  113. } else {
  114. builder.ClearDescription()
  115. }
  116. updated, err := builder.Save(ctx)
  117. if err != nil {
  118. return nil, err
  119. }
  120. return r.toModel(updated), nil
  121. }
  122. // Delete 删除规则
  123. func (r *errorPassthroughRepository) Delete(ctx context.Context, id int64) error {
  124. return r.client.ErrorPassthroughRule.DeleteOneID(id).Exec(ctx)
  125. }
  126. // toModel 将 Ent 实体转换为服务模型
  127. func (r *errorPassthroughRepository) toModel(e *ent.ErrorPassthroughRule) *model.ErrorPassthroughRule {
  128. rule := &model.ErrorPassthroughRule{
  129. ID: int64(e.ID),
  130. Name: e.Name,
  131. Enabled: e.Enabled,
  132. Priority: e.Priority,
  133. ErrorCodes: e.ErrorCodes,
  134. Keywords: e.Keywords,
  135. MatchMode: e.MatchMode,
  136. Platforms: e.Platforms,
  137. PassthroughCode: e.PassthroughCode,
  138. PassthroughBody: e.PassthroughBody,
  139. SkipMonitoring: e.SkipMonitoring,
  140. CreatedAt: e.CreatedAt,
  141. UpdatedAt: e.UpdatedAt,
  142. }
  143. if e.ResponseCode != nil {
  144. rule.ResponseCode = e.ResponseCode
  145. }
  146. if e.CustomMessage != nil {
  147. rule.CustomMessage = e.CustomMessage
  148. }
  149. if e.Description != nil {
  150. rule.Description = e.Description
  151. }
  152. // 确保切片不为 nil
  153. if rule.ErrorCodes == nil {
  154. rule.ErrorCodes = []int{}
  155. }
  156. if rule.Keywords == nil {
  157. rule.Keywords = []string{}
  158. }
  159. if rule.Platforms == nil {
  160. rule.Platforms = []string{}
  161. }
  162. return rule
  163. }