Implement endpoint for editing ports

This commit is contained in:
2024-07-03 10:19:09 +02:00
parent f5bdbca8ec
commit 8808994098

48
api.go
View File

@ -26,15 +26,16 @@ func initApi() {
c.JSON(200, portConfig)
})
router.GET("/api/port", handleGetPort)
router.POST("/api/ports", handleCreatePort)
router.DELETE("/api/ports", handleDeletePort)
router.POST("/api/port", handleCreatePort)
router.PUT("/api/port", handleEditPort)
router.DELETE("/api/port", handleDeletePort)
router.GET("/api/state", handleGetState)
router.PUT("/api/state", handleSetState)
router.POST("/api/routes", handleCreateRoute)
router.PUT("/api/routes", handleSetRoute)
router.DELETE("/api/routes", handleDeleteRoute)
router.POST("/api/route", handleCreateRoute)
router.PUT("/api/route", handleSetRoute)
router.DELETE("/api/route", handleDeleteRoute)
router.GET("/ws", handleWs)
@ -217,6 +218,43 @@ func setState(id string, body SetPortRequest) (gin.H, gin.H) {
return gin.H{"success": true, "state": port.State}, nil
}
type EditPortRequest struct {
Name *string `json:"name"`
}
func handleEditPort(c *gin.Context) {
var body EditPortRequest
err := c.BindJSON(&body)
if err != nil {
c.JSON(500, gin.H{"success": false, "error": "Parsing request body failed!"})
return
}
id := c.Query("UUID")
res, errMsg := editPort(id, body)
httpSendRes(c, errMsg, res)
}
func editPort(id string, body EditPortRequest) (gin.H, gin.H) {
if id == "" {
return nil, gin.H{"success": false, "error": "`UUID` parameter missing!"}
}
port, portIndex, _ := findPort(id)
if portIndex < 0 {
return nil, gin.H{"success": false, "error": "Port with provided UUID does not exist!"}
}
if body.Name != nil {
port.Name = *body.Name
}
saveConfig("ports", &portConfig)
onPortChange(port)
return gin.H{"success": true, "port": port}, nil
}
type CreateRouteRequest struct {
To string `json:"to"`
}