diff --git a/apiV1PayIn.go b/apiV1PayIn.go new file mode 100644 index 0000000..6a3e455 --- /dev/null +++ b/apiV1PayIn.go @@ -0,0 +1,59 @@ +package main + +import ( + "biukop.com/sfm/loan" + "encoding/json" + log "github.com/sirupsen/logrus" + "net/http" + "strconv" +) + +func decodeJsonPayInEdit(r *http.Request) (ret loan.PayIn, e error) { + decoder := json.NewDecoder(r.Body) + //decoder.DisallowUnknownFields() + e = decoder.Decode(&ret) + if e != nil { + log.Error("failed decoding PayIn for updating", e.Error()) + return + } + return +} + +func apiV1PayInPost(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + + input, e := decodeJsonPayInEdit(r) + log.Println(input) + + if e != nil { + apiV1Client404Error(w, r, ss) + return + } else { + e = input.Write() + if e != nil { + log.Error("cannot save basic loan", e.Error()) + apiV1Client404Error(w, r, ss) + } else { + apiV1SendJson(input, w, r, ss) + } + } + +} + +func apiV1PayInDelete(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + id := r.URL.Path[len(apiV1Prefix+"payIn/"):] //remove prefix + idx, e := strconv.Atoi(id) + + if e != nil { + log.Error("cannot identify PayInId ", id, e.Error()) + apiV1Client404Error(w, r, ss) + return + } + + e = loan.DeletePayIn(int64(idx)) + if e != nil { + log.Error("cannot delete PayIn by id ", id, e.Error()) + apiV1Client404Error(w, r, ss) + return + } + apiV1SendJson(idx, w, r, ss) +} diff --git a/apiv1.go b/apiv1.go index 9203167..59c6f4c 100644 --- a/apiv1.go +++ b/apiv1.go @@ -47,6 +47,8 @@ func setupApiV1Handler() []apiV1HandlerMap { {"GET", "people-list/", apiV1PeopleList}, {"GET", "broker-list/", apiV1BrokerList}, {"POST", "sync-people/", apiV1SyncPeople}, + {"POST", "payIn/", apiV1PayInPost}, + {"DELETE", "payIn/", apiV1PayInDelete}, {"GET", "login", apiV1DumpRequest}, } } else { //production @@ -70,6 +72,8 @@ func setupApiV1Handler() []apiV1HandlerMap { {"GET", "people-list", apiV1PeopleList}, {"GET", "broker-list/", apiV1BrokerList}, {"POST", "sync-people/", apiV1SyncPeople}, + {"POST", "payIn/", apiV1PayInPost}, + {"DELETE", "payIn/", apiV1PayInDelete}, {"GET", "login", apiV1EmptyResponse}, } }