firebase.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package firebase
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. "github.com/go-chi/chi/v5"
  7. "github.com/go-chi/render"
  8. )
  9. type (
  10. BatchGetRequest struct {
  11. Documents []string `json:"documents"`
  12. }
  13. BatchGetEmptyResponse struct {
  14. Missing string `json:"missing"`
  15. ReadTime string `json:"readTime"`
  16. }
  17. FoundInfoResponse struct {
  18. Name string `json:"name"`
  19. Fields interface{} `json:"fields"`
  20. CreateTime string `json:"createTime"`
  21. UpdateTime string `json:"updateTime"`
  22. }
  23. BatchGetExistsResponse struct {
  24. Found FoundInfoResponse `json:"found"`
  25. ReadTime string `json:"readTime"`
  26. }
  27. UpdateRequest struct {
  28. Name string `json:"name"`
  29. Fields interface{} `json:"fields"`
  30. }
  31. WriteRequest struct {
  32. Update UpdateRequest `json:"update"`
  33. }
  34. BatchCommitRequest struct {
  35. Writes []WriteRequest `json:"writes"`
  36. }
  37. WriteResult struct {
  38. UpdateTime string `json:"updateTime"`
  39. }
  40. BatchCommitResponse struct {
  41. WriteResults []WriteResult `json:"writeResults"`
  42. CommitTime string `json:"commitTime"`
  43. }
  44. )
  45. var savedItems = make(map[string]interface{})
  46. func (body *BatchGetRequest) Bind(r *http.Request) (err error) {
  47. return nil
  48. }
  49. func (body *BatchCommitRequest) Bind(r *http.Request) (err error) {
  50. return nil
  51. }
  52. func HandleBatchCommit() http.HandlerFunc {
  53. return func(w http.ResponseWriter, r *http.Request) {
  54. projectId := chi.URLParam(r, "project_id")
  55. databaseId := chi.URLParam(r, "database_id")
  56. _ = projectId
  57. _ = databaseId
  58. data := &BatchCommitRequest{}
  59. // Seems like requests is text/plain but content is json ...
  60. if err := render.DecodeJSON(r.Body, data); err != nil {
  61. fmt.Println(err)
  62. render.Status(r, http.StatusBadRequest)
  63. return
  64. }
  65. savedItems[data.Writes[0].Update.Name] = data.Writes[0].Update.Fields
  66. render.JSON(w, r, BatchCommitResponse{
  67. CommitTime: time.Now().Format(time.RFC3339),
  68. WriteResults: []WriteResult{
  69. WriteResult{UpdateTime: time.Now().Format(time.RFC3339)},
  70. },
  71. })
  72. render.Status(r, http.StatusOK)
  73. return
  74. }
  75. }
  76. func HandleBatchGet() http.HandlerFunc {
  77. return func(w http.ResponseWriter, r *http.Request) {
  78. projectId := chi.URLParam(r, "project_id")
  79. databaseId := chi.URLParam(r, "database_id")
  80. fmt.Printf("Got %v and %v\n", projectId, databaseId)
  81. data := &BatchGetRequest{}
  82. // Seems like requests is text/plain but content is json ...
  83. if err := render.DecodeJSON(r.Body, data); err != nil {
  84. fmt.Println(err)
  85. render.Status(r, http.StatusBadRequest)
  86. return
  87. }
  88. key := data.Documents[0]
  89. fmt.Printf("Got key %v \n", key)
  90. fields, ok := savedItems[key]
  91. if !ok {
  92. fmt.Println("missing key")
  93. render.JSON(w, r, []BatchGetEmptyResponse{BatchGetEmptyResponse{
  94. Missing: key,
  95. ReadTime: time.Now().Format(time.RFC3339),
  96. }})
  97. render.Status(r, http.StatusOK)
  98. return
  99. }
  100. fmt.Println("existing key")
  101. render.JSON(w, r, []BatchGetExistsResponse{BatchGetExistsResponse{
  102. Found: FoundInfoResponse{
  103. Name: key,
  104. Fields: fields,
  105. CreateTime: time.Now().Format(time.RFC3339),
  106. UpdateTime: time.Now().Format(time.RFC3339),
  107. },
  108. ReadTime: time.Now().Format(time.RFC3339),
  109. }})
  110. render.Status(r, http.StatusOK)
  111. return
  112. }
  113. }