mirror of
https://github.com/kaaninchen/Laterna.git
synced 2026-09-17 11:02:47 +00:00
refactor: change deleteController from payload to URL ID
This commit is contained in:
@@ -75,10 +75,10 @@ The token is shown once on the first startup. To regenerate it, run the server w
|
||||
#### 🎛️ Controllers
|
||||
|
||||
| Method | Route | Description | Payload |
|
||||
|--------|----------------------------|--------------------------------------|------------|
|
||||
|--------|----------------------------|--------------------------------------|---------|
|
||||
| POST | `/controllers` | Create a new controller | |
|
||||
| POST | `/controllers/toggle/{id}` | Toggle an existing controller on/off | |
|
||||
| DELETE | `/controllers` | Delete an existing controller | { "ID": 1} |
|
||||
| DELETE | `/controllers/{id}` | Delete an existing controller | |
|
||||
|
||||
|
||||
</details>
|
||||
@@ -103,10 +103,10 @@ The token is shown once on the first startup. To regenerate it, run the server w
|
||||
$ http POST localhost:8080/api/v1/controllers "Authorization: $TOKEN"
|
||||
```
|
||||
|
||||
#### Delete a controller
|
||||
#### Delete the controller with the ID of 1
|
||||
|
||||
```bash
|
||||
$ http DELETE localhost:8080/api/v1/controllers "Authorization: $TOKEN" ID:=1
|
||||
$ http DELETE localhost:8080/api/v1/controllers/1 "Authorization: $TOKEN"
|
||||
```
|
||||
|
||||
#### Get every controllers colors
|
||||
@@ -115,19 +115,19 @@ $ http DELETE localhost:8080/api/v1/controllers "Authorization: $TOKEN" ID:=1
|
||||
$ http GET localhost:8080/api/v1/colors/ "Authorization: $TOKEN"
|
||||
```
|
||||
|
||||
#### Get a specific controllers's color
|
||||
#### Get color of the controller with the ID of 1
|
||||
|
||||
```bash
|
||||
$ http GET localhost:8080/api/v1/colors/1 "Authorization: $TOKEN"
|
||||
```
|
||||
|
||||
#### Set a controller's color
|
||||
#### Set the color of the controller with the ID of 1
|
||||
|
||||
```bash
|
||||
$ http PUT localhost:8080/api/v1/colors/1 "Authorization: $TOKEN" Color="#C2C342"
|
||||
```
|
||||
|
||||
#### Toggle controller on/off
|
||||
#### Toggle the controller with the ID 1 on/off
|
||||
```bash
|
||||
$ http PUT localhost:8080/api/v1/colors/1 "Authorization: $TOKEN"
|
||||
```
|
||||
@@ -148,9 +148,8 @@ $ curl -X POST localhost:8080/api/v1/controllers \
|
||||
#### Delete a controller
|
||||
|
||||
```bash
|
||||
$ curl -X DELETE localhost:8080/api/v1/controllers \
|
||||
$ curl -X DELETE localhost:8080/api/v1/controllers/1 \
|
||||
-H "Authorization: $TOKEN" \
|
||||
-d '{"ID": 1}'
|
||||
```
|
||||
|
||||
#### Get every controllers colors
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/siestaw/laterna/server/cmd/internal/db"
|
||||
@@ -12,7 +11,7 @@ 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("DELETE /api/v1/controllers/{ID}", middleware.WithAdminAuth(deleteController))
|
||||
|
||||
mux.HandleFunc("POST /api/v1/controllers/toggle/{ID}", middleware.WithAdminAuth(toggleControllerState))
|
||||
}
|
||||
@@ -28,20 +27,24 @@ func createController(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func deleteController(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
var req models.ControllerRequests
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
utils.ErrorResponse(w, http.StatusBadRequest, "Invalid JSON")
|
||||
|
||||
idStr := r.PathValue(("ID"))
|
||||
id, err := utils.IDtoInt(idStr)
|
||||
if err != nil {
|
||||
utils.ErrorResponse(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
if !db.ControllerExists(req.ID) || req.ID <= 0 {
|
||||
utils.ErrorResponse(w, http.StatusBadRequest, "Invalid ID")
|
||||
|
||||
if !db.ControllerExists(id) {
|
||||
utils.ErrorResponse(w, http.StatusNotFound, "Lamp not found")
|
||||
return
|
||||
}
|
||||
if db.DeleteController(req.ID) != nil {
|
||||
|
||||
if db.DeleteController(id) != nil {
|
||||
utils.ErrorResponse(w, http.StatusInternalServerError, "An error occured. Check the server logs for more information")
|
||||
return
|
||||
}
|
||||
utils.SuccessResponse(w, http.StatusOK, models.DeleteData{Deleted: req.ID})
|
||||
utils.SuccessResponse(w, http.StatusOK, models.DeleteData{Deleted: id})
|
||||
}
|
||||
|
||||
func toggleControllerState(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user