From ea4a0d8a27ed37f041419606bd07d447600b5be1 Mon Sep 17 00:00:00 2001
From: Jonas Leder <jonas@jonasled.de>
Date: Mon, 24 Mar 2025 22:30:41 +0100
Subject: [PATCH] define config delete
---
docs/docs.go | 27 +++++++++++++++++++
docs/swagger.json | 27 +++++++++++++++++++
docs/swagger.yaml | 19 +++++++++++++
go.mod | 2 +-
go.sum | 4 +--
webserver/plugins/deleteConfig.go | 45 +++++++++++++++++++++++++++++++
webserver/plugins/init.go | 1 +
7 files changed, 122 insertions(+), 3 deletions(-)
create mode 100644 webserver/plugins/deleteConfig.go
diff --git a/docs/docs.go b/docs/docs.go
index 18243ac..c48188a 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 ffef5e2..8e90145 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 f1b427b..c1f9417 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 2d4a1cc..1d80096 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 d15aa75..00cb647 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 0000000..da3c37e
--- /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 a90f9a2..433fe1e 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)
}
}
--
GitLab