reveil/api/alarms.go

306 lines
8.1 KiB
Go
Raw Normal View History

2022-10-02 21:24:53 +00:00
package api
import (
2022-10-05 20:33:31 +00:00
"fmt"
2022-10-02 21:24:53 +00:00
"net/http"
2022-10-05 20:33:31 +00:00
"time"
2022-10-02 21:24:53 +00:00
"github.com/gin-gonic/gin"
"git.nemunai.re/nemunaire/reveil/config"
2022-10-05 20:33:31 +00:00
"git.nemunai.re/nemunaire/reveil/model"
2022-10-02 21:24:53 +00:00
)
2022-10-06 12:17:58 +00:00
func declareAlarmsRoutes(cfg *config.Config, db *reveil.LevelDBStorage, resetTimer func(), router *gin.RouterGroup) {
2022-10-02 21:24:53 +00:00
router.GET("/alarms/next", func(c *gin.Context) {
2022-12-08 16:49:42 +00:00
alarm, _, err := reveil.GetNextAlarm(cfg, db)
2022-10-06 12:02:56 +00:00
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
2022-10-02 21:24:53 +00:00
2022-10-06 12:02:56 +00:00
c.JSON(http.StatusOK, alarm)
2022-10-02 21:24:53 +00:00
})
2022-12-08 15:43:56 +00:00
router.DELETE("/alarms/next", func(c *gin.Context) {
err := reveil.DropNextAlarm(cfg, db)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
2023-01-13 16:01:32 +00:00
resetTimer()
2022-12-08 15:43:56 +00:00
c.JSON(http.StatusOK, true)
})
2022-10-05 20:33:31 +00:00
router.GET("/alarms/single", func(c *gin.Context) {
alarms, err := reveil.GetAlarmsSingle(db)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
c.JSON(http.StatusOK, alarms)
2022-10-02 21:24:53 +00:00
})
2022-10-05 20:33:31 +00:00
router.POST("/alarms/single", func(c *gin.Context) {
var alarm reveil.AlarmSingle
if err := c.ShouldBindJSON(&alarm); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
if time.Now().After(alarm.Time) {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "This date is already passed."})
return
}
2022-10-02 21:24:53 +00:00
2022-10-05 20:33:31 +00:00
alarm.Id = nil
if err := reveil.PutAlarmSingle(db, &alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
2022-10-02 21:24:53 +00:00
2022-10-06 12:17:58 +00:00
resetTimer()
2022-10-05 20:33:31 +00:00
c.JSON(http.StatusOK, alarm)
2022-10-02 21:24:53 +00:00
})
2022-10-05 20:33:31 +00:00
router.GET("/alarms/repeated", func(c *gin.Context) {
alarms, err := reveil.GetAlarmsRepeated(db)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
c.JSON(http.StatusOK, alarms)
2022-10-02 21:24:53 +00:00
})
2022-10-05 20:33:31 +00:00
router.POST("/alarms/repeated", func(c *gin.Context) {
var alarm reveil.AlarmRepeated
if err := c.ShouldBindJSON(&alarm); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
2022-10-02 21:24:53 +00:00
2022-10-05 20:33:31 +00:00
alarm.Id = nil
if err := reveil.PutAlarmRepeated(db, &alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
2022-10-06 12:17:58 +00:00
resetTimer()
2022-10-05 20:33:31 +00:00
c.JSON(http.StatusOK, alarm)
2022-10-02 21:24:53 +00:00
})
2022-10-05 20:33:31 +00:00
router.GET("/alarms/exceptions", func(c *gin.Context) {
exceptions, err := reveil.GetAlarmExceptions(db)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
2022-10-02 21:24:53 +00:00
2022-10-05 20:33:31 +00:00
c.JSON(http.StatusOK, exceptions)
2022-10-02 21:24:53 +00:00
})
2022-10-05 20:33:31 +00:00
router.POST("/alarms/exceptions", func(c *gin.Context) {
var alarm reveil.AlarmException
if err := c.ShouldBindJSON(&alarm); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
if alarm.Start == nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Start not defined"})
return
}
if alarm.End == nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "End not defined"})
return
}
if time.Now().After(time.Time(*alarm.End)) {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "End date is already passed."})
return
}
2022-10-06 12:02:56 +00:00
if time.Time(*alarm.Start).After(time.Time(*alarm.End)) {
2022-10-05 20:33:31 +00:00
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Start is defined after End. Please verify your inputs."})
return
}
alarm.Id = nil
if err := reveil.PutAlarmException(db, &alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
2022-10-06 12:17:58 +00:00
resetTimer()
2022-10-05 20:33:31 +00:00
c.JSON(http.StatusOK, alarm)
})
singleAlarmsRoutes := router.Group("/alarms/single/:aid")
singleAlarmsRoutes.Use(func(c *gin.Context) {
id, err := reveil.NewIdentifierFromString(c.Param("aid"))
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Invalid alarm idenfifier: %s", err.Error())})
return
}
alarm, err := reveil.GetAlarmSingle(db, id)
if err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": err.Error()})
return
}
c.Set("alarm", alarm)
c.Next()
2022-10-02 21:24:53 +00:00
})
2022-10-05 20:33:31 +00:00
singleAlarmsRoutes.GET("", func(c *gin.Context) {
2022-10-02 21:24:53 +00:00
c.JSON(http.StatusOK, c.MustGet("alarm"))
})
2022-10-05 20:33:31 +00:00
singleAlarmsRoutes.PUT("", func(c *gin.Context) {
oldalarm := c.MustGet("alarm").(*reveil.AlarmSingle)
2022-10-02 21:24:53 +00:00
2022-10-05 20:33:31 +00:00
var alarm reveil.AlarmSingle
if err := c.ShouldBindJSON(&alarm); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
2022-10-02 21:24:53 +00:00
2022-10-05 20:33:31 +00:00
alarm.Id = oldalarm.Id
if err := reveil.PutAlarmSingle(db, &alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
2022-10-06 12:17:58 +00:00
resetTimer()
2022-10-05 20:33:31 +00:00
c.JSON(http.StatusOK, alarm)
2022-10-02 21:24:53 +00:00
})
2022-10-05 20:33:31 +00:00
singleAlarmsRoutes.DELETE("", func(c *gin.Context) {
alarm := c.MustGet("alarm").(*reveil.AlarmSingle)
if err := reveil.DeleteAlarmSingle(db, alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
2022-10-06 12:17:58 +00:00
resetTimer()
2022-10-05 20:33:31 +00:00
c.JSON(http.StatusOK, nil)
2022-10-02 21:24:53 +00:00
})
2022-10-05 20:33:31 +00:00
repeatedAlarmsRoutes := router.Group("/alarms/repeated/:aid")
repeatedAlarmsRoutes.Use(func(c *gin.Context) {
id, err := reveil.NewIdentifierFromString(c.Param("aid"))
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Invalid alarm idenfifier: %s", err.Error())})
return
}
alarm, err := reveil.GetAlarmRepeated(db, id)
if err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": err.Error()})
return
}
c.Set("alarm", alarm)
c.Next()
})
repeatedAlarmsRoutes.GET("", func(c *gin.Context) {
2022-10-06 12:02:56 +00:00
alarm := c.MustGet("alarm").(*reveil.AlarmRepeated)
alarm.FillExcepts(cfg, db)
alarm.NextTime = alarm.GetNextOccurence(cfg, db)
2022-10-06 12:02:56 +00:00
c.JSON(http.StatusOK, alarm)
2022-10-02 21:24:53 +00:00
})
2022-10-05 20:33:31 +00:00
repeatedAlarmsRoutes.PUT("", func(c *gin.Context) {
oldalarm := c.MustGet("alarm").(*reveil.AlarmRepeated)
2022-10-02 21:24:53 +00:00
2022-10-05 20:33:31 +00:00
var alarm reveil.AlarmRepeated
if err := c.ShouldBindJSON(&alarm); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
2022-10-02 21:24:53 +00:00
2022-10-05 20:33:31 +00:00
alarm.Id = oldalarm.Id
if err := reveil.PutAlarmRepeated(db, &alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
2022-10-06 12:17:58 +00:00
resetTimer()
2022-10-05 20:33:31 +00:00
c.JSON(http.StatusOK, alarm)
2022-10-02 21:24:53 +00:00
})
2022-10-05 20:33:31 +00:00
repeatedAlarmsRoutes.DELETE("", func(c *gin.Context) {
alarm := c.MustGet("alarm").(*reveil.AlarmRepeated)
if err := reveil.DeleteAlarmRepeated(db, alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
2022-10-06 12:17:58 +00:00
resetTimer()
2022-10-05 20:33:31 +00:00
c.JSON(http.StatusOK, nil)
2022-10-02 21:24:53 +00:00
})
2022-10-05 20:33:31 +00:00
exceptionAlarmsRoutes := router.Group("/alarms/exceptions/:aid")
exceptionAlarmsRoutes.Use(func(c *gin.Context) {
id, err := reveil.NewIdentifierFromString(c.Param("aid"))
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Invalid alarm idenfifier: %s", err.Error())})
return
}
alarm, err := reveil.GetAlarmException(db, id)
if err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": err.Error()})
return
}
c.Set("alarm", alarm)
c.Next()
2022-10-02 21:24:53 +00:00
})
2022-10-05 20:33:31 +00:00
exceptionAlarmsRoutes.GET("", func(c *gin.Context) {
c.JSON(http.StatusOK, c.MustGet("alarm"))
})
exceptionAlarmsRoutes.PUT("", func(c *gin.Context) {
oldalarm := c.MustGet("alarm").(*reveil.AlarmException)
2022-10-02 21:24:53 +00:00
2022-10-05 20:33:31 +00:00
var alarm reveil.AlarmException
if err := c.ShouldBindJSON(&alarm); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
2022-10-02 21:24:53 +00:00
2022-10-05 20:33:31 +00:00
alarm.Id = oldalarm.Id
if err := reveil.PutAlarmException(db, &alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
2022-10-02 21:24:53 +00:00
2022-10-06 12:17:58 +00:00
resetTimer()
2022-10-05 20:33:31 +00:00
c.JSON(http.StatusOK, alarm)
})
exceptionAlarmsRoutes.DELETE("", func(c *gin.Context) {
alarm := c.MustGet("alarm").(*reveil.AlarmException)
2022-10-02 21:24:53 +00:00
2022-10-05 20:33:31 +00:00
if err := reveil.DeleteAlarmException(db, alarm); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
2022-10-02 21:24:53 +00:00
2022-10-06 12:17:58 +00:00
resetTimer()
2022-10-05 20:33:31 +00:00
c.JSON(http.StatusOK, nil)
})
2022-10-02 21:24:53 +00:00
}