feat: toggle controller state on/off

This commit is contained in:
Kaaninchen
2026-08-03 18:50:22 +02:00
parent 67162359ed
commit 3f470096ce
7 changed files with 86 additions and 11 deletions
+1
View File
@@ -24,6 +24,7 @@ func listColors(w http.ResponseWriter, r *http.Request) {
colors, err := db.GetAllColors()
if err != nil {
utils.ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.SuccessResponse(w, http.StatusOK, colors)
}
+32
View File
@@ -13,6 +13,8 @@ import (
func RegisterControllerRoutes(mux *http.ServeMux) {
mux.HandleFunc("POST /api/v1/controllers", middleware.WithAdminAuth(createController))
mux.HandleFunc("DELETE /api/v1/controllers", middleware.WithAdminAuth(deleteController))
mux.HandleFunc("POST /api/v1/controllers/toggle/{ID}", middleware.WithAdminAuth(toggleControllerState))
}
func createController(w http.ResponseWriter, r *http.Request) {
@@ -41,3 +43,33 @@ func deleteController(w http.ResponseWriter, r *http.Request) {
}
utils.SuccessResponse(w, http.StatusOK, models.DeleteData{Deleted: req.ID})
}
func toggleControllerState(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
idStr := r.PathValue("ID")
id, err := utils.IDtoInt((idStr))
if err != nil {
utils.ErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
if !db.ControllerExists((id)) {
utils.ErrorResponse(w, http.StatusNotFound, "Controller not found")
return
}
currentState, err := db.GetControllerState(id)
if err != nil {
utils.ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
err = db.SetControllerstate(!currentState, id)
if err != nil {
utils.ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
utils.SuccessResponse(w, http.StatusOK, models.ToggleController{ID: id, Active: !currentState})
}