changeset 1591:2d53065c95af

Added GET /imports/scheduler/{id:[0-9]+} to show infos about single import configuration.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 14 Dec 2018 12:49:44 +0100
parents 2fdd8e57542d
children c12cec1d7692
files pkg/controllers/routes.go pkg/controllers/scheduler.go
diffstat 2 files changed, 63 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/routes.go	Fri Dec 14 12:34:12 2018 +0100
+++ b/pkg/controllers/routes.go	Fri Dec 14 12:49:44 2018 +0100
@@ -181,6 +181,11 @@
 			Handle: deleteSchedule,
 		})).Methods(http.MethodDelete)
 
+	api.Handle("/imports/scheduler/{id:[0-9]+}",
+		waterwayAdmin(&JSONHandler{
+			Handle: infoSchedule,
+		})).Methods(http.MethodGet)
+
 	api.Handle("/imports/scheduler",
 		waterwayAdmin(&JSONHandler{
 			Input:  func() interface{} { return new(models.ImportConfig) },
--- a/pkg/controllers/scheduler.go	Fri Dec 14 12:34:12 2018 +0100
+++ b/pkg/controllers/scheduler.go	Fri Dec 14 12:49:44 2018 +0100
@@ -27,7 +27,7 @@
 )
 
 const (
-	selectImportConfigurationSQL = `
+	selectImportConfigurationPrefix = `
 SELECT
   id,
   username,
@@ -36,9 +36,14 @@
   auto_accept,
   cron,
   url
-FROM waterway.import_configuration
+FROM waterway.import_configuration`
+
+	selectImportConfigurationSQL = selectImportConfigurationPrefix + `
 ORDER by id`
 
+	selectImportConfigurationIDSQL = selectImportConfigurationPrefix + `
+WHERE id = $1`
+
 	insertImportConfigurationSQL = `
 INSERT INTO waterway.import_configuration
 (username, kind, cron, send_email, auto_accept, url)
@@ -54,6 +59,56 @@
 WHERE id = $1`
 )
 
+func infoSchedule(
+	_ interface{},
+	req *http.Request,
+	conn *sql.Conn,
+) (jr JSONResult, err error) {
+
+	ctx := req.Context()
+
+	id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64)
+
+	var (
+		entry models.IDImportConfig
+		kind  string
+		cron  sql.NullString
+		url   sql.NullString
+	)
+
+	err = conn.QueryRowContext(ctx, selectImportConfigurationIDSQL, id).Scan(
+		&entry.ID,
+		&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
+	}
+
+	entry.Kind = models.ImportKind(kind)
+	if cron.Valid {
+		cs := models.CronSpec(cron.String)
+		entry.Cron = &cs
+	}
+	if url.Valid {
+		entry.URL = &url.String
+	}
+
+	jr = JSONResult{Result: &entry}
+	return
+}
+
 func deleteSchedule(
 	_ interface{},
 	req *http.Request,
@@ -207,6 +262,7 @@
 			&entry.SendEMail,
 			&entry.AutoAccept,
 			&cron,
+			&url,
 		); err != nil {
 			return
 		}