cc.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/repository"
  7. )
  8. type CcService interface {
  9. GetCcList(ctx context.Context, req v1.CCListRequest) ([]v1.CCListResponse, error)
  10. EditCcState(ctx context.Context, req []v1.CCStateRequest) error
  11. }
  12. func NewCcService(
  13. service *Service,
  14. ccRepository repository.CcRepository,
  15. webForwardingRep repository.WebForwardingRepository,
  16. ) CcService {
  17. return &ccService{
  18. Service: service,
  19. ccRepository: ccRepository,
  20. webForwardingRep: webForwardingRep,
  21. }
  22. }
  23. type ccService struct {
  24. *Service
  25. ccRepository repository.CcRepository
  26. webForwardingRep repository.WebForwardingRepository
  27. }
  28. func (s *ccService) GetCcList(ctx context.Context, req v1.CCListRequest) ([]v1.CCListResponse, error) {
  29. webData, err := s.webForwardingRep.GetWebForwarding(ctx, req.WebId)
  30. if err != nil {
  31. return nil, err
  32. }
  33. if webData.CdnWebId == 0 {
  34. return nil, fmt.Errorf("网站不存在")
  35. }
  36. ccList, err := s.ccRepository.GetCcList(ctx, int64(webData.CdnWebId))
  37. if err != nil {
  38. return nil, err
  39. }
  40. return ccList, nil
  41. }
  42. func (s *ccService) EditCcState(ctx context.Context, req []v1.CCStateRequest) error {
  43. for _, v := range req {
  44. webData, err := s.webForwardingRep.GetWebForwarding(ctx, v.WebId)
  45. if err != nil {
  46. return err
  47. }
  48. if webData.CdnWebId == 0 {
  49. return fmt.Errorf("网站不存在")
  50. }
  51. err = s.ccRepository.EditCcState(ctx, int64(webData.CdnWebId), v.Ip)
  52. if err != nil {
  53. return err
  54. }
  55. }
  56. return nil
  57. }