Mercurial > gemma
diff pkg/controllers/importconfig.go @ 2052:0b203a3b3e8e unify_imports
Imports: Re-enabled /imports/config/{id} PATCH.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sun, 27 Jan 2019 23:09:42 +0100 |
parents | 7d627258e045 |
children | c64c47ff2ab1 |
line wrap: on
line diff
--- a/pkg/controllers/importconfig.go Sun Jan 27 22:40:32 2019 +0100 +++ b/pkg/controllers/importconfig.go Sun Jan 27 23:09:42 2019 +0100 @@ -28,18 +28,6 @@ "gemma.intevation.de/gemma/pkg/scheduler" ) -/* -const ( - selectImportConfigurationIDSQL = ` -SELECT - id, - username, - kind -FROM import.import_configuration -WHERE id = $1` -) -*/ - func runImportConfig( _ interface{}, req *http.Request, @@ -74,107 +62,83 @@ conn *sql.Conn, ) (jr JSONResult, err error) { - /* - - ctx := req.Context() - - importConfig := input.(*imports.Config) + ctx := req.Context() - id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64) - - var tx *sql.Tx + raw := input.(*json.RawMessage) - if tx, err = conn.BeginTx(ctx, nil); err != nil { - return - } - defer tx.Rollback() + id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64) - var ( - entry imports.IDConfig - kind string - dummy sql.NullString - url sql.NullString - ) - - err = conn.QueryRowContext(ctx, selectImportConfigurationIDSQL, id).Scan( - &entry.ID, - &entry.User, - &kind, - &entry.SendEMail, - &dummy, - &url, - ) + var pc *imports.PersistentConfig + pc, err = imports.LoadPersistentConfigContext(ctx, conn, id) + switch { + case err == sql.ErrNoRows: + err = JSONError{ + Code: http.StatusNotFound, + Message: fmt.Sprintf("No configuration %d found", id), + } + return + case err != nil: + return + } - switch { - case err == sql.ErrNoRows: - err = JSONError{ - Code: http.StatusNotFound, - Message: fmt.Sprintf("No schedule %d found", id), - } - return - case err != nil: - return + kind := imports.JobKind(pc.Kind) + ctor := imports.ImportModelForJobKind(kind) + if ctor == nil { + err = JSONError{ + Code: http.StatusInternalServerError, + Message: fmt.Sprintf("No constructor for kind '%s' found", pc.Kind), } + return + } + config := ctor() + if err = json.Unmarshal(*raw, config); err != nil { + return + } - session, _ := auth.GetSession(req) + _, oldCron := pc.Attributes.Get("cron") - entry.SendEMail = importConfig.SendEMail + session, _ := auth.GetSession(req) + pc.User = session.User + pc.Attributes = common.Attributes{} + pc.Attributes.Marshal(config) - // We always take the cron spec from the input. - // If there is no spec remove schedule. - var cron sql.NullString - if importConfig.Cron != nil { - cron = sql.NullString{String: string(*importConfig.Cron), Valid: true} - } + cron, newCron := pc.Attributes.Get("cron") + + var tx *sql.Tx + if tx, err = conn.BeginTx(ctx, nil); err != nil { + return + } + defer tx.Rollback() - if importConfig.URL != nil { - url = sql.NullString{String: *importConfig.URL, Valid: true} - } + if err = pc.UpdateContext(ctx, tx); err != nil { + return + } - if _, err = tx.ExecContext(ctx, updateImportConfigurationSQL, + if oldCron { + scheduler.UnbindByID(id) + } + + if newCron { + if err = scheduler.BindAction( + string(pc.Kind), + cron, id, - session.User, - string(importConfig.Kind), - cron, - url, - importConfig.SendEMail, ); err != nil { return } + } - if importConfig.Attributes != nil { - if _, err = tx.ExecContext(ctx, deleteImportConfiguationAttributesSQL, id); err != nil { - return - } - if err = storeConfigAttributes(ctx, tx, id, importConfig.Attributes); err != nil { - return - } - } - - scheduler.UnbindByID(id) + if err = tx.Commit(); err != nil { + return + } - if cron.Valid { - if err = scheduler.BindAction( - string(importConfig.Kind), - cron.String, - id, - ); err != nil { - return - } - } + var result = struct { + ID int64 `json:"id"` + }{ + ID: id, + } - if err = tx.Commit(); err != nil { - return - } - - var result = struct { - ID int64 `json:"id"` - }{ - ID: id, - } - - jr = JSONResult{Result: &result} - */ + jr = JSONResult{Result: &result} return } @@ -210,6 +174,7 @@ Code: http.StatusInternalServerError, Message: fmt.Sprintf("No constructor for kind '%s' found", cfg.Kind), } + return } what := ctor()