tct-block.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package controllers_table
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "syncscan-go/controllers"
  6. "syncscan-go/locals"
  7. models_table "syncscan-go/models/table"
  8. "github.com/dabory/abango-rest"
  9. "github.com/labstack/echo/v4"
  10. )
  11. type BlockPickReq struct {
  12. Page []models_table.EthBlock
  13. }
  14. type BlockActReq struct {
  15. Page []models_table.EthBlock
  16. }
  17. func BlockPick(c echo.Context) error {
  18. v := c.Get("receiver").(BlockPickReq)
  19. y := c.Get("abango").(abango.Controller)
  20. vRet := &struct {
  21. Page []models_table.EthBlock
  22. }{}
  23. for _, row := range v.Page {
  24. if err := row.GetaRow(&y); err != nil {
  25. return c.String(604, err.Error())
  26. }
  27. vRet.Page = append(vRet.Page, row)
  28. }
  29. ret, _ := json.Marshal(vRet)
  30. return c.JSONBlob(http.StatusOK, ret)
  31. }
  32. func BlockAct(c echo.Context) error {
  33. v := c.Get("receiver").(BlockActReq)
  34. y := c.Get("abango").(abango.Controller)
  35. // var msg string
  36. // var accErr error
  37. vRet := &locals.IdPageBase{}
  38. vIdRet := &locals.IdBase{}
  39. sess := y.Db.NewSession()
  40. defer sess.Close()
  41. if err := sess.Begin(); err == nil {
  42. for _, row := range v.Page {
  43. if row.Id == 0 {
  44. if err := row.AddaRow(&y); err != nil {
  45. sess.Rollback()
  46. return c.String(603, err.Error())
  47. }
  48. } else if row.Id > 0 {
  49. if err := row.EditaRow(&y); err != nil {
  50. sess.Rollback()
  51. return c.String(605, err.Error())
  52. }
  53. } else if row.Id < 0 {
  54. row.Id = -row.Id
  55. if err := row.DelaRow(&y); err != nil {
  56. sess.Rollback()
  57. return c.String(606, err.Error())
  58. }
  59. }
  60. vIdRet.Id = row.Id
  61. vRet.Page = append(vRet.Page, *vIdRet)
  62. }
  63. } else {
  64. return c.String(608, "Transaction Action Failed")
  65. }
  66. if err := sess.Commit(); err != nil {
  67. return c.String(608, "Transaction Commit Failed")
  68. }
  69. ret, _ := json.Marshal(vRet)
  70. return c.JSONBlob(http.StatusOK, ret)
  71. }
  72. func BlockPage(c echo.Context) error {
  73. v := c.Get("receiver").(controllers.CommonPageReq)
  74. y := c.Get("abango").(abango.Controller)
  75. vRet := &struct {
  76. models_table.BlockPageRet
  77. }{}
  78. vRet.PageVars = v.PageVars
  79. if err := vRet.GetPage(&y); err != nil {
  80. return c.String(607, err.Error())
  81. }
  82. ret, _ := json.Marshal(vRet)
  83. return c.JSONBlob(http.StatusOK, ret)
  84. }