comparison pkg/controllers/importqueue.go @ 1023:337a7f4c8a16

Add endpoint to list all or some import jobs. Unpaged path: /api/imports Paged path: /api/imports?offset=[0-9+]&limit=[0-9]+
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 24 Oct 2018 12:24:26 +0200
parents
children 3de54d7b7d30
comparison
equal deleted inserted replaced
1022:74229d9f7028 1023:337a7f4c8a16
1 // This is Free Software under GNU Affero General Public License v >= 3.0
2 // without warranty, see README.md and license for details.
3 //
4 // SPDX-License-Identifier: AGPL-3.0-or-later
5 // License-Filename: LICENSES/AGPL-3.0.txt
6 //
7 // Copyright (C) 2018 by via donau
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
9 // Software engineering by Intevation GmbH
10 //
11 // Author(s):
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
13
14 package controllers
15
16 import (
17 "database/sql"
18 "net/http"
19 "strconv"
20
21 "gemma.intevation.de/gemma/pkg/models"
22 "github.com/gorilla/mux"
23 )
24
25 const (
26 selectImportsUnpagedSQL = `
27 SELECT
28 id,
29 state::varchar,
30 enqueued,
31 kind,
32 username
33 FROM waterway.imports
34 ORDER BY id`
35
36 selectImportPagedSQL = selectImportsUnpagedSQL + `
37 LIMIT $1 OFFSET $2`
38 )
39
40 func listImports(
41 _ interface{},
42 req *http.Request,
43 conn *sql.Conn,
44 ) (jr JSONResult, err error) {
45 vars := mux.Vars(req)
46
47 off, of := vars["offset"]
48 lim, lf := vars["limit"]
49
50 var rows *sql.Rows
51
52 if of && lf {
53 offset, _ := strconv.ParseInt(off, 10, 64)
54 limit, _ := strconv.ParseInt(lim, 10, 64)
55 rows, err = conn.QueryContext(
56 req.Context(), selectImportPagedSQL, limit, offset)
57 } else {
58 rows, err = conn.QueryContext(
59 req.Context(), selectImportsUnpagedSQL)
60 }
61 if err != nil {
62 return
63 }
64 defer rows.Close()
65
66 imports := make([]*models.Import, 0, 20)
67
68 for rows.Next() {
69 var it models.Import
70 if err = rows.Scan(
71 &it.ID,
72 &it.State,
73 &it.Enqueued,
74 &it.Kind,
75 &it.User,
76 ); err != nil {
77 return
78 }
79 imports = append(imports, &it)
80 }
81
82 if err = rows.Err(); err != nil {
83 return
84 }
85
86 jr = JSONResult{
87 Result: struct {
88 Imports []*models.Import `json:"imports"`
89 }{
90 Imports: imports,
91 },
92 }
93
94 return
95 }