Esempio n. 1
0
func fetchUsers(w http.ResponseWriter, r *http.Request, dbl seed.DbLayer) {

	r.ParseForm()
	queryStr := `[{"c": ["all"]}]`
	if len(r.Form["field"]) == 1 && len(r.Form["value"]) == 1 {
		queryStr = fmt.Sprintf(`[{"eq": "%s", "in": ["%s"]}]`, r.Form["value"][0], r.Form["field"][0])
		fmt.Println("Searching with ", queryStr)
	}

	queryResult, err := dbl.Query("Users", queryStr)
	if err != nil {
		fmt.Println("Failed to query ", err)
		return
	}

	// we don't need to do this at all since the dblayer did it for us but
	// useful for debugging right now
	var usersCollection []seed.User
	json.Unmarshal(queryResult, &usersCollection)
	fmt.Println(queryResult)
	for id := range usersCollection {
		fmt.Println("Username is ", usersCollection[id].Username)
	}

	w.Write(queryResult)
}
Esempio n. 2
0
func postUsers(w http.ResponseWriter, r *http.Request, dbl seed.DbLayer) {
	var users []seed.User

	body := make([]byte, r.ContentLength)
	_, err := io.ReadFull(r.Body, body)
	if err != nil {
		http.Error(w, "Error reading stream.", 500)
		return
	}

	err = json.Unmarshal(body, &users)
	if err != nil {
		http.Error(w, fmt.Sprintf("Failed to parse JSON: %s", err), 500)
		return
	}

	_, err = dbl.CreateUsers(users)
	if err != nil {
		http.Error(w, fmt.Sprintf("Error creating users: ", err), 500)
		return
	}

	return
}