@@ -27,6 +27,9 @@ func NewService(repo *Repo, environmentRepo *environment.Repo, projectRepo *proj
2727 }
2828}
2929
30+ // ListPostgres lists Postgres databases using already-resolved API params.
31+ // Prefer List for command-facing project/environment selectors that still need
32+ // active-workspace scope resolution.
3033func (s * Service ) ListPostgres (ctx context.Context , params * client.ListPostgresParams ) ([]* Model , error ) {
3134 postgres , err := s .repo .ListPostgres (ctx , params )
3235 if err != nil {
@@ -38,7 +41,7 @@ func (s *Service) ListPostgres(ctx context.Context, params *client.ListPostgresP
3841 return nil , err
3942 }
4043
41- var postgresModels []* Model
44+ postgresModels := []* Model {}
4245
4346 for _ , pg := range postgres {
4447 model , err := s .hydratePostgresModel (ctx , pg , projects )
@@ -53,6 +56,25 @@ func (s *Service) ListPostgres(ctx context.Context, params *client.ListPostgresP
5356 return postgresModels , nil
5457}
5558
59+ // List resolves active-workspace project/environment selectors into API params
60+ // before listing Postgres databases.
61+ func (s * Service ) List (ctx context.Context , input ListInput ) ([]* Model , error ) {
62+ params := & client.ListPostgresParams {}
63+
64+ if input .HasFilter () {
65+ envIDs , err := s .listEnvIDs (ctx , input )
66+ if err != nil {
67+ return nil , err
68+ }
69+ if len (envIDs ) == 0 {
70+ return []* Model {}, nil
71+ }
72+ params .EnvironmentId = & envIDs
73+ }
74+
75+ return s .ListPostgres (ctx , params )
76+ }
77+
5678func (s * Service ) GetPostgres (ctx context.Context , id string ) (* Model , error ) {
5779 postgres , err := s .repo .GetPostgres (ctx , id )
5880 if err != nil {
@@ -71,6 +93,10 @@ func (s *Service) RestartPostgresDatabase(ctx context.Context, id string) error
7193 return s .repo .RestartPostgresDatabase (ctx , id )
7294}
7395
96+ func (s * Service ) GetConnectionInfo (ctx context.Context , id string ) (* client.PostgresConnectionInfo , error ) {
97+ return s .repo .GetPostgresConnectionInfo (ctx , id )
98+ }
99+
74100// Resolve resolves a Postgres database by ID or name within an optional
75101// active-workspace project/environment scope.
76102func (s * Service ) Resolve (ctx context.Context , input ResolveInput ) (* client.PostgresDetail , error ) {
@@ -124,6 +150,26 @@ func (s *Service) hydratePostgresModel(ctx context.Context, postgres *client.Pos
124150 return model , nil
125151}
126152
153+ // listEnvIDs translates active-workspace project/environment selectors into
154+ // environment IDs to filter on. A valid project with no environments returns
155+ // an empty ID list, which callers should treat as an empty resource list rather
156+ // than an invalid selector.
157+ func (s * Service ) listEnvIDs (ctx context.Context , input ListInput ) ([]string , error ) {
158+ scope , err := s .resolver .ResolveScopeInActiveWorkspace (ctx , resolve.ActiveWorkspaceScopeInput {
159+ ProjectIDOrName : input .ProjectIDOrName ,
160+ EnvironmentIDOrName : input .EnvironmentIDOrName ,
161+ })
162+ if err != nil {
163+ return nil , err
164+ }
165+ if scope .Environment != nil {
166+ return []string {scope .Environment .Id }, nil
167+ }
168+ // A successful filtered scope without a single environment is a project
169+ // filter; use that project's environments as the candidate set.
170+ return scope .Project .EnvironmentIds , nil
171+ }
172+
127173func (s * Service ) environmentForPostgres (ctx context.Context , pg * client.Postgres , envs []* client.Environment ) (* client.Environment , error ) {
128174 if pg .EnvironmentId == nil {
129175 return nil , nil
0 commit comments