gatewaygroup.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/repository"
  8. "github.com/spf13/cast"
  9. )
  10. type GatewayGroupService interface {
  11. GetGatewayGroup(ctx context.Context, id int64) (*model.GatewayGroup, error)
  12. AddGatewayGroup(ctx context.Context, req v1.AddGateWayGroupRequest) (int, error)
  13. EditGatewayGroup(ctx context.Context, group model.GatewayGroup) error
  14. DeleteGatewayGroup(ctx context.Context, group model.GatewayGroup) error
  15. GetGatewayGroupByHostId(ctx context.Context, hostId int) ([]model.GatewayGroup, error)
  16. }
  17. func NewGatewayGroupService(
  18. service *Service,
  19. gatewayGroupRepository repository.GatewayGroupRepository,
  20. required RequiredService,
  21. parser ParserService,
  22. ) GatewayGroupService {
  23. return &gatewayGroupService{
  24. Service: service,
  25. gatewayGroupRepository: gatewayGroupRepository,
  26. required: required,
  27. parser: parser,
  28. }
  29. }
  30. type gatewayGroupService struct {
  31. *Service
  32. gatewayGroupRepository repository.GatewayGroupRepository
  33. required RequiredService
  34. parser ParserService
  35. }
  36. func (s *gatewayGroupService) GetGatewayGroup(ctx context.Context, id int64) (*model.GatewayGroup, error) {
  37. return s.gatewayGroupRepository.GetGatewayGroup(ctx, id)
  38. }
  39. func (s *gatewayGroupService) AddGatewayGroup(ctx context.Context, req v1.AddGateWayGroupRequest) (int, error) {
  40. formData := map[string]interface{}{
  41. "name": req.Name,
  42. "comment": req.Comment,
  43. }
  44. respBody, err := s.required.SendForm(ctx, "admin/info/waf_gateway_group/new", "admin/new/waf_gateway_group", formData)
  45. if err != nil {
  46. return 0, err
  47. }
  48. gateWayGroupIdBase, err := s.parser.GetRuleIdByColumnName(ctx, respBody, req.Name)
  49. if err != nil {
  50. return 0, err
  51. }
  52. if gateWayGroupIdBase == "" {
  53. res, err := s.parser.ParseAlert(string(respBody))
  54. if err != nil {
  55. return 0, err
  56. }
  57. return 0, fmt.Errorf(res)
  58. }
  59. gateWayGroupId, err := cast.ToIntE(gateWayGroupIdBase)
  60. if err != nil {
  61. return 0, err
  62. }
  63. return gateWayGroupId, nil
  64. }
  65. func (s *gatewayGroupService) GetGatewayGroupByHostId(ctx context.Context, hostId int) ([]model.GatewayGroup, error) {
  66. res, err := s.gatewayGroupRepository.GetGatewayGroupByHostId(ctx, int64(hostId))
  67. if err != nil {
  68. return nil, err
  69. }
  70. return *res, nil
  71. }
  72. func (s *gatewayGroupService) EditGatewayGroup(ctx context.Context, ip model.GatewayGroup) error {
  73. if err := s.gatewayGroupRepository.EditGatewayGroup(ctx, &ip); err != nil {
  74. return err
  75. }
  76. return nil
  77. }
  78. func (s *gatewayGroupService) DeleteGatewayGroup(ctx context.Context, ip model.GatewayGroup) error {
  79. if err := s.gatewayGroupRepository.DeleteGatewayGroup(ctx, &ip); err != nil {
  80. return err
  81. }
  82. return nil
  83. }