示例#1
0
文件: user.go 项目: kosuda/golang-web
// 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)
		}
	}
}
示例#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)
		}
	}
}
示例#3
0
// MusicGet function
func PlaylistGet(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	log.Print("access to user get api")

	name := params.ByName("name")

	var data model.Playlist
	if err := db.Find(playlistColname, bson.M{"name": name}, &data); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	var data2 model.PlaylistDetail
	if err := db.Find(playlistDetailColname, bson.M{"name": name}, &data2); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// if id == "" {
	// 	log.Print("get all")
	//
	// 	var data model.MusicSlice
	// 	db.Find(colname, 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.Printf("get by id: id = %s", id)
	//
	// 	var data model.Artist
	// 	db.FindByID(colname, 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)
	// 	}
	// }
}