setCurrent

This commit is contained in:
Mitsu
2025-07-09 20:00:11 +02:00
parent 5281f73148
commit 684cd08c1c
4 changed files with 60 additions and 5 deletions
+20
View File
@@ -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
}
+23 -5
View File
@@ -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)
}
+4
View File
@@ -26,3 +26,7 @@ type LampState struct {
Color string `json:"color"`
UpdatedAt time.Time `json:"updated_at"`
}
type LampUpdateRequest struct {
Color string `json:"color"`
}
+13
View File
@@ -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{