Skip to content

Commit 2babf81

Browse files
committed
added common code
1 parent a72f269 commit 2babf81

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

internal/sql/repository/deploymentConfig/repository.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type Repository interface {
4646
GetAppAndEnvLevelConfigsInBulk(appIdToEnvIdsMap map[int][]int) ([]*DeploymentConfig, error)
4747
GetByAppIdAndEnvIdEvenIfInactive(appId, envId int) (*DeploymentConfig, error)
4848
UpdateRepoUrlByAppIdAndEnvId(repoUrl string, appId, envId int) error
49+
GetConfigByAppIds(appIds []int) ([]*DeploymentConfig, error)
4950
}
5051

5152
type RepositoryImpl struct {
@@ -172,3 +173,12 @@ func (impl *RepositoryImpl) UpdateRepoUrlByAppIdAndEnvId(repoUrl string, appId,
172173
Update()
173174
return err
174175
}
176+
177+
func (impl *RepositoryImpl) GetConfigByAppIds(appIds []int) ([]*DeploymentConfig, error) {
178+
var results []*DeploymentConfig
179+
err := impl.dbConnection.Model(&results).
180+
Where("app_id in (?) ", pg.In(appIds)).
181+
Where("active = ?", true).
182+
Select()
183+
return results, err
184+
}

pkg/deployment/common/deploymentConfigService.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
type DeploymentConfigService interface {
2020
CreateOrUpdateConfig(tx *pg.Tx, config *bean.DeploymentConfig, userId int32) (*bean.DeploymentConfig, error)
21+
CreateOrUpdateConfigInBulk(tx *pg.Tx, configToBeCreated, configToBeUpdated []*bean.DeploymentConfig, userId int32) error
2122
IsDeploymentConfigUsed() bool
2223
GetConfigForDevtronApps(appId, envId int) (*bean.DeploymentConfig, error)
2324
GetAndMigrateConfigIfAbsentForDevtronApps(appId, envId int) (*bean.DeploymentConfig, error)
@@ -27,6 +28,7 @@ type DeploymentConfigService interface {
2728
GetAppLevelConfigForDevtronApp(appId int) (*bean.DeploymentConfig, error)
2829
UpdateRepoUrlForAppAndEnvId(repoURL string, appId, envId int) error
2930
GetDeploymentAppTypeForCDInBulk(pipelines []*pipelineConfig.Pipeline) (map[int]string, error)
31+
GetConfigsByAppIds(appIds []int) ([]*bean.DeploymentConfig, error)
3032
}
3133

3234
type DeploymentConfigServiceImpl struct {
@@ -90,6 +92,41 @@ func (impl *DeploymentConfigServiceImpl) CreateOrUpdateConfig(tx *pg.Tx, config
9092
return ConvertDeploymentConfigDbObjToDTO(newDBObj), nil
9193
}
9294

95+
func (impl *DeploymentConfigServiceImpl) CreateOrUpdateConfigInBulk(tx *pg.Tx, configToBeCreated, configToBeUpdated []*bean.DeploymentConfig, userId int32) error {
96+
97+
dbObjCreate := make([]*deploymentConfig.DeploymentConfig, len(configToBeCreated))
98+
for i := range configToBeCreated {
99+
dbObj := ConvertDeploymentConfigDTOToDbObj(configToBeCreated[i])
100+
dbObj.AuditLog.CreateAuditLog(userId)
101+
dbObjCreate = append(dbObjCreate, dbObj)
102+
}
103+
104+
dbObjUpdate := make([]*deploymentConfig.DeploymentConfig, len(configToBeUpdated))
105+
for i := range configToBeUpdated {
106+
dbObj := ConvertDeploymentConfigDTOToDbObj(configToBeUpdated[i])
107+
dbObj.AuditLog.CreateAuditLog(userId)
108+
dbObjUpdate = append(dbObjUpdate, dbObj)
109+
}
110+
111+
if len(dbObjCreate) > 0 {
112+
_, err := impl.deploymentConfigRepository.SaveAll(tx, dbObjCreate)
113+
if err != nil {
114+
impl.logger.Errorw("error in saving deploymentConfig", "dbObjCreate", dbObjCreate, "err", err)
115+
return err
116+
}
117+
}
118+
119+
if len(dbObjUpdate) > 0 {
120+
_, err := impl.deploymentConfigRepository.UpdateAll(tx, dbObjUpdate)
121+
if err != nil {
122+
impl.logger.Errorw("error in updating deploymentConfig", "dbObjUpdate", dbObjUpdate, "err", err)
123+
return err
124+
}
125+
}
126+
127+
return nil
128+
}
129+
93130
func (impl *DeploymentConfigServiceImpl) IsDeploymentConfigUsed() bool {
94131
return impl.deploymentServiceTypeConfig.UseDeploymentConfigData
95132
}
@@ -478,3 +515,19 @@ func (impl *DeploymentConfigServiceImpl) GetDeploymentAppTypeForCDInBulk(pipelin
478515
}
479516
return resp, nil
480517
}
518+
519+
func (impl *DeploymentConfigServiceImpl) GetConfigsByAppIds(appIds []int) ([]*bean.DeploymentConfig, error) {
520+
if len(appIds) == 0 {
521+
return nil, nil
522+
}
523+
configs, err := impl.deploymentConfigRepository.GetConfigByAppIds(appIds)
524+
if err != nil {
525+
impl.logger.Errorw("error in getting deployment config db object by appIds", "appIds", appIds, "err", err)
526+
return nil, err
527+
}
528+
resp := make([]*bean.DeploymentConfig, 0, len(configs))
529+
for _, config := range configs {
530+
resp = append(resp, ConvertDeploymentConfigDbObjToDTO(config))
531+
}
532+
return resp, nil
533+
}

0 commit comments

Comments
 (0)