From 8808994098c57a54ca4c47d34bab67b857189e8b Mon Sep 17 00:00:00 2001 From: minie4 Date: Wed, 3 Jul 2024 10:19:09 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Implement=20endpoint=20for=20editin?= =?UTF-8?q?g=20ports?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api.go | 48 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/api.go b/api.go index 0cf0d6b..9ad6d0c 100644 --- a/api.go +++ b/api.go @@ -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"` }