diff --git a/cmd/internal/db/queries.go b/cmd/internal/db/queries.go index bd032dc..9bbbedf 100644 --- a/cmd/internal/db/queries.go +++ b/cmd/internal/db/queries.go @@ -17,3 +17,23 @@ func ViewColor(id int) (*models.LampState, error) { return &state, nil } + +func SetColor(id int, color string) error { + currentColor, err := ViewColor(id) + if err != nil { + return err + } + if currentColor.Color == color { + logger.DBLogger.Printf("Lamp %d already has color %s - skipping update", id, color) + return nil + } + + _, err = DB.Exec("UPDATE lamp_state SET color = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?", color, id) + if err != nil { + logger.DBLogger.Printf("Failed to update lamp %d: %v", id, err) + return err + } + + logger.DBLogger.Printf("Lamp %d color updated to %s", id, color) + return nil +} diff --git a/cmd/internal/http/server.go b/cmd/internal/http/server.go index 108268b..a445b72 100644 --- a/cmd/internal/http/server.go +++ b/cmd/internal/http/server.go @@ -4,11 +4,11 @@ import ( "encoding/json" "fmt" "net/http" - "strconv" "github.com/siestaw/laterna/server/cmd/internal/config" "github.com/siestaw/laterna/server/cmd/internal/db" "github.com/siestaw/laterna/server/cmd/internal/logger" + "github.com/siestaw/laterna/server/cmd/internal/models" "github.com/siestaw/laterna/server/cmd/utils" ) @@ -16,7 +16,7 @@ func StartHTTPServer() { router := http.NewServeMux() router.HandleFunc("GET /api/v1/id/{ID}", getCurrent) - router.HandleFunc("POST /api/v1/id/{ID}", setCurrent) + router.HandleFunc("PUT /api/v1/id/{ID}", setCurrent) port := config.AppConfig.HTTP.Port logger.HTTPLogger.Printf("HTTP Server running on :%v", port) @@ -25,10 +25,9 @@ func StartHTTPServer() { func getCurrent(w http.ResponseWriter, r *http.Request) { idStr := r.PathValue("ID") - id, err := strconv.Atoi(idStr) + id, err := utils.IDtoInt(idStr) if err != nil { - utils.HTTPErrorHandling(w, r, http.StatusBadRequest, "Invalid ID") - return + utils.HTTPErrorHandling(w, r, http.StatusBadRequest, err.Error()) } state, err := db.ViewColor(id) @@ -42,5 +41,24 @@ func getCurrent(w http.ResponseWriter, r *http.Request) { } func setCurrent(w http.ResponseWriter, r *http.Request) { + idStr := r.PathValue("ID") + id, err := utils.IDtoInt(idStr) + if err != nil { + utils.HTTPErrorHandling(w, r, http.StatusBadRequest, err.Error()) + return + } + var req models.LampUpdateRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + logger.HTTPLogger.Print(err) + utils.HTTPErrorHandling(w, r, http.StatusBadRequest, "Invalid JSON") + return + } + + err = db.SetColor(id, req.Color) + if err != nil { + logger.HTTPLogger.Printf("Could not update lamp: %s", err) + utils.HTTPErrorHandling(w, r, http.StatusInternalServerError, "Could not update lamp") + } + http.Redirect(w, r, fmt.Sprintf("/api/v1/id/%d", id), http.StatusSeeOther) } diff --git a/cmd/internal/models/models.go b/cmd/internal/models/models.go index e83152f..cc42a55 100644 --- a/cmd/internal/models/models.go +++ b/cmd/internal/models/models.go @@ -26,3 +26,7 @@ type LampState struct { Color string `json:"color"` UpdatedAt time.Time `json:"updated_at"` } + +type LampUpdateRequest struct { + Color string `json:"color"` +} diff --git a/cmd/utils/utils.go b/cmd/utils/utils.go index eb1586d..3321457 100644 --- a/cmd/utils/utils.go +++ b/cmd/utils/utils.go @@ -2,12 +2,25 @@ package utils import ( "encoding/json" + "errors" "net/http" + "strconv" "time" "github.com/siestaw/laterna/server/cmd/internal/models" ) +func IDtoInt(id string) (int, error) { + idInt, err := strconv.Atoi(id) + if err != nil { + return 0, errors.New("invalid ID format") + } + if idInt < 0 { + return 0, errors.New("ID must not be negative") + } + return idInt, nil +} + func HTTPErrorHandling(w http.ResponseWriter, r *http.Request, status int, message string) { timestamp := time.Now().Format("2006-01-02_15-04-05") errResp := models.HTTPError{