forked from GeoNet/fits
/
type.go
89 lines (75 loc) · 2.27 KB
/
type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"database/sql"
"github.com/GeoNet/web"
"github.com/GeoNet/web/api/apidoc"
"html/template"
"net/http"
)
var typeDoc = apidoc.Endpoint{Title: "Type",
Description: `Look up observation type information.`,
Queries: []*apidoc.Query{
typeD,
},
}
var typeD = &apidoc.Query{
Accept: web.V1JSON,
Title: "Type",
Description: "List all observation types.",
Example: "/type",
ExampleHost: exHost,
URI: "/type",
Props: map[string]template.HTML{
"description": `Type description e.g., <code>displacement from initial position</code>`,
"name": `Type name e.g., <code>east</code>`,
"typeID": typeIDDoc,
"unit": `Type unit e.g., <code>mm</code>.`,
},
}
func typeH(w http.ResponseWriter, r *http.Request) {
if len(r.URL.Query()) != 0 {
web.BadRequest(w, r, "incorrect number of query params.")
return
}
w.Header().Set("Content-Type", web.V1JSON)
var d string
err := db.QueryRow(
`select row_to_json(fc) from (select array_to_json(array_agg(t)) as type
from (select typeid as "typeID", type.name, symbol as unit, description
from fits.type join fits.unit using (unitpk)) as t) as fc`).Scan(&d)
if err != nil {
web.ServiceUnavailable(w, r, err)
return
}
b := []byte(d)
web.Ok(w, r, &b)
}
// validType checks that the typeID exists in the DB.
func validType(w http.ResponseWriter, r *http.Request, typeID string) bool {
var d string
err := db.QueryRow("select typeID FROM fits.type where typeID = $1", typeID).Scan(&d)
if err == sql.ErrNoRows {
web.NotFound(w, r, "invalid typeID: "+typeID)
return false
}
if err != nil {
web.ServiceUnavailable(w, r, err)
return false
}
return true
}
// validTypeMethod checks that the typeID and methodID exists in the DB
// and are a valid combination.
func validTypeMethod(w http.ResponseWriter, r *http.Request, typeID, methodID string) bool {
var d string
err := db.QueryRow("SELECT typepk FROM fits.type join fits.type_method using (typepk) join fits.method using (methodpk) WHERE typeid = $1 and methodid = $2", typeID, methodID).Scan(&d)
if err == sql.ErrNoRows {
web.NotFound(w, r, "invalid methodID for typeID: "+methodID+" "+typeID)
return false
}
if err != nil {
web.ServiceUnavailable(w, r, err)
return false
}
return true
}