mirror of
https://github.com/kaaninchen/Laterna.git
synced 2026-09-17 19:12:48 +00:00
setCurrent
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -26,3 +26,7 @@ type LampState struct {
|
||||
Color string `json:"color"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type LampUpdateRequest struct {
|
||||
Color string `json:"color"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user