# HG changeset patch # User Sascha L. Teichmann # Date 1547049842 -3600 # Node ID 61fa62f01f202f4840caee2bbd414ac3fda6be3d # Parent 8202d485331b8d90ce64330539f35fc0f45b2a0b Configured imports: When GET an import deliver the extra attributes, too. diff -r 8202d485331b -r 61fa62f01f20 pkg/controllers/importconfig.go --- a/pkg/controllers/importconfig.go Wed Jan 09 16:51:21 2019 +0100 +++ b/pkg/controllers/importconfig.go Wed Jan 09 17:04:02 2019 +0100 @@ -229,41 +229,18 @@ id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64) - var ( - entry imports.IDConfig - kind string - cron sql.NullString - url sql.NullString - ) + var entry *imports.IDConfig - err = conn.QueryRowContext(ctx, selectImportConfigurationIDSQL, id).Scan( - &entry.ID, - &entry.User, - &kind, - &entry.SendEMail, - &entry.AutoAccept, - &cron, - &url, - ) - + entry, err = imports.LoadIDConfigContext(ctx, conn, id) switch { - case err == sql.ErrNoRows: + case err != nil: + return + case entry == nil: err = JSONError{ Code: http.StatusNotFound, Message: fmt.Sprintf("No schedule %d found", id), } return - case err != nil: - return - } - - entry.Kind = imports.ImportKind(kind) - if cron.Valid { - cs := imports.CronSpec(cron.String) - entry.Cron = &cs - } - if url.Valid { - entry.URL = &url.String } jr = JSONResult{Result: &entry} diff -r 8202d485331b -r 61fa62f01f20 pkg/imports/config.go --- 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 +}