mirror of
https://github.com/kaaninchen/Laterna.git
synced 2026-09-17 19:12:48 +00:00
feat: toggle controller state on/off
This commit is contained in:
@@ -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})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user