Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

77 lines
1.6KB

  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 apiV1LoanSingleGet(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  9. l := loan.Loan{}
  10. loanId := r.URL.Path[len(apiV1Prefix+"loan/"):] //remove prefix
  11. e := l.Read(loanId)
  12. if e != nil {
  13. log.Error("cannot read loan by id ", loanId)
  14. apiV1Client404Error(w, r, ss)
  15. return
  16. }
  17. apiV1SendJson(l, w, r, ss)
  18. }
  19. func decodeJsonLoanEdit(r *http.Request) (ret loan.Loan, e error) {
  20. decoder := json.NewDecoder(r.Body)
  21. //decoder.DisallowUnknownFields()
  22. e = decoder.Decode(&ret)
  23. if e != nil {
  24. log.Error("failed decoding json for Filtering full_loan_summary ", e.Error())
  25. return
  26. }
  27. return
  28. }
  29. func apiV1LoanSinglePostBasic(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  30. l := loan.Loan{}
  31. input, e := decodeJsonLoanEdit(r)
  32. log.Println(input)
  33. if e != nil {
  34. apiV1Client404Error(w, r, ss)
  35. return
  36. } else {
  37. l.Id = input.Id
  38. l.Status = input.Status
  39. l.Amount = input.Amount
  40. l.Item = input.Item
  41. l.Rating = input.Rating
  42. l.Settlement = input.Settlement
  43. l.Description = input.Description
  44. e = l.WriteBasic()
  45. if e != nil {
  46. log.Error("cannot save basic loan", e.Error())
  47. apiV1Client404Error(w, r, ss)
  48. } else {
  49. apiV1SendJson(l, w, r, ss)
  50. }
  51. }
  52. }
  53. func apiV1LoanSingleDelete(w http.ResponseWriter, r *http.Request, ss *loan.Session) {
  54. loanId := r.URL.Path[len(apiV1Prefix+"loan/"):] //remove prefix
  55. e := loan.Delete(loanId)
  56. if e != nil {
  57. log.Error("cannot delete loan by id ", loanId, e.Error())
  58. apiV1Client404Error(w, r, ss)
  59. return
  60. }
  61. apiV1SendJson(loanId, w, r, ss)
  62. }