Example #1
0
func (cc CustomerController) CreateCustomer(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
	u := models.Customer{}
	json.NewDecoder(req.Body).Decode(&u)
	u.Id = bson.NewObjectId()
	cc.session.DB("go_rest_tutorial").C("customers").Insert(u)
	uj, _ := json.Marshal(u)
	res.Header().Set("Content-Type", "application/json")
	res.WriteHeader(201)
	fmt.Fprintf(res, "%s", uj)

}
Example #2
0
func CreateCustomer1(res http.ResponseWriter, req *http.Request, params httprouter.Params, next http.HandlerFunc) {
	u := models.Customer{}
	json.NewDecoder(req.Body).Decode(&u)
	u.Id = bson.NewObjectId()
	err := mgo.Session.DB("go_rest_tutorial").C("customers").Insert(u)
	fmt.Printf("%s", err)
	uj, _ := json.Marshal(u)
	res.Header().Set("Content-Type", "application/json")
	res.WriteHeader(201)
	fmt.Fprintf(res, "%s", uj)
	next(res, req)

}
Example #3
0
func (cc CustomerController) UpdateCustomer(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
	id := params.ByName("id")
	if !bson.IsObjectIdHex(id) {
		res.WriteHeader(404)
		return
	}
	u := models.Customer{}
	u.Id = bson.ObjectIdHex(id)
	json.NewDecoder(req.Body).Decode(&u)
	cc.session.DB("go_rest_tutorial").C("customers").UpsertId(u.Id, u)
	uj, _ := json.Marshal(u)
	res.Header().Set("Content-Type", "application/json")
	res.WriteHeader(200)
	fmt.Fprintf(res, "%s", uj)

}