Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

143 Zeilen
3.0KB

  1. package main
  2. import (
  3. "biukop.com/sfm/loan"
  4. "encoding/json"
  5. log "github.com/sirupsen/logrus"
  6. "net/http"
  7. )
  8. func apiV1BrokerList(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  9. filter := ""
  10. keys, ok := r.URL.Query()["filter"]
  11. if ok && len(keys) >= 1 {
  12. filter = keys[0]
  13. }
  14. data := loan.GetBrokerList(filter)
  15. apiV1SendJson(data, w, r, ss)
  16. }
  17. func apiV1BrokerGet(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  18. id := r.URL.Path[len(apiV1Prefix+"broker/"):]
  19. b := loan.Broker{}
  20. e := b.Read(id)
  21. if e != nil {
  22. log.Error("cannot find people by id", id)
  23. apiV1Client404Error(w, r, ss)
  24. return
  25. }
  26. apiV1SendJson(b, w, r, ss)
  27. }
  28. func decodeJsonBrokerEdit(r *http.Request) (ret loan.Broker, e error) {
  29. decoder := json.NewDecoder(r.Body)
  30. //decoder.DisallowUnknownFields()
  31. e = decoder.Decode(&ret)
  32. if e != nil {
  33. log.Error("failed decoding json for Filtering full_loan_summary ", e.Error())
  34. return
  35. }
  36. return
  37. }
  38. func apiV1BrokerDelete(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  39. id := r.URL.Path[len(apiV1Prefix+"broker/"):]
  40. e := loan.DeleteBroker(id)
  41. if e != nil {
  42. log.Error("cannot delete broker by id", id, e.Error())
  43. apiV1Server500Error(w, r)
  44. return
  45. }
  46. ue := UserExtra{}
  47. ue.Login = ""
  48. ue.License = ""
  49. ue.Organization = ""
  50. ue.BSB = ""
  51. ue.ACC = ""
  52. p := loan.People{}
  53. readPeopleErr := p.Read(id)
  54. u := loan.User{}
  55. readUserErr := u.Read(id)
  56. if readUserErr == nil {
  57. ue.Role = "user"
  58. } else if readPeopleErr == nil {
  59. ue.Role = "people"
  60. } else {
  61. ue.Role = "invalid_role"
  62. }
  63. apiV1SendJson(ue, w, r, ss)
  64. }
  65. func apiV1BrokerPut(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  66. id := r.URL.Path[len(apiV1Prefix+"broker/"):]
  67. u := loan.User{}
  68. e := u.Read(id)
  69. if e != nil { // no such people
  70. log.Error("cannot create broker without user, id=", id, e.Error())
  71. apiV1Client403Error(w, r, ss)
  72. return
  73. }
  74. b := loan.Broker{}
  75. b.User = u
  76. b.Organization = "SFM"
  77. e = b.Write()
  78. if e != nil { // no such people
  79. log.Error("cannot create broker from existing user, id=", id, u, e.Error())
  80. apiV1Server500Error(w, r)
  81. return
  82. }
  83. ue := UserExtra{}
  84. ue.Login = b.Login
  85. ue.Role = "broker"
  86. ue.License = b.License
  87. ue.Organization = b.Organization
  88. ue.BSB = b.BSB
  89. ue.ACC = b.ACC
  90. apiV1SendJson(ue, w, r, ss)
  91. }
  92. func apiV1BrokerPost(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  93. id := r.URL.Path[len(apiV1Prefix+"broker/"):]
  94. b := loan.Broker{}
  95. e := b.Read(id)
  96. if e != nil {
  97. log.Error("cannot find broker by id", id, e.Error())
  98. apiV1Client404Error(w, r, ss)
  99. return
  100. }
  101. input, e := decodeJsonBrokerEdit(r)
  102. if e != nil {
  103. log.Error("cannot find broker by id", id, e.Error())
  104. apiV1Client404Error(w, r, ss)
  105. return
  106. }
  107. b.BSB = input.BSB
  108. b.ACC = input.ACC
  109. b.Display = input.Display
  110. b.First = input.First
  111. b.Last = input.Last
  112. b.License = input.License
  113. if ss.GetRole() == "admin" {
  114. b.Login = input.Login
  115. b.Organization = input.Organization
  116. }
  117. e = b.Write()
  118. if e != nil {
  119. log.Error("failed to save broker ", b, e.Error())
  120. apiV1Client404Error(w, r, ss)
  121. return
  122. }
  123. apiV1SendJson(b, w, r, ss)
  124. }