/
db_api.go
38 lines (34 loc) · 896 Bytes
/
db_api.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
package api
import (
"context"
"net/http"
"github.com/dankersw/home-automation-backend/models"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
)
func (a *api) GetDbCollectionNames(gc *gin.Context) {
names, err := a.dbi.FetchCollectionNames()
if err != nil {
Reply(gc, http.StatusInternalServerError, nil, err)
} else {
Reply(gc, http.StatusOK, names, err)
}
}
func (a *api) GetSensorData(gc *gin.Context) {
filter := a.dbi.TimestampBetween(7, 0)
cursor, err := a.dbi.GetWithFilter("device_sensor_data", filter)
if err != nil {
fail(gc, "Failed to get sensor data", err)
return
}
data := []models.SensorData{}
for cursor.Next(context.TODO()) {
var item bson.M
if err := cursor.Decode(&item); err != nil {
fail(gc, "Failed to decode data", err)
return
}
data = append(data, models.ToSensorData(item))
}
Reply(gc, http.StatusOK, data, nil)
}