From b50a62a52d03d76b89dd09c1ff5d42ab2577598a Mon Sep 17 00:00:00 2001 From: Siesta <124433727+siestaw@users.noreply.github.com> Date: Mon, 28 Jul 2025 21:36:06 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(colors):=20Add=20a=20route=20w?= =?UTF-8?q?hich=20lists=20all=20available=20controllers=20with=20their=20m?= =?UTF-8?q?etadata?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 +++++---- cmd/internal/db/queries.go | 21 ++++++++++++++++++++- cmd/internal/routes/colors.go | 11 ++++++++++- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index dac307d..c8f01fb 100644 --- a/README.md +++ b/README.md @@ -66,10 +66,11 @@ Authorization: #### Colors -| Method | Route | Description | Request Body | -| ------ | -------------- | ------------------------------------- | ------------------------ | -| GET | `/colors/{id}` | Get the current color of a controller | — | -| PUT | `/colors/{id}` | Set the color of a controller | `{ "color": "#FF0000" }` | +| Method | Route | Description | Request Body | +| ------ | -------------- | -------------------------------------------------- | ------------------------ | +| GET | `/colors/` | Get the current color of all available controllers | — | +| GET | `/colors/{id}` | Get the current color of a specific controller | — | +| PUT | `/colors/{id}` | Set the color of a controller | `{ "color": "#FF0000" }` | --- diff --git a/cmd/internal/db/queries.go b/cmd/internal/db/queries.go index 8308fa5..4d347ad 100644 --- a/cmd/internal/db/queries.go +++ b/cmd/internal/db/queries.go @@ -15,7 +15,7 @@ func CreateController() (int64, error) { logger.DBLogger.Printf("Error creating controller: %v", err) return 0, err } - + id, err := result.LastInsertId() if err != nil { logger.DBLogger.Printf("Error fetching controller ID: %v", err) @@ -70,6 +70,25 @@ func ControllerExists(id int) bool { return count > 0 } +func GetAllColors() ([]models.LampState, error) { + rows, err := DB.Query("SELECT id, color, updated_at FROM controllers WHERE id > 0") + if err != nil { + return nil, err + } + defer rows.Close() + + var controllers []models.LampState + + for rows.Next() { + var state models.LampState + if err := rows.Scan(&state.ID, &state.Color, &state.UpdatedAt); err != nil { + return nil, err + } + controllers = append(controllers, state) + } + return controllers, nil +} + func ViewColor(id int) (*models.LampState, error) { row := DB.QueryRow("SELECT id, color, updated_at FROM controllers WHERE id = ?", id) diff --git a/cmd/internal/routes/colors.go b/cmd/internal/routes/colors.go index 100a10a..8a6a98a 100644 --- a/cmd/internal/routes/colors.go +++ b/cmd/internal/routes/colors.go @@ -14,9 +14,18 @@ import ( ) func RegisterColorRoutes(mux *http.ServeMux) { + mux.HandleFunc("GET /api/v1/colors/", middleware.WithAdminAuth(listColors)) mux.HandleFunc("GET /api/v1/colors/{ID}", middleware.WithAdminAuth(getCurrent)) mux.HandleFunc("PUT /api/v1/colors/{ID}", middleware.WithAdminAuth(setCurrent)) - // mux.HandleFunc("WS /api/v1/ws/colors/{ID}", setCurrentWebsocket) +} + +func listColors(w http.ResponseWriter, r *http.Request) { + defer r.Body.Close() + colors, err := db.GetAllColors() + if err != nil { + utils.ErrorResponse(w, http.StatusInternalServerError, err.Error()) + } + utils.SuccessResponse(w, http.StatusOK, colors) } func getCurrent(w http.ResponseWriter, r *http.Request) {