Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

166 lines
3.5KB

  1. package main
  2. import (
  3. "biukop.com/sfm/loan"
  4. "database/sql"
  5. "encoding/json"
  6. "github.com/brianvoe/gofakeit/v6"
  7. log "github.com/sirupsen/logrus"
  8. "net/http"
  9. )
  10. func apiV1PeopleList(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  11. filter := ""
  12. keys, ok := r.URL.Query()["filter"]
  13. if ok && len(keys) >= 1 {
  14. filter = keys[0]
  15. }
  16. data := loan.GetPeopleList(filter)
  17. apiV1SendJson(data, w, r, ss)
  18. }
  19. func apiV1PeopleGet(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  20. id := r.URL.Path[len(apiV1Prefix+"people/"):]
  21. p := loan.People{}
  22. e := p.Read(id)
  23. if e != nil {
  24. log.Error("cannot find people by id", id)
  25. apiV1Client404Error(w, r, ss)
  26. return
  27. }
  28. apiV1SendJson(p, w, r, ss)
  29. }
  30. func decodeJsonPeopleEdit(r *http.Request) (ret loan.People, e error) {
  31. decoder := json.NewDecoder(r.Body)
  32. //decoder.DisallowUnknownFields()
  33. e = decoder.Decode(&ret)
  34. if e != nil {
  35. log.Error("failed decoding json for Filtering full_loan_summary ", e.Error())
  36. return
  37. }
  38. return
  39. }
  40. func apiV1PeoplePost(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  41. id := r.URL.Path[len(apiV1Prefix+"people/"):]
  42. p := loan.People{}
  43. e := p.Read(id)
  44. if e != nil {
  45. log.Error("cannot find people by id", id, e.Error())
  46. apiV1Client404Error(w, r, ss)
  47. return
  48. }
  49. input, e := decodeJsonPeopleEdit(r)
  50. if e != nil {
  51. log.Error("invalid input for update people", id, e.Error())
  52. apiV1Client404Error(w, r, ss)
  53. return
  54. }
  55. p.First = input.First
  56. p.Last = input.Last
  57. p.Display = input.Display
  58. if ss.GetRole() == "admin" {
  59. p.Title = input.Title
  60. p.Nick = input.Nick
  61. p.Title = input.Title
  62. }
  63. e = p.Write()
  64. if e != nil {
  65. log.Error("fail to update people", p, e.Error())
  66. apiV1Server500Error(w, r)
  67. return
  68. }
  69. apiV1SendJson(p, w, r, ss)
  70. }
  71. func apiV1PeoplePut(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  72. p := loan.People{}
  73. p.Id = gofakeit.UUID()
  74. e := p.Write()
  75. if e != nil {
  76. log.Error("cannot create people by ", e.Error())
  77. apiV1Server500Error(w, r)
  78. return
  79. }
  80. p.WriteAvatar(randomDummyImage())
  81. apiV1SendJson(p, w, r, ss)
  82. }
  83. func apiV1PeopleDelete(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  84. id := r.URL.Path[len(apiV1Prefix+"people/"):]
  85. e := loan.DeletePeople(id)
  86. if e != nil {
  87. log.Error("cannot delete people by ", id, e.Error())
  88. apiV1Server500Error(w, r)
  89. return
  90. }
  91. apiV1SendJson(id, w, r, ss)
  92. }
  93. func apiV1PeopleExtraGet(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  94. id := r.URL.Path[len(apiV1Prefix+"people-extra/"):]
  95. ret := UserExtra{}
  96. ret.Role = loan.GetRoleById(id)
  97. switch ret.Role {
  98. case "people":
  99. apiV1SendJson(ret, w, r, ss)
  100. return
  101. case "user":
  102. u := loan.User{}
  103. e := u.Read(id)
  104. ret.Login = u.Login
  105. ret.Enabled = u.Enabled
  106. if e != nil {
  107. log.Error("cannot find user by id", id)
  108. apiV1Client404Error(w, r, ss)
  109. return
  110. }
  111. apiV1SendJson(ret, w, r, ss)
  112. return
  113. case "broker":
  114. b := loan.Broker{}
  115. e := b.Read(id)
  116. if e != nil { // this is broker
  117. if e != sql.ErrNoRows {
  118. log.Error("cannot find user by id", id)
  119. }
  120. apiV1Client404Error(w, r, ss)
  121. ret.Role = "user"
  122. return
  123. }
  124. ret.Login = b.Login
  125. ret.Enabled = b.Enabled
  126. ret.BSB = b.BSB
  127. ret.ACC = b.ACC
  128. ret.Organization = b.Organization
  129. ret.License = b.License
  130. apiV1SendJson(ret, w, r, ss)
  131. return
  132. case "admin":
  133. u := loan.User{}
  134. e := u.Read(id)
  135. ret.Login = u.Login
  136. ret.Enabled = u.Enabled
  137. if e != nil {
  138. log.Error("cannot find admin by id", id)
  139. apiV1Client404Error(w, r, ss)
  140. return
  141. }
  142. apiV1SendJson(ret, w, r, ss)
  143. return
  144. default:
  145. apiV1SendJson(ret, w, r, ss)
  146. return
  147. }
  148. }