diff pkg/controllers/importqueue.go @ 1192:3afa71405b87

Added REST endpoint to accept or decline an import. Use PUT /api/imports/{id:[0-9]+}/accepted to accept an import. Use PUT /api/imports/{id:[0-9]+}/declined to decline an import. TODO: Do some db magic to store the decision.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 16 Nov 2018 17:51:44 +0100
parents 3d50f558870c
children 58acc343b1b6
line wrap: on
line diff
--- a/pkg/controllers/importqueue.go	Fri Nov 16 14:37:07 2018 +0100
+++ b/pkg/controllers/importqueue.go	Fri Nov 16 17:51:44 2018 +0100
@@ -289,3 +289,57 @@
 
 	return
 }
+
+const (
+	isPendingSQL = `
+SELECT state = 'pending'::waterway.import_state WHERE id = $1`
+)
+
+func reviewImport(
+	_ interface{},
+	req *http.Request,
+	conn *sql.Conn,
+) (jr JSONResult, err error) {
+
+	vars := mux.Vars(req)
+	id, _ := strconv.ParseInt(vars["id"], 10, 64)
+	state := vars["state"]
+
+	ctx := req.Context()
+	var tx *sql.Tx
+	if tx, err = conn.BeginTx(ctx, nil); err != nil {
+		return
+	}
+	defer tx.Rollback()
+
+	var pending bool
+
+	err = tx.QueryRowContext(ctx, isPendingSQL, id).Scan(&pending)
+	switch {
+	case err == sql.ErrNoRows:
+		err = JSONError{
+			Code:    http.StatusNotFound,
+			Message: fmt.Sprintf("Cannot find import #%d.", id),
+		}
+		return
+	case err != nil:
+		return
+	case !pending:
+		err = JSONError{
+			Code:    http.StatusBadRequest,
+			Message: fmt.Sprintf("Import %d is not pending.", id),
+		}
+		return
+	}
+
+	if state == "accepted" {
+		// TODO: Call a stored procedure to do the grunt stuff
+		//       inside the database.
+	} else {
+	}
+
+	if err = tx.Commit(); err != nil {
+		return
+	}
+	return
+}