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.

68 lines
1.4 KiB

  1. package api
  2. import (
  3. "git.aiterp.net/lucifer/new-server/app/config"
  4. "git.aiterp.net/lucifer/new-server/app/services/publisher"
  5. "git.aiterp.net/lucifer/new-server/models"
  6. "github.com/gin-gonic/gin"
  7. )
  8. func Scenes(r gin.IRoutes) {
  9. r.GET("", handler(func(c *gin.Context) (interface{}, error) {
  10. return config.SceneRepository().FetchAll(ctxOf(c))
  11. }))
  12. r.GET("/:id", handler(func(c *gin.Context) (interface{}, error) {
  13. return config.SceneRepository().Find(ctxOf(c), intParam(c, "id"))
  14. }))
  15. r.POST("", handler(func(c *gin.Context) (interface{}, error) {
  16. var body models.Scene
  17. err := parseBody(c, &body)
  18. if err != nil {
  19. return nil, err
  20. }
  21. err = body.Validate()
  22. if err != nil {
  23. return nil, err
  24. }
  25. err = config.SceneRepository().Save(ctxOf(c), &body)
  26. if err != nil {
  27. return nil, err
  28. }
  29. publisher.Global().UpdateScene(body)
  30. return body, nil
  31. }))
  32. r.PUT("/:id", handler(func(c *gin.Context) (interface{}, error) {
  33. var body models.Scene
  34. err := parseBody(c, &body)
  35. if err != nil {
  36. return nil, err
  37. }
  38. scene, err := config.SceneRepository().Find(ctxOf(c), intParam(c, "id"))
  39. if err != nil {
  40. return nil, err
  41. }
  42. body.ID = scene.ID
  43. err = body.Validate()
  44. if err != nil {
  45. return nil, err
  46. }
  47. err = config.SceneRepository().Save(ctxOf(c), &body)
  48. if err != nil {
  49. return nil, err
  50. }
  51. publisher.Global().UpdateScene(body)
  52. return body, nil
  53. }))
  54. }