Exemple #1
0
// UserGet function
func UserGet(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	log.Print("access to user get api")

	id := params.ByName("id")

	if id == "" {
		log.Print("get all")

		var users model.UserSlice
		db.Find(colname, nil, &users)
		if json, err := users.MarshalJSON(); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		} else {
			w.Header().Set("Content-Type", "application/json")
			w.Write(json)
		}
	} else {
		log.Printf("get by id: id = %s", id)

		var user model.User
		db.FindByID(colname, id, &user)
		if json, err := user.MarshalJSON(); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		} else {
			w.Header().Set("Content-Type", "application/json")
			w.Write(json)
		}
	}
}
Exemple #2
0
// MusicGet function
func MusicGet(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	log.Print("access to user get api")

	id := params.ByName("id")
	// var artist_id string
	// var limit int
	// var start int
	// var title string
	//
	// if val, ok := r.URL.Query()["artist_id"]; ok {
	// 	artist_id = val[0]
	// } else {
	// 	artist_id = ""
	// }
	//
	// if val, ok := r.URL.Query()["limit"]; ok {
	// 	limit, _ = strconv.Atoi(val[0])
	// } else {
	// 	limit = 100
	// }
	//
	// if val, ok := r.URL.Query()["title"]; ok {
	// 	title = "/" + val[0] + "/"
	// } else {
	// 	title = ""
	// }
	//
	// if val, ok := r.URL.Query()["start"]; ok {
	// 	start, _ = strconv.Atoi(val[0])
	// } else {
	// 	start = 0
	// }

	if id == "" {
		log.Print("get all")

		var data model.MusicSlice
		db.Find(musicColname, nil, &data)
		if json, err := data.MarshalJSON(); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		} else {
			w.Header().Set("Content-Type", "application/json")
			w.Write(json)
		}
	} else {
		log.Print("get by id")

		var data model.MusicSlice
		db.FindByID(musicColname, id, &data)
		if json, err := data.MarshalJSON(); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		} else {
			w.Header().Set("Content-Type", "application/json")
			w.Write(json)
		}
	}
}