Finish Favorites download

This commit is contained in:
An Phan 2016-06-04 21:42:12 +08:00
parent 44e184454c
commit 6d0322f80d
4 changed files with 46 additions and 1 deletions

View file

@ -0,0 +1,23 @@
<?php
namespace App\Http\Controllers\API\Download;
use App\Http\Requests\API\Download\Request;
use App\Models\Song;
use Download;
class FavoritesController extends Controller
{
/**
* Download all songs in a playlist.
*
* @param Request $request
* @param Playlist $playlist
*
* @return
*/
public function download(Request $request)
{
return response()->download(Download::from(Song::getFavorites($request->user())));
}
}

View file

@ -58,6 +58,7 @@ Route::group(['prefix' => 'api', 'namespace' => 'API'], function () {
Route::get('album/{album}', 'AlbumController@download');
Route::get('artist/{artist}', 'ArtistController@download');
Route::get('playlist/{playlist}', 'PlaylistController@download');
Route::get('favorites', 'FavoritesController@download');
});
});
});

View file

@ -215,6 +215,27 @@ class Song extends Model
return $query->where('path', 'LIKE', "$path%");
}
/**
* Get all songs favorited by a user.
*
* @param User $user
* @param bool $toArray
*
* @return \Illuminate\Database\Eloquent\Collection|Array
*/
public static function getFavorites(User $user, $toArray = false)
{
$songs = Interaction::where([
'user_id' => $user->id,
'liked' => true,
])
->with('song')
->get()
->pluck('song');
return $toArray ? $songs->toArray() : $songs;
}
/**
* Sometimes the tags extracted from getID3 are HTML entity encoded.
* This makes sure they are always sane.

View file

@ -7,7 +7,7 @@ use App\Models\Album;
use App\Models\Artist;
use App\Models\Playlist;
use Exception;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Collection;
use Log;
use ZipArchive;