package main import ( "biukop.com/sfm/loan" "encoding/json" log "github.com/sirupsen/logrus" "net/http" "strconv" "time" ) 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 { lowerBound, _ := time.Parse("2006-01-02", "1900-01-01") if input.Settlement.Before(lowerBound) { input.Settlement = lowerBound } if input.Ts.Before(lowerBound) { input.Ts = time.Now() } e = input.Write() if e != nil { log.Error("cannot save basic loan", e.Error()) apiV1Client404Error(w, r, ss) } else { piEx := loan.PayInEx{} e = piEx.Read(input.Id) if e != nil { log.Error("weird failed to read PayInEx after successfully write PayIn", input, piEx, e.Error()) apiV1Client404Error(w, r, ss) } else { apiV1SendJson(piEx, 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) }