gatewaygroup.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. ) GatewayGroupService {
  28. return &gatewayGroupService{
  29. Service: service,
  30. gatewayGroupRepository: gatewayGroupRepository,
  31. required: required,
  32. parser: parser,
  33. }
  34. }
  35. type gatewayGroupService struct {
  36. *Service
  37. gatewayGroupRepository repository.GatewayGroupRepository
  38. required RequiredService
  39. parser ParserService
  40. }
  41. func (s *gatewayGroupService) GetGatewayGroup(ctx context.Context, id int64) (*model.GatewayGroup, error) {
  42. return s.gatewayGroupRepository.GetGatewayGroup(ctx, id)
  43. }
  44. func (s *gatewayGroupService) AddGatewayGroup(ctx context.Context, req v1.AddGateWayGroupRequest) (int, error) {
  45. formData := map[string]interface{}{
  46. "name": req.Name,
  47. "comment": req.Comment,
  48. }
  49. respBody, err := s.required.SendForm(ctx, "admin/info/waf_gateway_group/new", "admin/new/waf_gateway_group", formData)
  50. if err != nil {
  51. return 0, err
  52. }
  53. gateWayGroupIdBase, err := s.parser.GetRuleIdByColumnName(ctx, respBody, req.Name)
  54. if err != nil {
  55. return 0, err
  56. }
  57. if gateWayGroupIdBase == "" {
  58. res, err := s.parser.ParseAlert(string(respBody))
  59. if err != nil {
  60. return 0, err
  61. }
  62. return 0, fmt.Errorf(res)
  63. }
  64. gateWayGroupId, err := cast.ToIntE(gateWayGroupIdBase)
  65. if err != nil {
  66. return 0, err
  67. }
  68. return gateWayGroupId, nil
  69. }
  70. func (s *gatewayGroupService) GetGatewayGroupByHostId(ctx context.Context, hostId int) ([]model.GatewayGroup, error) {
  71. res, err := s.gatewayGroupRepository.GetGatewayGroupByHostId(ctx, int64(hostId))
  72. if err != nil {
  73. return nil, err
  74. }
  75. return *res, nil
  76. }
  77. func (s *gatewayGroupService) EditGatewayGroup(ctx context.Context, req v1.AddGateWayGroupAdminRequest) error {
  78. if err := s.gatewayGroupRepository.EditGatewayGroup(ctx, &model.GatewayGroup{
  79. Id: req.Id,
  80. Name: req.Name,
  81. Comment: req.Comment,
  82. HostId: req.HostId,
  83. RuleId: req.RuleId,
  84. BanUdp: req.BanUdp,
  85. BanOverseas: req.BanOverseas,
  86. Operator: req.Operator,
  87. }); err != nil {
  88. return err
  89. }
  90. return nil
  91. }
  92. func (s *gatewayGroupService) DeleteGatewayGroup(ctx context.Context, id int) error {
  93. if err := s.gatewayGroupRepository.DeleteGatewayGroup(ctx, id); err != nil {
  94. return err
  95. }
  96. return nil
  97. }
  98. func (s *gatewayGroupService) GetGatewayGroupList(ctx context.Context,req v1.SearchGatewayGroupParams) (*v1.PaginatedResponse[model.GatewayGroup], error) {
  99. res, err := s.gatewayGroupRepository.GetGatewayGroupList(ctx, req)
  100. if err != nil {
  101. if errors.Is(err, gorm.ErrRecordNotFound) {
  102. return nil, v1.ErrNotFound
  103. }
  104. return nil, err
  105. }
  106. return res, nil
  107. }
  108. func (s *gatewayGroupService) AddGatewayGroupAdmin(ctx context.Context,req v1.AddGateWayGroupAdminRequest) error {
  109. if err := s.gatewayGroupRepository.AddGatewayGroup(ctx, &model.GatewayGroup{
  110. Name: req.Name,
  111. Comment: req.Comment,
  112. HostId: req.HostId,
  113. RuleId: req.RuleId,
  114. BanUdp: req.BanUdp,
  115. BanOverseas: req.BanOverseas,
  116. Operator: req.Operator,
  117. }); err != nil {
  118. return err
  119. }
  120. return nil
  121. }
  122. func (s *gatewayGroupService) EditGatewayGroupAdmin(ctx context.Context, req v1.AddGateWayGroupAdminRequest) error {
  123. if err := s.gatewayGroupRepository.EditGatewayGroupById(ctx, &model.GatewayGroup{
  124. Id: req.Id,
  125. Name: req.Name,
  126. Comment: req.Comment,
  127. HostId: req.HostId,
  128. RuleId: req.RuleId,
  129. BanUdp: req.BanUdp,
  130. BanOverseas: req.BanOverseas,
  131. Operator: req.Operator,
  132. }); err != nil {
  133. return err
  134. }
  135. return nil
  136. }