server/admin/api/events.go

155 lines
3.5 KiB
Go
Raw Normal View History

package api
import (
2016-12-26 02:44:04 +00:00
"encoding/json"
"io/ioutil"
2022-05-16 09:38:46 +00:00
"log"
"net/http"
"path"
2022-05-16 09:38:46 +00:00
"strconv"
2016-12-26 02:44:04 +00:00
"srs.epita.fr/fic-server/libfic"
2022-05-16 09:38:46 +00:00
"github.com/gin-gonic/gin"
)
2022-05-16 09:38:46 +00:00
func declareEventsRoutes(router *gin.RouterGroup) {
router.GET("/events", getEvents)
router.GET("/events.json", getLastEvents)
router.POST("/events", newEvent)
router.DELETE("/events", clearEvents)
apiEventsRoutes := router.Group("/events/:evid")
apiEventsRoutes.Use(EventHandler)
apiEventsRoutes.GET("", showEvent)
apiEventsRoutes.PUT("", updateEvent)
apiEventsRoutes.DELETE("", deleteEvent)
}
func EventHandler(c *gin.Context) {
evid, err := strconv.ParseInt(string(c.Params.ByName("evid")), 10, 64)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Invalid event identifier"})
return
}
event, err := fic.GetEvent(evid)
if err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Event not found"})
return
}
c.Set("event", event)
2016-12-26 02:44:04 +00:00
2022-05-16 09:38:46 +00:00
c.Next()
}
func genEventsFile() error {
if evts, err := fic.GetLastEvents(); err != nil {
return err
} else if j, err := json.Marshal(evts); err != nil {
return err
} else if err := ioutil.WriteFile(path.Join(TeamsDir, "events.json"), j, 0666); err != nil {
return err
}
return nil
}
2022-05-16 09:38:46 +00:00
func getEvents(c *gin.Context) {
evts, err := fic.GetEvents()
if err != nil {
log.Println("Unable to GetEvents:", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to retrieve events list"})
return
}
2022-05-16 09:38:46 +00:00
c.JSON(http.StatusOK, evts)
}
2016-12-26 02:44:04 +00:00
2022-05-16 09:38:46 +00:00
func getLastEvents(c *gin.Context) {
evts, err := fic.GetLastEvents()
if err != nil {
log.Println("Unable to GetLastEvents:", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to retrieve last events list"})
return
2016-12-26 02:44:04 +00:00
}
2022-05-16 09:38:46 +00:00
c.JSON(http.StatusOK, evts)
2016-12-26 02:44:04 +00:00
}
2022-05-16 09:38:46 +00:00
func newEvent(c *gin.Context) {
2016-12-26 02:44:04 +00:00
var ue fic.Event
2022-05-16 09:38:46 +00:00
err := c.ShouldBindJSON(&ue)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
event, err := fic.NewEvent(ue.Text, ue.Kind)
if err != nil {
log.Printf("Unable to newEvent: %s", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during event creation."})
return
2016-12-26 02:44:04 +00:00
}
2022-05-16 09:38:46 +00:00
genEventsFile()
c.JSON(http.StatusOK, event)
}
func clearEvents(c *gin.Context) {
nb, err := fic.ClearEvents()
if err != nil {
log.Printf("Unable to clearEvent: %s", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during event clearing."})
return
2016-12-26 02:44:04 +00:00
}
2022-05-16 09:38:46 +00:00
c.JSON(http.StatusOK, nb)
2016-12-26 02:44:04 +00:00
}
2022-05-16 09:38:46 +00:00
func showEvent(c *gin.Context) {
event := c.MustGet("event").(*fic.Event)
c.JSON(http.StatusOK, event)
2016-12-26 02:44:04 +00:00
}
2022-05-16 09:38:46 +00:00
func updateEvent(c *gin.Context) {
event := c.MustGet("event").(*fic.Event)
2016-12-26 02:44:04 +00:00
var ue fic.Event
2022-05-16 09:38:46 +00:00
err := c.ShouldBindJSON(&ue)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
2016-12-26 02:44:04 +00:00
}
ue.Id = event.Id
if _, err := ue.Update(); err != nil {
2022-05-16 09:38:46 +00:00
log.Printf("Unable to updateEvent: %s", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during event update."})
return
2016-12-26 02:44:04 +00:00
}
2022-05-16 09:38:46 +00:00
genEventsFile()
c.JSON(http.StatusOK, ue)
2016-12-26 02:44:04 +00:00
}
2022-05-16 09:38:46 +00:00
func deleteEvent(c *gin.Context) {
event := c.MustGet("event").(*fic.Event)
_, err := event.Delete()
if err != nil {
log.Printf("Unable to deleteEvent: %s", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during event deletion."})
return
2018-12-06 02:46:36 +00:00
}
2022-05-16 09:38:46 +00:00
genEventsFile()
c.JSON(http.StatusOK, true)
2016-12-26 02:44:04 +00:00
}