package main import ( "biukop.com/sfm/loan" "database/sql" "encoding/json" "github.com/brianvoe/gofakeit/v6" 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 p.Enabled = input.Enabled 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) } func apiV1PeoplePut(w http.ResponseWriter, r *http.Request, ss *loan.Session) { p := loan.People{} p.Id = gofakeit.UUID() e := p.Write() if e != nil { log.Error("cannot create people by ", e.Error()) apiV1Server500Error(w, r) return } p.WriteAvatar(randomDummyImage()) apiV1SendJson(p, w, r, ss) } func apiV1PeopleDelete(w http.ResponseWriter, r *http.Request, ss *loan.Session) { id := r.URL.Path[len(apiV1Prefix+"people/"):] e := loan.DeletePeople(id) if e != nil { log.Error("cannot delete people by ", id, e.Error()) apiV1Server500Error(w, r) return } apiV1SendJson(id, w, r, ss) } func apiV1PeopleExtraGet(w http.ResponseWriter, r *http.Request, ss *loan.Session) { id := r.URL.Path[len(apiV1Prefix+"people-extra/"):] ret := UserExtra{} ret.Role = loan.GetRoleById(id) //TODO; check manager and account role switch ret.Role { case "people": apiV1SendJson(ret, w, r, ss) return case "user": u := loan.User{} e := u.Read(id) ret.Login = u.Login if e != nil { log.Error("cannot find user by id", id) apiV1Client404Error(w, r, ss) return } apiV1SendJson(ret, w, r, ss) return case "broker": b := loan.Broker{} e := b.Read(id) if e != nil { // this is broker if e != sql.ErrNoRows { log.Error("cannot find user by id", id) } apiV1Client404Error(w, r, ss) ret.Role = "user" return } ret.Login = b.Login ret.BSB = b.BSB ret.ACC = b.ACC ret.Organization = b.Organization ret.License = b.License apiV1SendJson(ret, w, r, ss) return case "admin": u := loan.User{} e := u.Read(id) ret.Login = u.Login if e != nil { log.Error("cannot find admin by id", id) apiV1Client404Error(w, r, ss) return } apiV1SendJson(ret, w, r, ss) return default: apiV1SendJson(ret, w, r, ss) return } }