package main import ( "biukop.com/sfm/loan" "encoding/json" log "github.com/sirupsen/logrus" "net/http" ) func apiV1BrokerList(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.GetBrokerList(filter) apiV1SendJson(data, w, r, ss) } func apiV1BrokerGet(w http.ResponseWriter, r *http.Request, ss *loan.Session) { id := r.URL.Path[len(apiV1Prefix+"broker/"):] b := loan.Broker{} e := b.Read(id) if e != nil { log.Error("cannot find people by id", id) apiV1Client404Error(w, r, ss) return } apiV1SendJson(b, w, r, ss) } func decodeJsonBrokerEdit(r *http.Request) (ret loan.Broker, 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 apiV1BrokerDelete(w http.ResponseWriter, r *http.Request, ss *loan.Session) { id := r.URL.Path[len(apiV1Prefix+"broker/"):] e := loan.DeleteBroker(id) if e != nil { log.Error("cannot delete broker by id", id, e.Error()) apiV1Server500Error(w, r) return } ue := UserExtra{} ue.Login = "" ue.License = "" ue.Organization = "" ue.BSB = "" ue.ACC = "" p := loan.People{} readPeopleErr := p.Read(id) u := loan.User{} readUserErr := u.Read(id) if readUserErr == nil { ue.Role = "user" } else if readPeopleErr == nil { ue.Role = "people" } else { ue.Role = "invalid_role" } apiV1SendJson(ue, w, r, ss) } func apiV1BrokerPut(w http.ResponseWriter, r *http.Request, ss *loan.Session) { id := r.URL.Path[len(apiV1Prefix+"broker/"):] u := loan.User{} e := u.Read(id) if e != nil { // no such people log.Error("cannot create broker without user, id=", id, e.Error()) apiV1Client403Error(w, r, ss) return } b := loan.Broker{} b.User = u b.Organization = "SFM" e = b.Write() if e != nil { // no such people log.Error("cannot create broker from existing user, id=", id, u, e.Error()) apiV1Server500Error(w, r) return } ue := UserExtra{} ue.Login = b.Login ue.Role = "broker" ue.License = b.License ue.Organization = b.Organization ue.BSB = b.BSB ue.ACC = b.ACC apiV1SendJson(ue, w, r, ss) } func apiV1BrokerPost(w http.ResponseWriter, r *http.Request, ss *loan.Session) { id := r.URL.Path[len(apiV1Prefix+"broker/"):] b := loan.Broker{} e := b.Read(id) if e != nil { log.Error("cannot find broker by id", id, e.Error()) apiV1Client404Error(w, r, ss) return } input, e := decodeJsonBrokerEdit(r) if e != nil { log.Error("cannot find broker by id", id, e.Error()) apiV1Client404Error(w, r, ss) return } b.BSB = input.BSB b.ACC = input.ACC b.Display = input.Display b.First = input.First b.Last = input.Last b.License = input.License if ss.GetRole() == "admin" { b.Login = input.Login b.Organization = input.Organization } e = b.Write() if e != nil { log.Error("failed to save broker ", b, e.Error()) apiV1Client404Error(w, r, ss) return } apiV1SendJson(b, w, r, ss) }