From a49c3a8472248ce078f489530cf3f0bccaf1a7c2 Mon Sep 17 00:00:00 2001 From: Klagarge Date: Thu, 30 Apr 2026 23:15:02 +0200 Subject: [PATCH] feat(db): add GET history endpoint Signed-off-by: Klagarge --- db/get-db.http | 3 +++ db/src/rest/rest.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/db/get-db.http b/db/get-db.http index f1ef739..e7a7d2a 100644 --- a/db/get-db.http +++ b/db/get-db.http @@ -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 diff --git a/db/src/rest/rest.go b/db/src/rest/rest.go index ea935ed..5cd9b5d 100644 --- a/db/src/rest/rest.go +++ b/db/src/rest/rest.go @@ -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) +}