123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package service
- import (
- "context"
- "fmt"
- v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
- "github.com/go-nunu/nunu-layout-advanced/internal/model"
- "github.com/go-nunu/nunu-layout-advanced/internal/repository"
- "github.com/spf13/cast"
- )
- type GatewayGroupService interface {
- GetGatewayGroup(ctx context.Context, id int64) (*model.GatewayGroup, error)
- AddGatewayGroup(ctx context.Context, req v1.AddGateWayGroupRequest) (int, error)
- }
- func NewGatewayGroupService(
- service *Service,
- gatewayGroupRepository repository.GatewayGroupRepository,
- required RequiredService,
- parser ParserService,
- ) GatewayGroupService {
- return &gatewayGroupService{
- Service: service,
- gatewayGroupRepository: gatewayGroupRepository,
- required: required,
- parser: parser,
- }
- }
- type gatewayGroupService struct {
- *Service
- gatewayGroupRepository repository.GatewayGroupRepository
- required RequiredService
- parser ParserService
- }
- func (s *gatewayGroupService) GetGatewayGroup(ctx context.Context, id int64) (*model.GatewayGroup, error) {
- return s.gatewayGroupRepository.GetGatewayGroup(ctx, id)
- }
- func (s *gatewayGroupService) AddGatewayGroup(ctx context.Context, req v1.AddGateWayGroupRequest) (int, error) {
- formData := map[string]interface{}{
- "name": req.Name,
- "comment": req.Comment,
- }
- respBody, err := s.required.SendForm(ctx, "admin/info/waf_gateway_group/new", "admin/new/waf_gateway_group", formData)
- if err != nil {
- return 0, err
- }
- gateWayGroupIdBase, err := s.parser.GetRuleIdByColumnName(ctx, respBody, req.Name)
- if err != nil {
- return 0, err
- }
- if gateWayGroupIdBase == "" {
- res, err := s.parser.ParseAlert(string(respBody))
- if err != nil {
- return 0, err
- }
- return 0, fmt.Errorf(res)
- }
- gateWayGroupId, err := cast.ToIntE(gateWayGroupIdBase)
- if err != nil {
- return 0, err
- }
- return gateWayGroupId, nil
- }
|