diff --git a/.idea/SFM_Loan_RestApi.iml b/.idea/SFM_Loan_RestApi.iml index 5e764c4..0dc9279 100644 --- a/.idea/SFM_Loan_RestApi.iml +++ b/.idea/SFM_Loan_RestApi.iml @@ -2,6 +2,7 @@ + diff --git a/apiV1PeopleList.go b/apiV1PeopleList.go new file mode 100644 index 0000000..9158d14 --- /dev/null +++ b/apiV1PeopleList.go @@ -0,0 +1,29 @@ +package main + +import ( + "biukop.com/sfm/loan" + log "github.com/sirupsen/logrus" + "net/http" +) + +func apiV1PeopleList(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.GetPeopleList(filter) + apiV1SendJson(data, w, r, ss) +} + +func apiV1PeopleGet(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + id := r.URL.Path[len(apiV1Prefix+"people/"):] + p := loan.People{} + e := p.Read(id) + if e != nil { + log.Error("cannot find people by id", id) + apiV1Client404Error(w, r, ss) + return + } + apiV1SendJson(p, w, r, ss) +} diff --git a/apiV1Reward.go b/apiV1Reward.go new file mode 100644 index 0000000..1e1c6cb --- /dev/null +++ b/apiV1Reward.go @@ -0,0 +1,66 @@ +package main + +import ( + "biukop.com/sfm/loan" + "encoding/json" + log "github.com/sirupsen/logrus" + "net/http" + "strconv" +) + +func decodeJsonRewardEdit(r *http.Request) (ret loan.Reward, e error) { + decoder := json.NewDecoder(r.Body) + //decoder.DisallowUnknownFields() + e = decoder.Decode(&ret) + if e != nil { + log.Error("failed decoding json for edit Reward ", e.Error()) + return + } + return +} +func apiV1RewardPost(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + + re := loan.Reward{} + + input, e := decodeJsonRewardEdit(r) + + log.Println(input) + if e != nil { + apiV1Client404Error(w, r, ss) + return + } else { + re.Id = input.Id + re.PayoutId = input.PayoutId + re.Description = input.Description + re.LoanId = input.LoanId + re.Amount = input.Amount + re.From = input.From + re.To = input.To + re.Ts = input.Ts + e = re.Write() + if e != nil { + apiV1Client404Error(w, r, ss) + } else { + apiV1SendJson(re, w, r, ss) + } + + } +} + +func apiV1RewardDelete(w http.ResponseWriter, r *http.Request, ss *loan.Session) { + id := r.URL.Path[len(apiV1Prefix+"reward/"):] + id64, e := strconv.Atoi(id) + if e != nil { + log.Error("cannot find people by id", id) + apiV1Client404Error(w, r, ss) + return + } + + e = loan.DeleteReward(int64(id64)) + if e != nil { + log.Error("cannot find people by id", id) + apiV1Client404Error(w, r, ss) + return + } + apiV1SendJson(id64, w, r, ss) +} diff --git a/apiv1.go b/apiv1.go index 8525cc9..49377f8 100644 --- a/apiv1.go +++ b/apiv1.go @@ -37,8 +37,12 @@ func setupApiV1Handler() []apiV1HandlerMap { {"GET", "chart/top-broker", apiV1ChartTopBroker}, {"POST", "grid/loan/full-loan-overview", apiV1GridLoanFullOverview}, {"GET", "loan/", apiV1LoanSingleGet}, + {"GET", "people/", apiV1PeopleGet}, {"POST", "loan/basic/", apiV1LoanSinglePostBasic}, {"GET", "avatar/", apiV1Avatar}, + {"POST", "reward/", apiV1RewardPost}, + {"DELETE", "reward/", apiV1RewardDelete}, + {"GET", "people-list", apiV1PeopleList}, {"GET", "login", apiV1DumpRequest}, } } else { //production @@ -52,8 +56,12 @@ func setupApiV1Handler() []apiV1HandlerMap { {"GET", "chart/top-broker", apiV1ChartTopBroker}, {"POST", "grid/loan/full-loan-overview", apiV1GridLoanFullOverview}, {"GET", "loan/", apiV1LoanSingleGet}, + {"GET", "people/", apiV1PeopleGet}, {"POST", "loan/basic/", apiV1LoanSinglePostBasic}, {"GET", "avatar/", apiV1Avatar}, + {"POST", "reward/", apiV1RewardPost}, + {"DELETE", "reward/", apiV1RewardDelete}, + {"GET", "people-list", apiV1PeopleList}, {"GET", "login", apiV1EmptyResponse}, } }