feat(db): add GET history endpoint
Signed-off-by: Klagarge <remi@heredero.ch>
This commit is contained in:
@@ -3,5 +3,8 @@
|
||||
### GET last value of temp, co2, humidity, windows states
|
||||
GET http://localhost:8080/api/v1/rooms/{{room-id}}/current
|
||||
|
||||
### GET history of a room
|
||||
GET http://localhost:8080/api/v1/rooms/{{room-id}}/history
|
||||
|
||||
### GET all rooms
|
||||
GET http://localhost:8080/api/v1/rooms
|
||||
|
||||
@@ -29,6 +29,7 @@ func (g *RestGateway) setupRoutes() {
|
||||
{
|
||||
v1.GET("/rooms", g.getRooms)
|
||||
v1.GET("/rooms/:room-id/current", g.getRoomCurrent)
|
||||
v1.GET("/rooms/:room-id/history", g.getRoomHistory)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,3 +91,31 @@ func (g *RestGateway) getRoomCurrent(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Room not found or no data available"})
|
||||
}
|
||||
|
||||
// GET /api/v1/rooms/{room-id}/history
|
||||
func (g *RestGateway) getRoomHistory(c *gin.Context) {
|
||||
roomID := c.Param("room-id")
|
||||
|
||||
// Default to last 24 hours if not specified
|
||||
query := fmt.Sprintf(` SELECT * FROM "provence" WHERE "room" = '%s' AND time > now() - INTERVAL '1 day' ORDER BY time ASC
|
||||
`, roomID)
|
||||
|
||||
// Using context.Background() as seen in working snippet
|
||||
it, err := g.influxGateway.Query(context.Background(), query)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var history []map[string]any
|
||||
for it.Next() {
|
||||
history = append(history, it.Value())
|
||||
}
|
||||
|
||||
if err := it.Err(); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, history)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user