From bf9084c1104d11f0f1858669f41fbe291eebb4a9 Mon Sep 17 00:00:00 2001 From: sp Date: Mon, 31 May 2021 05:21:03 +1000 Subject: [PATCH] support user-ex for profile editing. --- apiV1User.go | 24 +-------------------- apiV1UserEx.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ apiV1login.go | 7 ++++++ apiv1.go | 2 ++ 4 files changed, 68 insertions(+), 23 deletions(-) create mode 100644 apiV1UserEx.go diff --git a/apiV1User.go b/apiV1User.go index eaf47ac..05e02b3 100644 --- a/apiV1User.go +++ b/apiV1User.go @@ -103,7 +103,7 @@ func apiV1UserPost(w http.ResponseWriter, r *http.Request, ss *loan.Session) { u := loan.User{} e = u.Read(id) if e != nil { - log.Error("cannot save basic loan", e.Error()) + log.Error("cannot save basic user", e.Error()) apiV1SendJson(" [ Error Occurred ] : "+e.Error(), w, r, ss) return } @@ -151,25 +151,3 @@ func apiV1UserEnable(w http.ResponseWriter, r *http.Request, ss *loan.Session) { } apiV1SendJson(p.Enabled, w, r, ss) } - -func apiV1UserExGet(w http.ResponseWriter, r *http.Request, ss *loan.Session) { - id := r.URL.Path[len(apiV1Prefix+"user-ex/"):] - ret := loan.UserEx{} - e := ret.Read(id) - if e != nil { - log.Error("read people error", id, e.Error()) - apiV1Client404Error(w, r, ss) - return - } - apiV1SendJson(ret, w, r, ss) -} - -func apiV1UserExList(w http.ResponseWriter, r *http.Request, ss *loan.Session) { - filter := "" - keys, ok := r.URL.Query()["filter"] - if ok && len(keys) >= 1 { - filter = keys[0] - } - data := loan.GetUserExList(filter) - apiV1SendJson(data, w, r, ss) -} diff --git a/apiV1UserEx.go b/apiV1UserEx.go new file mode 100644 index 0000000..82ac175 --- /dev/null +++ b/apiV1UserEx.go @@ -0,0 +1,58 @@ +package main + +import ( + "biukop.com/sfm/loan" + "encoding/json" + log "github.com/sirupsen/logrus" + "net/http" +) + +func apiV1UserExList(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + filter := "" + keys, ok := r.URL.Query()["filter"] + if ok && len(keys) >= 1 { + filter = keys[0] + } + data := loan.GetUserExList(filter) + apiV1SendJson(data, w, r, ss) +} + +func apiV1UserExGet(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + id := r.URL.Path[len(apiV1Prefix+"user-ex/"):] + ret := loan.UserEx{} + e := ret.Read(id) + if e != nil { + log.Error("read people error", id, e.Error()) + apiV1Client404Error(w, r, ss) + return + } + apiV1SendJson(ret, w, r, ss) +} + +func decodeJsonUserEx(r *http.Request) (ret loan.UserEx, e error) { + decoder := json.NewDecoder(r.Body) + //decoder.DisallowUnknownFields() + e = decoder.Decode(&ret) + if e != nil { + log.Error("failed decoding json for UserEx ", e.Error()) + return + } + return +} + +func apiV1UserExPost(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + input, e := decodeJsonUserEx(r) + + log.Println(input) + if e != nil { + apiV1Client404Error(w, r, ss) + return + } + + e = input.Write() + if e != nil { + apiV1Client404Error(w, r, ss) + return + } + apiV1SendJson(input, w, r, ss) +} diff --git a/apiV1login.go b/apiV1login.go index 44651d8..4477a7a 100644 --- a/apiV1login.go +++ b/apiV1login.go @@ -53,6 +53,13 @@ func apiV1Login(w http.ResponseWriter, r *http.Request, ss *loan.Session) { if e == nil { res.add("User", u.People) res.add("UserExtra", getUserExtraForLogin(u, ss)) + uex := loan.UserEx{} + err := uex.Read(u.People.Id) + if err != nil { + log.Error("cannot read UserEx when login", err) + } else { + res.add("UserEx", uex) + } } res.add("Biukop-Session", ss.Id) res.add("Biukop-Mid", ss.Get("Biukop-Mid")) diff --git a/apiv1.go b/apiv1.go index fd1f9f2..750566e 100644 --- a/apiv1.go +++ b/apiv1.go @@ -54,6 +54,7 @@ func setupApiV1Handler() []apiV1HandlerMap { {"POST", "user-enable/", apiV1UserEnable}, {"GET", "user-ex/", apiV1UserExGet}, + {"POST", "user-ex/", apiV1UserExPost}, {"GET", "user-ex-list/", apiV1UserExList}, {"GET", "broker/", apiV1BrokerGet}, @@ -139,6 +140,7 @@ func setupApiV1Handler() []apiV1HandlerMap { {"POST", "user-enable/", apiV1UserEnable}, {"GET", "user-ex/", apiV1UserExGet}, + {"POST", "user-ex/", apiV1UserExPost}, {"GET", "user-ex-list/", apiV1UserExList}, {"GET", "broker/", apiV1BrokerGet},