-
Notifications
You must be signed in to change notification settings - Fork 19
/
featureofinterest.go
executable file
·111 lines (90 loc) · 3.46 KB
/
featureofinterest.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package api
import (
"errors"
entities "github.com/gost/core"
gostErrors "github.com/gost/server/errors"
"github.com/gost/server/sensorthings/odata"
)
// GetFeatureOfInterest returns a FeatureOfInterest by id
func (a *APIv1) GetFeatureOfInterest(id interface{}, qo *odata.QueryOptions, path string) (*entities.FeatureOfInterest, error) {
l, err := a.db.GetFeatureOfInterest(id, qo)
if err != nil {
return nil, err
}
a.SetLinks(l, qo)
return l, nil
}
// GetFeatureOfInterestByObservation retrieves a FeatureOfInterest by given Observation id
func (a *APIv1) GetFeatureOfInterestByObservation(id interface{}, qo *odata.QueryOptions, path string) (*entities.FeatureOfInterest, error) {
l, err := a.db.GetFeatureOfInterestByObservation(id, qo)
if err != nil {
return nil, err
}
a.SetLinks(l, qo)
return l, nil
}
// GetFeatureOfInterests return FeaturesOfInterest based on the given QueryOptions
func (a *APIv1) GetFeatureOfInterests(qo *odata.QueryOptions, path string) (*entities.ArrayResponse, error) {
fois, count, hasNext, err := a.db.GetFeatureOfInterests(qo)
return processFeatureOfInterest(a, fois, qo, path, count, hasNext, err)
}
func processFeatureOfInterest(a *APIv1, fois []*entities.FeatureOfInterest, qo *odata.QueryOptions, path string, count int, hasNext bool, err error) (*entities.ArrayResponse, error) {
for idx, item := range fois {
i := *item
a.SetLinks(&i, qo)
fois[idx] = &i
}
var data interface{} = fois
return a.createArrayResponse(count, hasNext, path, qo, data), nil
}
// PostFeatureOfInterest adds a FeatureOfInterest to the database
func (a *APIv1) PostFeatureOfInterest(foi *entities.FeatureOfInterest) (*entities.FeatureOfInterest, []error) {
_, err := containsMandatoryParams(foi)
if err != nil {
return nil, err
}
if foi.EncodingType != entities.EncodingGeoJSON.Value {
err := errors.New("Encoding not supported. Supported encoding: " + entities.EncodingGeoJSON.Value)
return nil, []error{err}
}
l, err2 := a.db.PostFeatureOfInterest(foi)
if err2 != nil {
return nil, []error{err2}
}
l.SetAllLinks(a.config.GetExternalServerURI())
return l, nil
}
// PutFeatureOfInterest adds a FeatureOfInterest to the database
func (a *APIv1) PutFeatureOfInterest(id interface{}, foi *entities.FeatureOfInterest) (*entities.FeatureOfInterest, []error) {
supported, err2 := entities.CheckEncodingSupported(foi.EncodingType)
if !supported || err2 != nil {
return nil, []error{err2}
}
l, err2 := a.db.PutFeatureOfInterest(id, foi)
if err2 != nil {
return nil, []error{err2}
}
l.SetAllLinks(a.config.GetExternalServerURI())
return l, nil
}
// PatchFeatureOfInterest updates the given FeatureOfInterest in the database
func (a *APIv1) PatchFeatureOfInterest(id interface{}, foi *entities.FeatureOfInterest) (*entities.FeatureOfInterest, error) {
if foi.Observations != nil {
return nil, gostErrors.NewBadRequestError(errors.New("Unable to deep patch FeatureOfInterest"))
}
if len(foi.EncodingType) != 0 {
if foi.EncodingType != entities.EncodingGeoJSON.Value {
err := errors.New("Encoding not supported. Supported encoding: " + entities.EncodingGeoJSON.Value)
return nil, err
}
supported, err := entities.CheckEncodingSupported(foi.EncodingType)
if !supported || err != nil {
return nil, err
}
}
return a.db.PatchFeatureOfInterest(id, foi)
}
// DeleteFeatureOfInterest deletes a given FeatureOfInterest from the database
func (a *APIv1) DeleteFeatureOfInterest(id interface{}) error {
return a.db.DeleteFeatureOfInterest(id)
}