Added support for deleting sensors

This commit is contained in:
Jan Zípek 2022-08-28 21:47:36 +02:00
parent b68efea626
commit fd170185e0
Signed by: kamen
GPG Key ID: A17882625B33AC31
5 changed files with 62 additions and 4 deletions

View File

@ -21,3 +21,6 @@ export const updateSensor = ({ id, ...body }: { id: number; name: string }) =>
headers: { 'content-type': 'application/json' }, headers: { 'content-type': 'application/json' },
body: JSON.stringify(body), body: JSON.stringify(body),
}) })
export const deleteSensor = (id: number) =>
request<SensorInfo>(`/api/sensors/${id}`, { method: 'DELETE' }, 'void')

View File

@ -1,6 +1,8 @@
import { SensorInfo } from '@/api/sensors' import { deleteSensor, SensorInfo } from '@/api/sensors'
import { InputWithCopy } from '@/components/InputWithCopy' import { InputWithCopy } from '@/components/InputWithCopy'
import { useConfirmModal } from '@/contexts/ConfirmModalsContext'
import { CancelIcon, EditIcon } from '@/icons' import { CancelIcon, EditIcon } from '@/icons'
import { useMutation, useQueryClient } from 'react-query'
type Props = { type Props = {
sensor: SensorInfo sensor: SensorInfo
@ -8,6 +10,17 @@ type Props = {
} }
export const SensorItem = ({ sensor, onEdit }: 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 ( return (
<div className="box sensor-item"> <div className="box sensor-item">
<div className="name">{sensor.name}</div> <div className="name">{sensor.name}</div>
@ -31,7 +44,10 @@ export const SensorItem = ({ sensor, onEdit }: Props) => {
<EditIcon /> Edit <EditIcon /> Edit
</button> </button>
<button> <button
onClick={deleteConfirm.show}
disabled={deleteMutation.isLoading}
>
<CancelIcon /> Delete <CancelIcon /> Delete
</button> </button>
</div> </div>

View File

@ -41,7 +41,8 @@ func main() {
loginProtected.GET("/api/sensors", routes.GetSensors(server)) loginProtected.GET("/api/sensors", routes.GetSensors(server))
loginProtected.POST("/api/sensors", routes.PostSensors(server)) loginProtected.POST("/api/sensors", routes.PostSensors(server))
loginProtected.GET("/api/sensors/:sensor", routes.GetSensor(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/latest", routes.GetSensorLatestValue(server))
loginProtected.GET("/api/sensors/:sensor/values", routes.GetSensorValues(server)) loginProtected.GET("/api/sensors/:sensor/values", routes.GetSensorValues(server))
//loginProtected.GET("/api/sensors/:sensor/config", routes.GetSensorConfig(server)) //loginProtected.GET("/api/sensors/:sensor/config", routes.GetSensorConfig(server))

View File

@ -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) { return func(c *gin.Context) {
body := putSensorsBody{} body := putSensorsBody{}
@ -101,3 +101,23 @@ func GetSensor(s *app.Server) gin.HandlerFunc {
c.JSON(http.StatusOK, sensor) 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)
}
}

View File

@ -103,6 +103,24 @@ func (s *SensorsService) Update(id int64, name string) (*SensorItem, error) {
return item, nil 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") var randomKeySource = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
func generateRandomString(length int) (string, error) { func generateRandomString(length int) (string, error) {