/
schemas.go
51 lines (41 loc) · 1.14 KB
/
schemas.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
package controllers
import (
"fmt"
"net/http"
"github.com/nuveo/prest/adapters/postgres"
"github.com/nuveo/prest/statements"
)
// GetSchemas list all (or filter) schemas
func GetSchemas(w http.ResponseWriter, r *http.Request) {
requestWhere, values, err := postgres.WhereByRequest(r, 1)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
sqlSchemas, hasCount := postgres.SchemaClause(r)
if requestWhere != "" {
sqlSchemas = fmt.Sprint(sqlSchemas, " WHERE ", requestWhere)
}
order, err := postgres.OrderByRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if order != "" {
sqlSchemas = fmt.Sprint(sqlSchemas, order)
} else if !hasCount {
sqlSchemas = fmt.Sprint(sqlSchemas, fmt.Sprintf(statements.SchemasOrderBy, statements.FieldSchemaName))
}
page, err := postgres.PaginateIfPossible(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
sqlSchemas = fmt.Sprint(sqlSchemas, " ", page)
object, err := postgres.Query(sqlSchemas, values...)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Write(object)
}