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.

112 lines
2.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package api
  2. import (
  3. "fmt"
  4. "git.aiterp.net/lucifer/new-server/app/config"
  5. "git.aiterp.net/lucifer/new-server/models"
  6. "github.com/gin-gonic/gin"
  7. "log"
  8. )
  9. func Bridges(r gin.IRoutes) {
  10. r.GET("", handler(func(c *gin.Context) (interface{}, error) {
  11. return config.BridgeRepository().FetchAll(ctxOf(c))
  12. }))
  13. r.GET("/:id", handler(func(c *gin.Context) (interface{}, error) {
  14. return config.BridgeRepository().Find(ctxOf(c), intParam(c, "id"))
  15. }))
  16. r.POST("", handler(func(c *gin.Context) (interface{}, error) {
  17. var body struct {
  18. Driver models.DriverKind `json:"driver"`
  19. Address string `json:"address"`
  20. Token string `json:"token"`
  21. ForceSave bool `json:"forceSave"`
  22. DryRun bool `json:"dryRun"`
  23. }
  24. err := parseBody(c, &body)
  25. if err != nil {
  26. return nil, err
  27. }
  28. driver, err := config.DriverProvider().Provide(body.Driver)
  29. if err != nil {
  30. return nil, err
  31. }
  32. if body.ForceSave {
  33. bridge := models.Bridge{
  34. Name: fmt.Sprintf("Manually saved %s bridge (%s)", body.Driver, body.Address),
  35. Driver: body.Driver,
  36. Address: body.Address,
  37. Token: body.Token,
  38. }
  39. err := config.BridgeRepository().Save(ctxOf(c), &bridge)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return []models.Bridge{bridge}, nil
  44. }
  45. bridges, err := driver.SearchBridge(ctxOf(c), body.Address, body.DryRun)
  46. if err != nil {
  47. return nil, err
  48. }
  49. if !body.DryRun {
  50. for _, bridge := range bridges {
  51. err := config.BridgeRepository().Save(ctxOf(c), &bridge)
  52. if err != nil {
  53. return nil, err
  54. }
  55. log.Printf("Saved new bridge: %s (%s)", bridge.Name, bridge.Address)
  56. }
  57. }
  58. return bridges, nil
  59. }))
  60. r.PUT("/:id", handler(func(c *gin.Context) (interface{}, error) {
  61. var body struct {
  62. Name *string `json:"name"`
  63. }
  64. err := parseBody(c, &body)
  65. if err != nil {
  66. return nil, err
  67. }
  68. bridge, err := config.BridgeRepository().Find(ctxOf(c), intParam(c, "id"))
  69. if err != nil {
  70. return nil, err
  71. }
  72. if body.Name != nil {
  73. bridge.Name = *body.Name
  74. }
  75. err = config.BridgeRepository().Save(ctxOf(c), &bridge)
  76. if err != nil {
  77. return nil, err
  78. }
  79. return bridge, nil
  80. }))
  81. r.DELETE("/:id", handler(func(c *gin.Context) (interface{}, error) {
  82. bridge, err := config.BridgeRepository().Find(ctxOf(c), intParam(c, "id"))
  83. if err != nil {
  84. return nil, err
  85. }
  86. err = config.BridgeRepository().Delete(ctxOf(c), &bridge)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return bridge, nil
  91. }))
  92. }