func CreateUser(w http.ResponseWriter, r *http.Request) { fmt.Println("------------------ CreateUser ------------------") fmt.Println(r) fmt.Println("-----------------------------") fmt.Println(r.Body) user := model.User{} decoder := json.NewDecoder(r.Body) err := decoder.Decode(&user) if err != nil { log.Fatal(err) w.WriteHeader(http.StatusInternalServerError) return } user.Id = bson.NewObjectId() data.InsertUser(user) j, err := json.Marshal(user) if err != nil { log.Fatal(err) w.WriteHeader(http.StatusInternalServerError) return } w.WriteHeader(http.StatusCreated) fmt.Fprintf(w, string(j)) }
func UpdateUser(w http.ResponseWriter, r *http.Request) { fmt.Println("------------------ UpdateUser ------------------") vars := mux.Vars(r) if bson.IsObjectIdHex(vars["id"]) { id := bson.ObjectIdHex(vars["id"]) user := model.User{} decoder := json.NewDecoder(r.Body) err := decoder.Decode(&user) if err != nil { log.Fatal(err) w.WriteHeader(http.StatusInternalServerError) return } user.Id = id data.UpdateUser(user) j, err := json.Marshal(user) if err != nil { log.Fatal(err) w.WriteHeader(http.StatusInternalServerError) return } fmt.Fprintf(w, string(j)) } else { w.WriteHeader(http.StatusBadRequest) return } }