|
- package main
-
- import (
- "biukop.com/sfm/loan"
- "encoding/json"
- 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)
- }
-
- func decodeJsonPeopleEdit(r *http.Request) (ret loan.People, 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 apiV1PeoplePost(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, e.Error())
- apiV1Client404Error(w, r, ss)
- return
- }
-
- input, e := decodeJsonPeopleEdit(r)
- if e != nil {
- log.Error("invalid input for update people", id, e.Error())
- apiV1Client404Error(w, r, ss)
- return
- }
-
- p.First = input.First
- p.Last = input.Last
- p.Display = input.Display
-
- if ss.GetRole() == "admin" {
- p.Title = input.Title
- p.Nick = input.Nick
- p.Title = input.Title
- }
-
- e = p.Write()
- if e != nil {
- log.Error("fail to update people", p, e.Error())
- apiV1Server500Error(w, r)
- return
- }
- apiV1SendJson(p, w, r, ss)
- }
|