// // look up a record in cloudant with the provided name // func find_record(name string) (*http.Response, error) { selector := `{ "selector": { "_id": { "$gt": 0 }, "name": "` + name + `" } }` return cadb.Post("records", selector, "_find") }
// // POST /record // update an existing record with a new value // @param name // @param value // func PostRecord(w http.ResponseWriter, r *http.Request) { // get name and value from request decoder := json.NewDecoder(r.Body) var f map[string]interface{} decoder.Decode(&f) name := f["name"].(string) value := f["value"].(string) name = strings.Trim(name, " ") value = strings.Trim(value, " ") // request docs with the same name from cloudant res, err := find_record(name) if err != nil { w.WriteHeader(500) //fmt.Printf("error reading from cloudant, routes.GetRecord: %s\n", err) return } // parse response from json buf := new(bytes.Buffer) buf.ReadFrom(res.Body) js := buf.String() var response Response err = json.Unmarshal([]byte(js), &response) if err != nil { w.WriteHeader(500) estr := fmt.Sprintf("error converting documents to json, routes.PostRecord: %s\n", err) fmt.Fprint(w, estr) //fmt.Println(estr) return } // check if docs matching name are found if len(response.Docs) > 0 { // doc found, now update it doc := response.Docs[0] doc.Value = value rec, _ := json.Marshal(doc) fmt.Println(string(rec)) res, err := cadb.Post("records", string(rec), "") if err != nil { w.WriteHeader(500) fmt.Fprint(w, `{"error":"unable to update Record on cloudant"}`) //fmt.Printf("error reading from cloudant, routes.GetRecord: %s\n", err) return } fmt.Fprint(w, res) } else { w.WriteHeader(404) //fmt.Println("no records found that match name=" + name) fmt.Fprint(w, `{"error":"no Record found with name: `+name+`"}`) } }
// // PUT /record // add a new record to the database under a new unique name // @param name // @parma value // func PutRecord(w http.ResponseWriter, r *http.Request) { // get name and value from request decoder := json.NewDecoder(r.Body) var f map[string]interface{} decoder.Decode(&f) name := f["name"].(string) value := f["value"].(string) name = strings.Trim(name, " ") value = strings.Trim(value, " ") // request docs with the same name from cloudant res, err := find_record(name) if err != nil { w.WriteHeader(500) //fmt.Printf("error reading from cloudant, routes.GetRecord: %s\n", err) return } // parse response from json buf := new(bytes.Buffer) buf.ReadFrom(res.Body) js := buf.String() var response Response err = json.Unmarshal([]byte(js), &response) if err != nil { w.WriteHeader(500) estr := fmt.Sprintf("error converting documents to json, routes.GetRecord: %s\n", err) fmt.Fprint(w, estr) //fmt.Println(estr) return } // check if docs matching name are found if len(response.Docs) == 0 { record := `{"name":"` + name + `","value":"` + value + `"}` // create new record in cloudant res, err := cadb.Post("records", record, "") if err != nil { w.WriteHeader(500) //fmt.Println("error creating record which seemed to be unique") fmt.Fprint(w, `{"error":"error creating record which seemed to be unique"}`) } //fmt.Println(res) fmt.Fprint(w, res) } else { w.WriteHeader(400) //fmt.Println("a record already exists with name=" + name) fmt.Fprint(w, `{"error":"a Record already exists with name: '`+name+`'"}`) } }