From 9cd7c91693765a183177f8d4cd61ed6cc456b213 Mon Sep 17 00:00:00 2001 From: sp Date: Wed, 10 Mar 2021 22:56:42 +1100 Subject: [PATCH] Loan Single edit service backend --- apiV1LoanSingle.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++ apiv1.go | 32 +++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 apiV1LoanSingle.go diff --git a/apiV1LoanSingle.go b/apiV1LoanSingle.go new file mode 100644 index 0000000..d4273d9 --- /dev/null +++ b/apiV1LoanSingle.go @@ -0,0 +1,60 @@ +package main + +import ( + "biukop.com/sfm/loan" + "encoding/json" + log "github.com/sirupsen/logrus" + "net/http" +) + +func apiV1LoanSingleGet(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + l := loan.Loan{} + + loanId := r.URL.Path[len(apiV1Prefix+"loan/"):] //remove prefix + e := l.Read(loanId) + if e != nil { + log.Error("cannot read loan by id ", loanId) + apiV1Client404Error(w, r, ss) + return + } + + apiV1SendJson(l, w, r, ss) + +} + +func decodeJsonLoanEdit(r *http.Request) (ret loan.Loan, e error) { + decoder := json.NewDecoder(r.Body) + //decoder.DisallowUnknownFields() + e = decoder.Decode(&ret) + if e != nil { + log.Error("failed decoding json for Filtering full_loan_summary ", e.Error()) + return + } + return +} +func apiV1LoanSinglePostBasic(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + l := loan.Loan{} + + input, e := decodeJsonLoanEdit(r) + + log.Println(input) + if e != nil { + apiV1Client404Error(w, r, ss) + return + } else { + l.Id = input.Id + l.Status = input.Status + l.Amount = input.Amount + l.Item = input.Item + l.Rating = input.Rating + l.Description = input.Description + e = l.WriteBasic() + if e != nil { + apiV1Client404Error(w, r, ss) + } else { + apiV1SendJson(l, w, r, ss) + } + + } + +} diff --git a/apiv1.go b/apiv1.go index 7679deb..8525cc9 100644 --- a/apiv1.go +++ b/apiv1.go @@ -36,6 +36,8 @@ func setupApiV1Handler() []apiV1HandlerMap { {"GET", "chart/recent-10-loans", apiV1ChartRecent10Loans}, {"GET", "chart/top-broker", apiV1ChartTopBroker}, {"POST", "grid/loan/full-loan-overview", apiV1GridLoanFullOverview}, + {"GET", "loan/", apiV1LoanSingleGet}, + {"POST", "loan/basic/", apiV1LoanSinglePostBasic}, {"GET", "avatar/", apiV1Avatar}, {"GET", "login", apiV1DumpRequest}, } @@ -49,6 +51,8 @@ func setupApiV1Handler() []apiV1HandlerMap { {"GET", "chart/recent-10-loans", apiV1ChartRecent10Loans}, {"GET", "chart/top-broker", apiV1ChartTopBroker}, {"POST", "grid/loan/full-loan-overview", apiV1GridLoanFullOverview}, + {"GET", "loan/", apiV1LoanSingleGet}, + {"POST", "loan/basic/", apiV1LoanSinglePostBasic}, {"GET", "avatar/", apiV1Avatar}, {"GET", "login", apiV1EmptyResponse}, } @@ -181,6 +185,14 @@ func apiV1InitSessionByBrowserId(r *http.Request) (session loan.Session, e error } func apiV1AddTrackingCookie(w http.ResponseWriter, r *http.Request, session *loan.Session) { + //set session header too. + w.Header().Add("Access-Control-Expose-Headers", "Biukop-Session") + if session == nil { + w.Header().Add("Biukop-Session", "") + } else { + w.Header().Add("Biukop-Session", session.Id) + } + //add tracking cookie expiration := time.Now().Add(365 * 24 * time.Hour) mid := apiV1GetMachineId(r) @@ -208,6 +220,7 @@ func apiV1InitSessionByHttpHeader(r *http.Request) (ss loan.Session, e error) { e = errors.New("session not found: " + sid) } return + return } func apiV1ErrorCheck(e error) { @@ -247,6 +260,25 @@ func apiV1Client403Error(w http.ResponseWriter, r *http.Request, ss *loan.Sessio log.Warnf("Not authorized http(%s) path= %s, %s", r.Method, r.URL.Path, dump) } +func apiV1Client404Error(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + w.WriteHeader(404) + type struct404 struct { + Error int + ErrorMsg string + } + e404 := struct404{Error: 404, ErrorMsg: "Not Found " + time.Now().Format(time.RFC1123)} + msg404, _ := json.Marshal(e404) + + //before send out + apiV1AddTrackingCookie(w, r, ss) //always the last one to set cookies + fmt.Fprintln(w, string(msg404)) + + //write log + dump := logRequestDebug(httputil.DumpRequest(r, true)) + dump = strings.TrimSpace(dump) + log.Warnf("Not found http(%s) path= %s, %s", r.Method, r.URL.Path, dump) +} + func apiV1DumpRequest(w http.ResponseWriter, r *http.Request, ss *loan.Session) { dump := logRequestDebug(httputil.DumpRequest(r, true)) dump = strings.TrimSpace(dump)