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.

82 lines
1.8 KiB

3 years ago
3 years ago
  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. handler, err := config.EventHandlerRepository().FindByID(ctxOf(c), intParam(c, "id"))
  31. if err != nil {
  32. return nil, err
  33. }
  34. if c.Query("hard") == "true" {
  35. var body models.EventHandler
  36. err := parseBody(c, &body)
  37. if err != nil || body.ID != intParam(c, "id") {
  38. return nil, err
  39. }
  40. handler = &body
  41. } else {
  42. var body models.EventHandlerUpdate
  43. err := parseBody(c, &body)
  44. if err != nil {
  45. return nil, err
  46. }
  47. handler.ApplyUpdate(body)
  48. }
  49. err = config.EventHandlerRepository().Save(ctxOf(c), handler)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return handler, nil
  54. }))
  55. r.DELETE("/:id", handler(func(c *gin.Context) (interface{}, error) {
  56. handler, err := config.EventHandlerRepository().FindByID(ctxOf(c), intParam(c, "id"))
  57. if err != nil {
  58. return nil, err
  59. }
  60. err = config.EventHandlerRepository().Delete(ctxOf(c), handler)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return handler, nil
  65. }))
  66. }