No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

141 líneas
2.9KB

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