gatewaygroup.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  8. "github.com/go-nunu/nunu-layout-advanced/internal/repository"
  9. "github.com/spf13/cast"
  10. "gorm.io/gorm"
  11. )
  12. type GatewayGroupService interface {
  13. GetGatewayGroup(ctx context.Context, id int64) (*model.GatewayGroup, error)
  14. AddGatewayGroup(ctx context.Context, req v1.AddGateWayGroupRequest) (int, error)
  15. EditGatewayGroup(ctx context.Context, req v1.AddGateWayGroupAdminRequest) error
  16. DeleteGatewayGroup(ctx context.Context, id int) error
  17. GetGatewayGroupByHostId(ctx context.Context, hostId int) ([]model.GatewayGroup, error)
  18. GetGatewayGroupList(ctx context.Context,req v1.SearchGatewayGroupParams) (*v1.PaginatedResponse[model.GatewayGroup] , error)
  19. AddGatewayGroupAdmin(ctx context.Context,req v1.AddGateWayGroupAdminRequest) error
  20. EditGatewayGroupAdmin(ctx context.Context, req v1.AddGateWayGroupAdminRequest) error
  21. }
  22. func NewGatewayGroupService(
  23. service *Service,
  24. gatewayGroupRepository repository.GatewayGroupRepository,
  25. required RequiredService,
  26. parser ParserService,
  27. request RequestService,
  28. ) GatewayGroupService {
  29. return &gatewayGroupService{
  30. Service: service,
  31. gatewayGroupRepository: gatewayGroupRepository,
  32. required: required,
  33. parser: parser,
  34. request: request,
  35. }
  36. }
  37. type gatewayGroupService struct {
  38. *Service
  39. gatewayGroupRepository repository.GatewayGroupRepository
  40. required RequiredService
  41. parser ParserService
  42. request RequestService
  43. }
  44. func (s *gatewayGroupService) GetGatewayGroup(ctx context.Context, id int64) (*model.GatewayGroup, error) {
  45. return s.gatewayGroupRepository.GetGatewayGroup(ctx, id)
  46. }
  47. func (s *gatewayGroupService) AddGatewayGroup(ctx context.Context, req v1.AddGateWayGroupRequest) (int, error) {
  48. formData := map[string]interface{}{
  49. "name": req.Name,
  50. "comment": req.Comment,
  51. }
  52. respBody, err := s.required.SendForm(ctx, "admin/info/waf_gateway_group/new", "admin/new/waf_gateway_group", formData)
  53. if err != nil {
  54. return 0, err
  55. }
  56. gateWayGroupIdBase, err := s.parser.GetRuleIdByColumnName(ctx, respBody, req.Name)
  57. if err != nil {
  58. return 0, err
  59. }
  60. if gateWayGroupIdBase == "" {
  61. res, err := s.parser.ParseAlert(string(respBody))
  62. if err != nil {
  63. return 0, err
  64. }
  65. return 0, fmt.Errorf(res)
  66. }
  67. gateWayGroupId, err := cast.ToIntE(gateWayGroupIdBase)
  68. if err != nil {
  69. return 0, err
  70. }
  71. return gateWayGroupId, nil
  72. }
  73. func (s *gatewayGroupService) GetGatewayGroupByHostId(ctx context.Context, hostId int) ([]model.GatewayGroup, error) {
  74. res, err := s.gatewayGroupRepository.GetGatewayGroupByHostId(ctx, int64(hostId))
  75. if err != nil {
  76. return nil, err
  77. }
  78. return *res, nil
  79. }
  80. func (s *gatewayGroupService) EditGatewayGroup(ctx context.Context, req v1.AddGateWayGroupAdminRequest) error {
  81. if err := s.gatewayGroupRepository.EditGatewayGroup(ctx, &model.GatewayGroup{
  82. Id: req.Id,
  83. Name: req.Name,
  84. Comment: req.Comment,
  85. HostId: req.HostId,
  86. RuleId: req.RuleId,
  87. BanUdp: req.BanUdp,
  88. BanOverseas: req.BanOverseas,
  89. Operator: req.Operator,
  90. }); err != nil {
  91. return err
  92. }
  93. return nil
  94. }
  95. func (s *gatewayGroupService) DeleteGatewayGroup(ctx context.Context, id int) error {
  96. if err := s.gatewayGroupRepository.DeleteGatewayGroup(ctx, id); err != nil {
  97. return err
  98. }
  99. return nil
  100. }
  101. func (s *gatewayGroupService) GetGatewayGroupList(ctx context.Context,req v1.SearchGatewayGroupParams) (*v1.PaginatedResponse[model.GatewayGroup], error) {
  102. res, err := s.gatewayGroupRepository.GetGatewayGroupList(ctx, req)
  103. if err != nil {
  104. if errors.Is(err, gorm.ErrRecordNotFound) {
  105. return nil, v1.ErrNotFound
  106. }
  107. return nil, err
  108. }
  109. return res, nil
  110. }
  111. func (s *gatewayGroupService) AddGatewayGroupAdmin(ctx context.Context,req v1.AddGateWayGroupAdminRequest) error {
  112. if err := s.gatewayGroupRepository.AddGatewayGroup(ctx, &model.GatewayGroup{
  113. Name: req.Name,
  114. Comment: req.Comment,
  115. HostId: req.HostId,
  116. RuleId: req.RuleId,
  117. BanUdp: req.BanUdp,
  118. BanOverseas: req.BanOverseas,
  119. Operator: req.Operator,
  120. }); err != nil {
  121. return err
  122. }
  123. return nil
  124. }
  125. func (s *gatewayGroupService) EditGatewayGroupAdmin(ctx context.Context, req v1.AddGateWayGroupAdminRequest) error {
  126. if err := s.gatewayGroupRepository.EditGatewayGroupById(ctx, &model.GatewayGroup{
  127. Id: req.Id,
  128. Name: req.Name,
  129. Comment: req.Comment,
  130. HostId: req.HostId,
  131. RuleId: req.RuleId,
  132. BanUdp: req.BanUdp,
  133. BanOverseas: req.BanOverseas,
  134. Operator: req.Operator,
  135. }); err != nil {
  136. return err
  137. }
  138. return nil
  139. }