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.

166 line
3.6KB

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