Exemple #1
0
func (m MainController) Explore() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {

		// retrieve user session
		session, err := store.Get(r, "user")

		songs, err := models.SongFactory{}.GetSongs()

		// catch retrieval errors
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		err = t.ExecuteTemplate(w, "index", map[string]interface{}{
			"Title":   "Explore",
			"Songs":   songs,
			"Session": session,
		})

		// catch template rendering errors
		if err != nil {
			log.Println(err)
		}
	}
}
Exemple #2
0
func (m MainController) Index() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {

		// retrieve user session
		session, err := store.Get(r, "user")

		songs, err := models.SongFactory{}.GetSongsTrending()
		users := models.UserFactory{}.GetUsersFeatured()

		// catch retrieval errors
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		// render home page template
		err = t.ExecuteTemplate(w, "index", map[string]interface{}{
			"Songs":   songs,
			"Users":   users,
			"Session": session,
		})

		// catch template rendering errors
		if err != nil {
			log.Println(err)
		}
	}
}
// Retrieve a song, details from db and authenticate url with goamz
func (s SongController) Retrieve() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {

		// retrieve user session
		session, _ := store.Get(r, "user")

		// get url parameters
		params := mux.Vars(r)

		// retrieve song by id
		song, err := models.SongFactory{}.GetSongById(params["id"])

		// song retrieval error
		if err != nil {
			log.Println(err)

			session.AddFlash("There was an error retrieving that song.")
			session.Save(r, w)

			// redirect to songs index page
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		uri := bucket.SignedURL(
			fmt.Sprintf("songs/%d/%d.mp3", song.User.Id, song.Id),
			time.Now().Add(time.Duration(10)*time.Minute))

		song.SignedUrl = &uri

		// render songs template
		err = t.ExecuteTemplate(w, "songs/show", map[string]interface{}{
			"Title":   song.Title,
			"Song":    song,
			"Session": session,
		})

		// catch template rendering errors
		if err != nil {
			log.Println(err)
		}

	}
}