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
|
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"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/siestaw/laterna/server/cmd/internal/config"
|
"github.com/siestaw/laterna/server/cmd/internal/config"
|
||||||
"github.com/siestaw/laterna/server/cmd/internal/db"
|
"github.com/siestaw/laterna/server/cmd/internal/db"
|
||||||
"github.com/siestaw/laterna/server/cmd/internal/logger"
|
"github.com/siestaw/laterna/server/cmd/internal/logger"
|
||||||
|
"github.com/siestaw/laterna/server/cmd/internal/models"
|
||||||
"github.com/siestaw/laterna/server/cmd/utils"
|
"github.com/siestaw/laterna/server/cmd/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ func StartHTTPServer() {
|
|||||||
router := http.NewServeMux()
|
router := http.NewServeMux()
|
||||||
|
|
||||||
router.HandleFunc("GET /api/v1/id/{ID}", getCurrent)
|
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
|
port := config.AppConfig.HTTP.Port
|
||||||
logger.HTTPLogger.Printf("HTTP Server running on :%v", port)
|
logger.HTTPLogger.Printf("HTTP Server running on :%v", port)
|
||||||
@@ -25,10 +25,9 @@ func StartHTTPServer() {
|
|||||||
|
|
||||||
func getCurrent(w http.ResponseWriter, r *http.Request) {
|
func getCurrent(w http.ResponseWriter, r *http.Request) {
|
||||||
idStr := r.PathValue("ID")
|
idStr := r.PathValue("ID")
|
||||||
id, err := strconv.Atoi(idStr)
|
id, err := utils.IDtoInt(idStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HTTPErrorHandling(w, r, http.StatusBadRequest, "Invalid ID")
|
utils.HTTPErrorHandling(w, r, http.StatusBadRequest, err.Error())
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
state, err := db.ViewColor(id)
|
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) {
|
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"`
|
Color string `json:"color"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type LampUpdateRequest struct {
|
||||||
|
Color string `json:"color"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,12 +2,25 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/siestaw/laterna/server/cmd/internal/models"
|
"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) {
|
func HTTPErrorHandling(w http.ResponseWriter, r *http.Request, status int, message string) {
|
||||||
timestamp := time.Now().Format("2006-01-02_15-04-05")
|
timestamp := time.Now().Format("2006-01-02_15-04-05")
|
||||||
errResp := models.HTTPError{
|
errResp := models.HTTPError{
|
||||||
|
|||||||
Reference in New Issue
Block a user