github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/integrationsystem/service.go (about) 1 package integrationsystem 2 3 import ( 4 "context" 5 6 "github.com/kyma-incubator/compass/components/director/pkg/log" 7 8 "github.com/kyma-incubator/compass/components/director/pkg/apperrors" 9 10 "github.com/pkg/errors" 11 12 "github.com/kyma-incubator/compass/components/director/internal/model" 13 ) 14 15 // IntegrationSystemRepository missing godoc 16 //go:generate mockery --name=IntegrationSystemRepository --output=automock --outpkg=automock --case=underscore --disable-version-string 17 type IntegrationSystemRepository interface { 18 Create(ctx context.Context, item model.IntegrationSystem) error 19 Get(ctx context.Context, id string) (*model.IntegrationSystem, error) 20 Exists(ctx context.Context, id string) (bool, error) 21 List(ctx context.Context, pageSize int, cursor string) (model.IntegrationSystemPage, error) 22 Update(ctx context.Context, model model.IntegrationSystem) error 23 Delete(ctx context.Context, id string) error 24 } 25 26 // UIDService missing godoc 27 //go:generate mockery --name=UIDService --output=automock --outpkg=automock --case=underscore --disable-version-string 28 type UIDService interface { 29 Generate() string 30 } 31 32 type service struct { 33 intSysRepo IntegrationSystemRepository 34 35 uidService UIDService 36 } 37 38 // NewService missing godoc 39 func NewService(intSysRepo IntegrationSystemRepository, uidService UIDService) *service { 40 return &service{ 41 intSysRepo: intSysRepo, 42 uidService: uidService, 43 } 44 } 45 46 // Create creates an Integration System using `in` 47 func (s *service) Create(ctx context.Context, in model.IntegrationSystemInput) (string, error) { 48 id := s.uidService.Generate() 49 intSys := in.ToIntegrationSystem(id) 50 51 log.C(ctx).Debugf("Creating Integration System with name %q and id %q", in.Name, id) 52 if err := s.intSysRepo.Create(ctx, intSys); err != nil { 53 return "", errors.Wrap(err, "while creating Integration System") 54 } 55 56 return id, nil 57 } 58 59 // Get missing godoc 60 func (s *service) Get(ctx context.Context, id string) (*model.IntegrationSystem, error) { 61 intSys, err := s.intSysRepo.Get(ctx, id) 62 if err != nil { 63 return nil, errors.Wrapf(err, "while getting Integration System with ID %s", id) 64 } 65 66 return intSys, nil 67 } 68 69 // Exists missing godoc 70 func (s *service) Exists(ctx context.Context, id string) (bool, error) { 71 exist, err := s.intSysRepo.Exists(ctx, id) 72 if err != nil { 73 return false, errors.Wrapf(err, "while getting Integration System with ID %s", id) 74 } 75 76 return exist, nil 77 } 78 79 // List missing godoc 80 func (s *service) List(ctx context.Context, pageSize int, cursor string) (model.IntegrationSystemPage, error) { 81 if pageSize < 1 || pageSize > 200 { 82 return model.IntegrationSystemPage{}, apperrors.NewInvalidDataError("page size must be between 1 and 200") 83 } 84 85 return s.intSysRepo.List(ctx, pageSize, cursor) 86 } 87 88 // Update updates an Integration System with a given `id` using `in` 89 func (s *service) Update(ctx context.Context, id string, in model.IntegrationSystemInput) error { 90 intSys := in.ToIntegrationSystem(id) 91 92 log.C(ctx).Debugf("Updating Integration System with id %q", id) 93 if err := s.intSysRepo.Update(ctx, intSys); err != nil { 94 return errors.Wrapf(err, "while updating Integration System with ID %s", id) 95 } 96 97 return nil 98 } 99 100 // Delete deletes an Integration System with `id` 101 func (s *service) Delete(ctx context.Context, id string) error { 102 log.C(ctx).Debugf("Deleting Integration System with id %q", id) 103 if err := s.intSysRepo.Delete(ctx, id); err != nil { 104 return errors.Wrapf(err, "while deleting Integration System with ID %s", id) 105 } 106 107 return nil 108 }