You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 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.Middle = input.Middle
  58. p.Display = input.Display
  59. p.Enabled = input.Enabled
  60. if ss.GetRole() == "admin" {
  61. p.Title = input.Title
  62. p.Nick = input.Nick
  63. p.Title = input.Title
  64. }
  65. e = p.Write()
  66. if e != nil {
  67. log.Error("fail to update people", p, e.Error())
  68. apiV1Server500Error(w, r)
  69. return
  70. }
  71. apiV1SendJson(p, w, r, ss)
  72. }
  73. func apiV1PeoplePut(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  74. p := loan.People{}
  75. p.Id = gofakeit.UUID()
  76. e := p.Write()
  77. if e != nil {
  78. log.Error("cannot create people by ", e.Error())
  79. apiV1Server500Error(w, r)
  80. return
  81. }
  82. p.WriteAvatar(randomDummyImage())
  83. apiV1SendJson(p, w, r, ss)
  84. }
  85. func apiV1PeopleDelete(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  86. id := r.URL.Path[len(apiV1Prefix+"people/"):]
  87. e := loan.DeletePeople(id)
  88. if e != nil {
  89. log.Error("cannot delete people by ", id, e.Error())
  90. apiV1Server500Error(w, r)
  91. return
  92. }
  93. apiV1SendJson(id, w, r, ss)
  94. }
  95. func apiV1PeopleExtraGet(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  96. id := r.URL.Path[len(apiV1Prefix+"people-extra/"):]
  97. ret := UserExtra{}
  98. ret.Role = loan.GetRoleById(id)
  99. //TODO; check manager and account role
  100. switch ret.Role {
  101. case "people":
  102. apiV1SendJson(ret, w, r, ss)
  103. return
  104. case "user":
  105. u := loan.User{}
  106. e := u.Read(id)
  107. ret.Login = u.Login
  108. if e != nil {
  109. log.Error("cannot find user by id", id)
  110. apiV1Client404Error(w, r, ss)
  111. return
  112. }
  113. apiV1SendJson(ret, w, r, ss)
  114. return
  115. case "broker":
  116. b := loan.Broker{}
  117. e := b.Read(id)
  118. if e != nil { // this is broker
  119. if e != sql.ErrNoRows {
  120. log.Error("cannot find user by id", id)
  121. }
  122. apiV1Client404Error(w, r, ss)
  123. ret.Role = "user"
  124. return
  125. }
  126. ret.Login = b.Login
  127. ret.BSB = b.BSB
  128. ret.ACC = b.ACC
  129. ret.Organization = b.Organization
  130. ret.License = b.License
  131. apiV1SendJson(ret, w, r, ss)
  132. return
  133. case "admin":
  134. u := loan.User{}
  135. e := u.Read(id)
  136. ret.Login = u.Login
  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. }