gatewaygroup.go 3.9 KB

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