diff --git a/docs/docs.go b/docs/docs.go index 18243ac88a32e92cd9f9f95fb662ef788eea9cf3..c48188a64b408c69749312e62801190f1d39b80b 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -270,6 +270,33 @@ const docTemplate = `{ } ], "responses": {} + }, + "delete": { + "description": "Delete a existing config", + "produces": [ + "application/json" + ], + "tags": [ + "plugins" + ], + "summary": "Delete Config", + "parameters": [ + { + "type": "string", + "description": "Plugin name", + "name": "plugin", + "in": "path", + "required": true + }, + { + "type": "integer", + "description": "ID of the config to update", + "name": "configId", + "in": "path", + "required": true + } + ], + "responses": {} } }, "/api/v1/smokedetectors": { diff --git a/docs/swagger.json b/docs/swagger.json index ffef5e28f98ad74967c20c2e6aa2b1a3d9054c72..8e901451001886c751658359dda58bfc6220f716 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -261,6 +261,33 @@ } ], "responses": {} + }, + "delete": { + "description": "Delete a existing config", + "produces": [ + "application/json" + ], + "tags": [ + "plugins" + ], + "summary": "Delete Config", + "parameters": [ + { + "type": "string", + "description": "Plugin name", + "name": "plugin", + "in": "path", + "required": true + }, + { + "type": "integer", + "description": "ID of the config to update", + "name": "configId", + "in": "path", + "required": true + } + ], + "responses": {} } }, "/api/v1/smokedetectors": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index f1b427b9d2b68211b3553a27f5b3bbc4af311cd2..c1f94175fcfe14e1a710f9a5db36c2e161ecf37f 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -173,6 +173,25 @@ paths: tags: - plugins /api/v1/plugins/{plugin}/{configId}: + delete: + description: Delete a existing config + parameters: + - description: Plugin name + in: path + name: plugin + required: true + type: string + - description: ID of the config to update + in: path + name: configId + required: true + type: integer + produces: + - application/json + responses: {} + summary: Delete Config + tags: + - plugins get: description: Get a existing config parameters: diff --git a/go.mod b/go.mod index 2d4a1cc28ebd5d48ca719e9ee6ac245ede7474fa..1d80096a160140a303f151cedfd3a21296af096a 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/toorop/gin-logrus v0.0.0-20210225092905-2c785434f26f github.com/wind-c/comqtt/v2 v2.6.0 gorm.io/gorm v1.25.12 - jonasled.dev/firehouse-smokedetection/plugin-interface v0.0.9 + jonasled.dev/firehouse-smokedetection/plugin-interface v0.0.10 jonasled.dev/jonasled/go-libs v0.0.3 ) diff --git a/go.sum b/go.sum index d15aa75232dcf81f2786c89ea2899aacb3c242f2..00cb64783dcc66c890b31013982a45e5c58fe9a8 100644 --- a/go.sum +++ b/go.sum @@ -212,8 +212,8 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= -jonasled.dev/firehouse-smokedetection/plugin-interface v0.0.9 h1:WbuQRhJ3QzzzkdS9rOVzIjr4Zus/U+S3GXa0Wp/Tecs= -jonasled.dev/firehouse-smokedetection/plugin-interface v0.0.9/go.mod h1:7OmpERugGg+B1OKE5R2glhdbbF2XmYP0OTfbdNNBm40= +jonasled.dev/firehouse-smokedetection/plugin-interface v0.0.10 h1:qBk+WBGmYirLjUlziIY5YClc4L0TOPR0hMBIfF2HSj0= +jonasled.dev/firehouse-smokedetection/plugin-interface v0.0.10/go.mod h1:7OmpERugGg+B1OKE5R2glhdbbF2XmYP0OTfbdNNBm40= jonasled.dev/jonasled/go-libs v0.0.3 h1:2Tg1pYJMufBMPkyYVVAvsUJe/R+TRRtRVW9MCbc24bM= jonasled.dev/jonasled/go-libs v0.0.3/go.mod h1:E8Sev4obR0x6y7x0HaIN1tImeLnVJgwJXt++jzTwiFM= modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE= diff --git a/webserver/plugins/deleteConfig.go b/webserver/plugins/deleteConfig.go new file mode 100644 index 0000000000000000000000000000000000000000..da3c37ee093343075f7ba224114e557e3db987bb --- /dev/null +++ b/webserver/plugins/deleteConfig.go @@ -0,0 +1,45 @@ +package plugins + +import ( + "net/http" + "slices" + "strconv" + + "github.com/gin-gonic/gin" + "jonasled.dev/firehouse-smokedetection/backend/alarm" + "jonasled.dev/firehouse-smokedetection/backend/types" +) + +// @Summary Delete Config +// @Description Delete a existing config +// @Tags plugins +// @Produce json +// @Param plugin path string true "Plugin name" +// @Param configId path int true "ID of the config to update" +// @router /api/v1/plugins/{plugin}/{configId} [delete] +func deleteConfig(ctx *gin.Context) { + pluginNames := alarm.GetAllPlugins() + pluginName := ctx.Param("plugin") + configId := ctx.Param("id") + if !slices.Contains(pluginNames, pluginName) { + ctx.JSON(http.StatusNotFound, types.GeneralResponse{ + Success: false, + Message: "Plugin with given name not found", + }) + return + } + + configIdInt, err := strconv.Atoi(configId) + if err != nil { + ctx.JSON(http.StatusNotFound, types.GeneralResponse{ + Success: false, + Message: "Failed parsing config id as number", + }) + return + } + alarm.Plugins[pluginName].DeleteConfig(uint(configIdInt)) + ctx.JSON(http.StatusOK, types.GeneralResponse{ + Success: true, + Message: "The config has been deleted", + }) +} diff --git a/webserver/plugins/init.go b/webserver/plugins/init.go index a90f9a2e7281cdbaee72c24c6cbb85efc708ffb8..433fe1e884b97ece3d97885c1a557d7bf8aaddbe 100644 --- a/webserver/plugins/init.go +++ b/webserver/plugins/init.go @@ -10,6 +10,7 @@ func Init(r *gin.Engine) { plugins.POST(":plugin", setNewConfig) plugins.PUT(":plugin/:id", updateConfig) plugins.GET(":plugin/:id", getConfig) + plugins.DELETE(":plugin/:id", deleteConfig) } }