Added support for deleting sensors
This commit is contained in:
parent
b68efea626
commit
fd170185e0
|
|
@ -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<SensorInfo>(`/api/sensors/${id}`, { method: 'DELETE' }, 'void')
|
||||
|
|
|
|||
|
|
@ -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 (
|
||||
<div className="box sensor-item">
|
||||
<div className="name">{sensor.name}</div>
|
||||
|
|
@ -31,7 +44,10 @@ export const SensorItem = ({ sensor, onEdit }: Props) => {
|
|||
<EditIcon /> Edit
|
||||
</button>
|
||||
|
||||
<button>
|
||||
<button
|
||||
onClick={deleteConfirm.show}
|
||||
disabled={deleteMutation.isLoading}
|
||||
>
|
||||
<CancelIcon /> Delete
|
||||
</button>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue