You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.6 KiB

  1. package api
  2. import (
  3. "git.aiterp.net/lucifer/new-server/app/config"
  4. "git.aiterp.net/lucifer/new-server/models"
  5. "github.com/gin-gonic/gin"
  6. )
  7. func EventHandlers(r gin.IRoutes) {
  8. r.GET("", handler(func(c *gin.Context) (interface{}, error) {
  9. return config.EventHandlerRepository().FetchAll(ctxOf(c))
  10. }))
  11. r.GET("/:id", handler(func(c *gin.Context) (interface{}, error) {
  12. return config.EventHandlerRepository().FindByID(ctxOf(c), intParam(c, "id"))
  13. }))
  14. r.POST("", handler(func(c *gin.Context) (interface{}, error) {
  15. var body models.EventHandler
  16. err := parseBody(c, &body)
  17. if err != nil {
  18. return nil, err
  19. }
  20. if body.Conditions == nil {
  21. body.Conditions = make(map[string]models.EventCondition)
  22. }
  23. err = config.EventHandlerRepository().Save(ctxOf(c), &body)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return body, nil
  28. }))
  29. r.PUT("/:id", handler(func(c *gin.Context) (interface{}, error) {
  30. var body models.EventHandlerUpdate
  31. err := parseBody(c, &body)
  32. if err != nil {
  33. return nil, err
  34. }
  35. handler, err := config.EventHandlerRepository().FindByID(ctxOf(c), intParam(c, "id"))
  36. if err != nil {
  37. return nil, err
  38. }
  39. handler.ApplyUpdate(body)
  40. err = config.EventHandlerRepository().Save(ctxOf(c), handler)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return handler, nil
  45. }))
  46. r.DELETE("/:id", handler(func(c *gin.Context) (interface{}, error) {
  47. handler, err := config.EventHandlerRepository().FindByID(ctxOf(c), intParam(c, "id"))
  48. if err != nil {
  49. return nil, err
  50. }
  51. err = config.EventHandlerRepository().Delete(ctxOf(c), handler)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return handler, nil
  56. }))
  57. }