コード例 #1
0
ファイル: customer.go プロジェクト: yuhuichen/gocode
func (cc CustomerController) GetCustomer(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	id := p.ByName("id") p.
	
	if !bson.IsObjectIdHex(id) {
		w.WriteHeader(404)
		return
	}

	oid := bson.ObjectIdHex(id)
	
	c := models.Customer{}

	if err := cc.session.DB(dbName).C(collectionName).FindId(oid).One(&c); err != nil {
		log.Println(id, err.Error())
		w.WriteHeader(404)
		return
	}

	cj, _ := json.Marshal(c)
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(200)
	fmt.Fprintf(w, "%s", cj)
}
コード例 #2
0
func (loccontroller Controller) LocationModifier(writer http.ResponseWriter, request *http.Request, param httprouter.Params) {

	id := param.ByName("id")
var locatn Loc
	var objretrived Loc
	if !bson.IsObjectIdHex(id) {
		writer.WriteHeader(400)
		return
	}
var qurybuld string
	nid := bson.ObjectIdHex(id
	
	json.NewDecoder(request.Body).Decode(&locatn)
	keystate := strings.Fields(locatn.State)
keycity := strings.Fields(locatn.City)
	addkey := strings.Fields(locatn.Addrs)
	

	k := append(addkey, keycity...)
	keyloc := append(k, keystate...)
	for l := 0; l < len(keyloc); l++ {
		if l == len(keyloc)-1 {
			qurybuld += keyloc[l]
		} else {
			qurybuld += keyloc[l] + "+"
		}
	}
	url := fmt.Sprintf("http://maps.google.com/maps/api/geocode/json?address=%sess&sensor=false", qurybuld)
	response, err := http.Get(url)
	if err != nil {
		panic(err)
	}

	jsndata, err := ioutil.ReadAll(response.Body)

	if err != nil {
		panic(err)
	}
	var cordnt Response

	err = json.Unmarshal(jsndata, &cordnt)
	if err != nil {
		panic(err)
	}
	if len(cordnt.Reslt) == 0 {
		writer.WriteHeader(400)
		return
	}
	locatn.Coordinate.Latitude = cordnt.Reslt[0].Geo.Loc.Latitude
	locatn.Coordinate.Longitude = cordnt.Reslt[0].Geo.Loc.Longitude

	if err := loccontroller.session.DB("jaspalgill").C("cmpe273").FindId(nid).One(&objretrived); err != nil {
		writer.WriteHeader(400)
		return
	}
locatn.id = objretrived.id
	locatn.Name = objretrived.Name
	
	if locatn.State == "" {
		locatn.State = objretrived.State
	}
	if locatn.Addrs == "" {
		locatn.Addrs = objretrived.Addrs
	}
if locatn.Zip == "" {
		locatn.Zip = objretrived.Zip
	}
if locatn.City == "" {
		locatn.City = objretrived.City
	}
	
	
	if err := loccontroller.session.DB("jaspalgill").C("cmpe273").RemoveId(nid); err != nil {
		writer.WriteHeader(400)
		return
	}

	loccontroller.session.DB("jaspalgill").C("cmpe273").Insert(locatn)

	jsn, _ := json.Marshal(locatn)


	writer.Header().Set("Content-Type", "application/json")
	writer.WriteHeader(400)
	fmt.Fprintf(writer, "%sess", jsn)

}