diff pkg/imports/config.go @ 1739:61fa62f01f20

Configured imports: When GET an import deliver the extra attributes, too.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 09 Jan 2019 17:04:02 +0100
parents 49e047c2106e
children 807569b08513
line wrap: on
line diff
--- a/pkg/imports/config.go	Wed Jan 09 16:51:21 2019 +0100
+++ b/pkg/imports/config.go	Wed Jan 09 17:04:02 2019 +0100
@@ -121,57 +121,20 @@
 WHERE import_configuration_id = $1`
 )
 
-func loadIDConfig(id int64) (*IDConfig, error) {
-
-	ctx := context.Background()
-	cfg := &IDConfig{ID: id}
+// LoadIDConfigContext loads an import configuration from database.
+func LoadIDConfigContext(ctx context.Context, conn *sql.Conn, id int64) (*IDConfig, error) {
 
-	err := auth.RunAs(ctx, configUser, func(conn *sql.Conn) error {
-		var kind ImportKind
-		var cron, url sql.NullString
-		if err := conn.QueryRowContext(ctx, loadConfigSQL, id).Scan(
-			&cfg.User,
-			&kind,
-			&cfg.SendEMail,
-			&cfg.AutoAccept,
-			&cron,
-			&url,
-		); err != nil {
-			return err
-		}
-		cfg.Kind = ImportKind(kind)
-		if cron.Valid {
-			c := CronSpec(cron.String)
-			cfg.Cron = &c
-		}
-		if url.Valid {
-			cfg.URL = &url.String
-		}
-		// load the extra attributes.
-		rows, err := conn.QueryContext(ctx, loadConfigAttributesSQL, id)
-		if err != nil {
-			return err
-		}
-		defer rows.Close()
-		var attributes common.Attributes
-		for rows.Next() {
-			var k, v string
-			if err = rows.Scan(&k, &v); err != nil {
-				return err
-			}
-			if attributes == nil {
-				attributes = common.Attributes{}
-			}
-			attributes[k] = v
-		}
-		if err = rows.Err(); err != nil {
-			return err
-		}
-		if len(attributes) > 0 {
-			cfg.Attributes = attributes
-		}
-		return nil
-	})
+	cfg := &IDConfig{ID: id}
+	var kind ImportKind
+	var cron, url sql.NullString
+	err := conn.QueryRowContext(ctx, loadConfigSQL, id).Scan(
+		&cfg.User,
+		&kind,
+		&cfg.SendEMail,
+		&cfg.AutoAccept,
+		&cron,
+		&url,
+	)
 
 	switch {
 	case err == sql.ErrNoRows:
@@ -180,5 +143,50 @@
 		return nil, err
 	}
 
+	cfg.Kind = ImportKind(kind)
+	if cron.Valid {
+		c := CronSpec(cron.String)
+		cfg.Cron = &c
+	}
+	if url.Valid {
+		cfg.URL = &url.String
+	}
+	// load the extra attributes.
+	rows, err := conn.QueryContext(ctx, loadConfigAttributesSQL, id)
+	if err != nil {
+		return nil, err
+	}
+	defer rows.Close()
+	var attributes common.Attributes
+	for rows.Next() {
+		var k, v string
+		if err = rows.Scan(&k, &v); err != nil {
+			return nil, err
+		}
+		if attributes == nil {
+			attributes = common.Attributes{}
+		}
+		attributes[k] = v
+	}
+	if err = rows.Err(); err != nil {
+		return nil, err
+	}
+	if len(attributes) > 0 {
+		cfg.Attributes = attributes
+	}
 	return cfg, nil
 }
+
+func loadIDConfig(id int64) (*IDConfig, error) {
+	return loadIDConfigContext(context.Background(), id)
+}
+
+func loadIDConfigContext(ctx context.Context, id int64) (*IDConfig, error) {
+	var cfg *IDConfig
+	err := auth.RunAs(ctx, configUser, func(conn *sql.Conn) error {
+		var err error
+		cfg, err = LoadIDConfigContext(ctx, conn, id)
+		return err
+	})
+	return cfg, err
+}