mirror of
https://github.com/kaaninchen/Laterna.git
synced 2026-09-17 19:12:48 +00:00
✨ feat(colors): Add a route which lists all available controllers with their metadata
This commit is contained in:
@@ -67,8 +67,9 @@ Authorization: <token>
|
|||||||
#### Colors
|
#### Colors
|
||||||
|
|
||||||
| Method | Route | Description | Request Body |
|
| Method | Route | Description | Request Body |
|
||||||
| ------ | -------------- | ------------------------------------- | ------------------------ |
|
| ------ | -------------- | -------------------------------------------------- | ------------------------ |
|
||||||
| GET | `/colors/{id}` | Get the current color of a controller | — |
|
| 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" }` |
|
| PUT | `/colors/{id}` | Set the color of a controller | `{ "color": "#FF0000" }` |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -70,6 +70,25 @@ func ControllerExists(id int) bool {
|
|||||||
return count > 0
|
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) {
|
func ViewColor(id int) (*models.LampState, error) {
|
||||||
row := DB.QueryRow("SELECT id, color, updated_at FROM controllers WHERE id = ?", id)
|
row := DB.QueryRow("SELECT id, color, updated_at FROM controllers WHERE id = ?", id)
|
||||||
|
|
||||||
|
|||||||
@@ -14,9 +14,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func RegisterColorRoutes(mux *http.ServeMux) {
|
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("GET /api/v1/colors/{ID}", middleware.WithAdminAuth(getCurrent))
|
||||||
mux.HandleFunc("PUT /api/v1/colors/{ID}", middleware.WithAdminAuth(setCurrent))
|
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) {
|
func getCurrent(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
Reference in New Issue
Block a user