# HG changeset patch # User Sascha L. Teichmann # Date 1542387104 -3600 # Node ID 3afa71405b877cf83b78b8afef6ae869efbbe3a7 # Parent b23622905a3fc447b9f49ac99f132bff818d90e0 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. diff -r b23622905a3f -r 3afa71405b87 pkg/controllers/importqueue.go --- 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 +} diff -r b23622905a3f -r 3afa71405b87 pkg/controllers/routes.go --- a/pkg/controllers/routes.go Fri Nov 16 14:37:07 2018 +0100 +++ b/pkg/controllers/routes.go Fri Nov 16 17:51:44 2018 +0100 @@ -186,6 +186,12 @@ Handle: deleteImport, })).Methods(http.MethodDelete) + // Handler to review an import which is pending. + api.Handle("/imports/{id:[0-9]+}/{state:(?:accepted|declined)}", + waterwayAdmin(&JSONHandler{ + Handle: reviewImport, + })).Methods(http.MethodPut) + // Token handling: Login/Logout. api.HandleFunc("/login", login). Methods(http.MethodPost)