// CreateUser creates a new user resource func (uc UserController) CreateUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) { // Grab http params mth := p.ByName("methode") // Stub an user to be populated from the body u := models.User{} // Populate the user data json.NewDecoder(r.Body).Decode(&u) // Add an Id u.Id = bson.NewObjectId() if mth == "add_user" { uc.session.DB("hereiam").C("users").Insert(u) } // Marshal provided interface into JSON structure uj, _ := json.Marshal(u) // Write content-type, statuscode, payload w.Header().Set("Content-Type", "application/json") w.WriteHeader(201) fmt.Fprintf(w, "%s", uj) fmt.Printf("%s", uj) }
// send Messages user retrieves an individual user resource func (uc UserController) SendMessages(w http.ResponseWriter, r *http.Request, p httprouter.Params) { messages := p.ByName("messages") from := p.ByName("from") // Stub user u := models.User{} // Populate the user data json.NewDecoder(r.Body).Decode(&u) // Fetch user if err := uc.session.DB("hereiam").C("users").Find(bson.M{"phone": u.Phone}).One(&u); err != nil { w.WriteHeader(404) return } sendGCMMessages(1, from, u.Regid, u.Name, u.Email, messages) // Marshal provided interface into JSON structure uj, _ := json.Marshal(u) // Write content-type, statuscode, payload w.Header().Set("Content-Type", "application/json") w.WriteHeader(200) fmt.Fprintf(w, "%s", uj) fmt.Printf("%s", uj) }
// GetUser retrieves an individual user resource func (uc UserController) GetUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) { // Grab phone phone := p.ByName("phone") // Stub user u := models.User{} // Fetch user if err := uc.session.DB("hereiam").C("users").Find(bson.M{"phone": phone}).One(&u); err != nil { w.WriteHeader(404) return } // Marshal provided interface into JSON structure uj, _ := json.Marshal(u) // Write content-type, statuscode, payload w.Header().Set("Content-Type", "application/json") w.WriteHeader(200) fmt.Fprintf(w, "%s", uj) fmt.Printf("%s", uj) }
// RemoveUser removes an existing user resource func (uc UserController) RemoveUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) { // Grab id id := p.ByName("id") // Verify id is ObjectId, otherwise bail if !bson.IsObjectIdHex(id) { w.WriteHeader(404) return } // Grab id oid := bson.ObjectIdHex(id) // Remove user if err := uc.session.DB("hereiam").C("users").RemoveId(oid); err != nil { w.WriteHeader(404) return } // Write status w.WriteHeader(200) }