選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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