func (s *Manager) RemovePreparedQueries(ctx context.Context, project, dbAlias, id string, params model.RequestParams) (int, error) {
	hookResponse := s.integrationMan.InvokeHook(ctx, params)
	if hookResponse.CheckResponse() {
		if err := hookResponse.Error(); err != nil {
			return hookResponse.Status(), err
		}
		return hookResponse.Status(), nil
	}
	s.lock.Lock()
	defer s.lock.Unlock()
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return http.StatusBadRequest, err
	}
	if _, p := s.checkIfDbAliasExists(projectConfig.DatabaseConfigs, dbAlias); !p {
		return http.StatusBadRequest, helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Unable to remove prepared query as provided db alias (%s) does not exists", dbAlias), nil, nil)
	}
	resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabasePreparedQuery, dbAlias, id)
	delete(projectConfig.DatabasePreparedQueries, resourceID)
	if err := s.modules.SetDatabasePreparedQueryConfig(ctx, project, projectConfig.DatabasePreparedQueries); err != nil {
		return http.StatusInternalServerError, helpers.Logger.LogError(helpers.GetRequestID(ctx), "Unable to set database prepared query config", err, nil)
	}
	if err := s.store.DeleteResource(ctx, resourceID); err != nil {
		return http.StatusInternalServerError, err
	}
	return http.StatusOK, nil
}
RemovePreparedQueries removes the database PreparedQueries
package syncman
import (
	"context"
	"errors"
	"fmt"
	"net/http"
	"time"
	"github.com/spaceuptech/helpers"
	"github.com/spaceuptech/space-cloud/gateway/model"
	"github.com/spaceuptech/space-cloud/gateway/modules/crud"
	helpers2 "github.com/spaceuptech/space-cloud/gateway/modules/schema/helpers"
	"github.com/spaceuptech/space-cloud/gateway/config"
)
func (s *Manager) SetDeleteCollection(ctx context.Context, project, dbAlias, col string, module *crud.Module, params model.RequestParams) (int, error) {
	hookResponse := s.integrationMan.InvokeHook(ctx, params)
	if hookResponse.CheckResponse() {
		if err := hookResponse.Error(); err != nil {
			return hookResponse.Status(), err
		}
		return hookResponse.Status(), nil
	}
	s.lock.Lock()
	defer s.lock.Unlock()
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return http.StatusBadRequest, err
	}
	if _, p := s.checkIfDbAliasExists(projectConfig.DatabaseConfigs, dbAlias); !p {
		return http.StatusBadRequest, helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Unable to get prepared query provided db alias (%s) does not exists", dbAlias), nil, nil)
	}
	resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseSchema, dbAlias, col)
	delete(projectConfig.DatabaseSchemas, resourceID)
	if err := s.modules.SetDatabaseSchemaConfig(ctx, project, projectConfig.DatabaseSchemas); err != nil {
		return http.StatusInternalServerError, helpers.Logger.LogError(helpers.GetRequestID(ctx), "Unable to set crud config", err, nil)
	}
	if err := module.DeleteTable(ctx, dbAlias, col); err != nil {
		return http.StatusInternalServerError, err
	}
	dbRulesResourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseRule, dbAlias, col, "rule")
	delete(projectConfig.DatabaseRules, dbRulesResourceID)
	if err := s.modules.SetDatabaseRulesConfig(ctx, project, projectConfig.DatabaseRules); err != nil {
		return http.StatusInternalServerError, helpers.Logger.LogError(helpers.GetRequestID(ctx), "Unable to set crud config", err, nil)
	}
	if err := s.store.DeleteResource(ctx, resourceID); err != nil {
		return http.StatusInternalServerError, err
	}
	if err := s.store.DeleteResource(ctx, dbRulesResourceID); err != nil {
		return http.StatusInternalServerError, err
	}
	return http.StatusOK, nil
}
func (s *Manager) SetDatabaseConnection(ctx context.Context, project, dbAlias string, v *config.DatabaseConfig, params model.RequestParams) (int, error) {
	hookResponse := s.integrationMan.InvokeHook(ctx, params)
	if hookResponse.CheckResponse() {
		if err := hookResponse.Error(); err != nil {
			return hookResponse.Status(), err
		}
		return hookResponse.Status(), nil
	}
	s.lock.Lock()
	defer s.lock.Unlock()
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return http.StatusBadRequest, err
	}
	v.DbAlias = dbAlias
	resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseConfig, dbAlias)
	if projectConfig.DatabaseConfigs == nil {
		projectConfig.DatabaseConfigs = config.DatabaseConfigs{resourceID: v}
	} else {
		projectConfig.DatabaseConfigs[resourceID] = v
	}
	if err := s.modules.SetDatabaseConfig(ctx, project, projectConfig.DatabaseConfigs, projectConfig.DatabaseSchemas, projectConfig.DatabaseRules, projectConfig.DatabasePreparedQueries); err != nil {
		return http.StatusInternalServerError, helpers.Logger.LogError(helpers.GetRequestID(ctx), "Unable to set crud config", err, nil)
	}
	if err := s.store.SetResource(ctx, resourceID, v); err != nil {
		return http.StatusInternalServerError, err
	}
	return http.StatusOK, nil
}
func (s *Manager) RemoveDatabaseConfig(ctx context.Context, project, dbAlias string, params model.RequestParams) (int, error) {
	hookResponse := s.integrationMan.InvokeHook(ctx, params)
	if hookResponse.CheckResponse() {
		if err := hookResponse.Error(); err != nil {
			return hookResponse.Status(), err
		}
		return hookResponse.Status(), nil
	}
	s.lock.Lock()
	defer s.lock.Unlock()
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return http.StatusBadRequest, err
	}
	resourceIDs := make([]string, 0)
	resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseConfig, dbAlias)
	resourceIDs = append(resourceIDs, resourceID)
	delete(projectConfig.DatabaseConfigs, resourceID)
	for _, databaseRule := range projectConfig.DatabaseRules {
		if databaseRule.DbAlias == dbAlias {
			resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseRule, dbAlias, databaseRule.Table, "rule")
			resourceIDs = append(resourceIDs, resourceID)
			delete(projectConfig.DatabaseRules, resourceID)
		}
	}
	for _, databaseSchema := range projectConfig.DatabaseSchemas {
		if databaseSchema.DbAlias == dbAlias {
			resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseSchema, dbAlias, databaseSchema.Table)
			resourceIDs = append(resourceIDs, resourceID)
			delete(projectConfig.DatabaseSchemas, resourceID)
		}
	}
	for _, preparedQuery := range projectConfig.DatabasePreparedQueries {
		if preparedQuery.DbAlias == dbAlias {
			resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabasePreparedQuery, dbAlias, preparedQuery.ID)
			resourceIDs = append(resourceIDs, resourceID)
			delete(projectConfig.DatabasePreparedQueries, resourceID)
		}
	}
	if err := s.modules.SetDatabaseConfig(ctx, project, projectConfig.DatabaseConfigs, projectConfig.DatabaseSchemas, projectConfig.DatabaseRules, projectConfig.DatabasePreparedQueries); err != nil {
		return http.StatusInternalServerError, helpers.Logger.LogError(helpers.GetRequestID(ctx), "Unable to set crud config", err, nil)
	}
	for _, resourceID := range resourceIDs {
		if err := s.store.DeleteResource(ctx, resourceID); err != nil {
			return http.StatusInternalServerError, err
		}
	}
	_ = s.modules.Caching().PurgeCache(ctx, project, &model.CachePurgeRequest{Resource: config.ResourceDatabaseSchema, DbAlias: dbAlias, ID: "*"})
	return http.StatusOK, nil
}
func (s *Manager) GetLogicalDatabaseName(ctx context.Context, project, dbAlias string) (string, error) {
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return "", err
	}
	resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabaseConfig, dbAlias)
	dbConfig, ok := projectConfig.DatabaseConfigs[resourceID]
	if !ok {
		return "", helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Unable to get logical database name provided db alias (%s) does not exists", dbAlias), nil, nil)
	}
	return dbConfig.DBName, nil
}
func (s *Manager) GetPreparedQuery(ctx context.Context, project, dbAlias, id string, params model.RequestParams) (int, []interface{}, error) {
	hookResponse := s.integrationMan.InvokeHook(ctx, params)
	if hookResponse.CheckResponse() {
		if err := hookResponse.Error(); err != nil {
			return hookResponse.Status(), nil, err
		}
		return hookResponse.Status(), hookResponse.Result().([]interface{}), nil
	}
	s.lock.Lock()
	defer s.lock.Unlock()
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return http.StatusBadRequest, nil, err
	}
	if dbAlias != "*" {
		if _, p := s.checkIfDbAliasExists(projectConfig.DatabaseConfigs, dbAlias); !p {
			return http.StatusBadRequest, nil, helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Unable to get prepared query as provided db alias (%s) does not exists", dbAlias), nil, nil)
		}
		if id != "*" {
			resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabasePreparedQuery, dbAlias, id)
			preparedQuery, ok := projectConfig.DatabasePreparedQueries[resourceID]
			if !ok {
				return http.StatusBadRequest, nil, helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Prepared query with id (%s) not present in config", id), nil, nil)
			}
			return http.StatusOK, []interface{}{preparedQuery}, nil
		}
		coll := make([]interface{}, 0)
		for _, value := range projectConfig.DatabasePreparedQueries {
			if value.DbAlias == dbAlias {
				coll = append(coll, value)
			}
		}
		return http.StatusOK, coll, nil
	}
	coll := make([]interface{}, 0)
	for _, dbPreparedQuery := range projectConfig.DatabasePreparedQueries {
		coll = append(coll, dbPreparedQuery)
	}
	return http.StatusOK, coll, nil
}
func (s *Manager) SetPreparedQueries(ctx context.Context, project, dbAlias, id string, v *config.DatbasePreparedQuery, params model.RequestParams) (int, error) {
	hookResponse := s.integrationMan.InvokeHook(ctx, params)
	if hookResponse.CheckResponse() {
		if err := hookResponse.Error(); err != nil {
			return hookResponse.Status(), err
		}
		return hookResponse.Status(), nil
	}
	s.lock.Lock()
	defer s.lock.Unlock()
	v.ID = id
	v.DbAlias = dbAlias
	projectConfig, err := s.getConfigWithoutLock(ctx, project)
	if err != nil {
		return http.StatusBadRequest, err
	}
	if _, p := s.checkIfDbAliasExists(projectConfig.DatabaseConfigs, dbAlias); !p {
		return http.StatusBadRequest, helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Unable to set prepared query as provided db alias (%s) does not exists", dbAlias), nil, nil)
	}
	resourceID := config.GenerateResourceID(s.clusterID, project, config.ResourceDatabasePreparedQuery, dbAlias, id)
	if projectConfig.DatabasePreparedQueries == nil {
		projectConfig.DatabasePreparedQueries = config.DatabasePreparedQueries{resourceID: v}
	} else {
		projectConfig.DatabasePreparedQueries[resourceID] = v
	}
	if err := s.modules.SetDatabasePreparedQueryConfig(ctx, project, projectConfig.DatabasePreparedQueries); err != nil {
		return http.StatusInternalServerError, helpers.Logger.LogError(helpers.GetRequestID(ctx), "Unable to set database prepared query config", err, nil)
	}
	if err := s.store.SetResource(ctx, resourceID, v); err != nil {
		return http.StatusInternalServerError, err
	}
	return http.StatusOK, nil
}