diff --git a/client/src/api/sensors.ts b/client/src/api/sensors.ts index ce478ba..7a9de58 100644 --- a/client/src/api/sensors.ts +++ b/client/src/api/sensors.ts @@ -21,3 +21,6 @@ export const updateSensor = ({ id, ...body }: { id: number; name: string }) => headers: { 'content-type': 'application/json' }, body: JSON.stringify(body), }) + +export const deleteSensor = (id: number) => + request(`/api/sensors/${id}`, { method: 'DELETE' }, 'void') diff --git a/client/src/pages/sensors/components/SensorItem.tsx b/client/src/pages/sensors/components/SensorItem.tsx index 930d66f..ace6c5a 100644 --- a/client/src/pages/sensors/components/SensorItem.tsx +++ b/client/src/pages/sensors/components/SensorItem.tsx @@ -1,6 +1,8 @@ -import { SensorInfo } from '@/api/sensors' +import { deleteSensor, SensorInfo } from '@/api/sensors' import { InputWithCopy } from '@/components/InputWithCopy' +import { useConfirmModal } from '@/contexts/ConfirmModalsContext' import { CancelIcon, EditIcon } from '@/icons' +import { useMutation, useQueryClient } from 'react-query' type Props = { sensor: SensorInfo @@ -8,6 +10,17 @@ type Props = { } export const SensorItem = ({ sensor, onEdit }: Props) => { + const queryClient = useQueryClient() + + const deleteMutation = useMutation(deleteSensor, { + onSuccess: () => queryClient.invalidateQueries(['/sensors']), + }) + + const deleteConfirm = useConfirmModal({ + content: `Are you sure you want to delete sensor ${sensor.name}?`, + onConfirm: () => deleteMutation.mutate(sensor.id), + }) + return (
{sensor.name}
@@ -31,7 +44,10 @@ export const SensorItem = ({ sensor, onEdit }: Props) => { Edit -
diff --git a/server/main.go b/server/main.go index c472bae..3a596f8 100644 --- a/server/main.go +++ b/server/main.go @@ -41,7 +41,8 @@ func main() { loginProtected.GET("/api/sensors", routes.GetSensors(server)) loginProtected.POST("/api/sensors", routes.PostSensors(server)) loginProtected.GET("/api/sensors/:sensor", routes.GetSensor(server)) - loginProtected.PUT("/api/sensors/:sensor", routes.PutSensors(server)) + loginProtected.PUT("/api/sensors/:sensor", routes.PutSensor(server)) + loginProtected.DELETE("/api/sensors/:sensor", routes.DeleteSensor(server)) loginProtected.GET("/api/sensors/:sensor/values/latest", routes.GetSensorLatestValue(server)) loginProtected.GET("/api/sensors/:sensor/values", routes.GetSensorValues(server)) //loginProtected.GET("/api/sensors/:sensor/config", routes.GetSensorConfig(server)) diff --git a/server/routes/sensors.go b/server/routes/sensors.go index 5cfb2a1..902040a 100644 --- a/server/routes/sensors.go +++ b/server/routes/sensors.go @@ -48,7 +48,7 @@ func PostSensors(s *app.Server) gin.HandlerFunc { } } -func PutSensors(s *app.Server) gin.HandlerFunc { +func PutSensor(s *app.Server) gin.HandlerFunc { return func(c *gin.Context) { body := putSensorsBody{} @@ -101,3 +101,23 @@ func GetSensor(s *app.Server) gin.HandlerFunc { c.JSON(http.StatusOK, sensor) } } + +func DeleteSensor(s *app.Server) gin.HandlerFunc { + return func(c *gin.Context) { + sensorId, err := getSensorId(c) + + if err != nil { + c.AbortWithError(http.StatusBadRequest, err) + return + } + + err = s.Services.Sensors.DeleteById(sensorId) + + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + c.Status(http.StatusOK) + } +} diff --git a/server/services/sensors_service.go b/server/services/sensors_service.go index 6373b38..b060150 100644 --- a/server/services/sensors_service.go +++ b/server/services/sensors_service.go @@ -103,6 +103,24 @@ func (s *SensorsService) Update(id int64, name string) (*SensorItem, error) { return item, nil } +func (s *SensorsService) DeleteById(id int64) error { + item, err := s.GetById(id) + + if err != nil { + return err + } + + _, err = s.ctx.DB.Exec("DELETE FROM sensor_values WHERE sensor_id = ?", item.Id) + + if err != nil { + return err + } + + _, err = s.ctx.DB.Exec("DELETE FROM sensors WHERE id = ?", item.Id) + + return err +} + var randomKeySource = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") func generateRandomString(length int) (string, error) {