Mercurial > gemma
changeset 1592:c12cec1d7692
Added PATCH /imports/scheduler/{id:[0-9]+} to modify a single import configuration.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 14 Dec 2018 13:25:19 +0100 |
parents | 2d53065c95af |
children | bbbc27a7ec99 |
files | pkg/controllers/routes.go pkg/controllers/scheduler.go pkg/models/scheduler.go |
diffstat | 3 files changed, 118 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/controllers/routes.go Fri Dec 14 12:49:44 2018 +0100 +++ b/pkg/controllers/routes.go Fri Dec 14 13:25:19 2018 +0100 @@ -178,6 +178,11 @@ // Import scheduler configuration api.Handle("/imports/scheduler/{id:[0-9]+}", waterwayAdmin(&JSONHandler{ + Handle: modifySchedule, + })).Methods(http.MethodPatch) + + api.Handle("/imports/scheduler/{id:[0-9]+}", + waterwayAdmin(&JSONHandler{ Handle: deleteSchedule, })).Methods(http.MethodDelete)
--- a/pkg/controllers/scheduler.go Fri Dec 14 12:49:44 2018 +0100 +++ b/pkg/controllers/scheduler.go Fri Dec 14 13:25:19 2018 +0100 @@ -57,8 +57,118 @@ deleteImportConfiguationSQL = ` DELETE FROM waterway.import_configuration WHERE id = $1` + + updateImportConfigurationSQL = ` +UPDATE waterway.import_configuration SET + username = $2 + kind = $3, + cron = $4, + url = $5, + send_email = $6, + auto_accept = $7 +WHERE id = $1 +` ) +func modifySchedule( + input interface{}, + req *http.Request, + conn *sql.Conn, +) (jr JSONResult, err error) { + + ctx := req.Context() + + importConfig := input.(*models.ImportConfig) + + id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64) + + var tx *sql.Tx + + if tx, err = conn.BeginTx(ctx, nil); err != nil { + return + } + defer tx.Rollback() + + var ( + entry models.IDImportConfig + kind string + cron sql.NullString + url sql.NullString + ) + + err = conn.QueryRowContext(ctx, selectImportConfigurationIDSQL, id).Scan( + &entry.ID, + &entry.User, + &kind, + &entry.SendEMail, + &entry.AutoAccept, + &cron, + &url, + ) + + switch { + case err == sql.ErrNoRows: + err = JSONError{ + Code: http.StatusNotFound, + Message: fmt.Sprintf("No schedule %d found", id), + } + return + case err != nil: + return + } + + session, _ := auth.GetSession(req) + + entry.SendEMail = importConfig.SendEMail + entry.AutoAccept = importConfig.AutoAccept + + if importConfig.Cron != nil { + cron = sql.NullString{String: string(*importConfig.Cron), Valid: true} + } + + if importConfig.URL != nil { + url = sql.NullString{String: *importConfig.URL, Valid: true} + } + + if _, err = tx.ExecContext(ctx, updateImportConfigurationSQL, + id, + session.User, + string(importConfig.Kind), + cron, + url, + importConfig.SendEMail, + importConfig.AutoAccept, + ); err != nil { + return + } + + scheduler.UnbindByID(id) + + if cron.Valid { + if err = scheduler.BindAction( + string(importConfig.Kind), + cron.String, + session.User, + &id, + ); err != nil { + return + } + } + + if err = tx.Commit(); err != nil { + return + } + + var result = struct { + ID int64 `json:"id"` + }{ + ID: id, + } + + jr = JSONResult{Result: &result} + return +} + func infoSchedule( _ interface{}, req *http.Request, @@ -78,6 +188,7 @@ err = conn.QueryRowContext(ctx, selectImportConfigurationIDSQL, id).Scan( &entry.ID, + &entry.User, &kind, &entry.SendEMail, &entry.AutoAccept, @@ -258,6 +369,7 @@ ) if err = rows.Scan( &entry.ID, + &entry.User, &kind, &entry.SendEMail, &entry.AutoAccept,
--- a/pkg/models/scheduler.go Fri Dec 14 12:49:44 2018 +0100 +++ b/pkg/models/scheduler.go Fri Dec 14 13:25:19 2018 +0100 @@ -36,6 +36,7 @@ IDImportConfig struct { ID int64 `json:"id"` + User string `json:"user"` Kind ImportKind `json:"kind"` SendEMail bool `json:"send-email"` AutoAccept bool `json:"auto-accept"`