Add "Recently Played" playlist

This commit is contained in:
Phan An 2018-10-20 23:44:11 +02:00
parent ee6ac687c4
commit 239171f2e6
6 changed files with 56 additions and 1 deletions

View file

@ -17,6 +17,8 @@ use Illuminate\Http\Request;
class DataController extends Controller class DataController extends Controller
{ {
private const RECENTLY_PLAYED_EXCERPT_COUNT = 7;
private $lastfmService; private $lastfmService;
private $youTubeService; private $youTubeService;
private $iTunesService; private $iTunesService;
@ -60,6 +62,10 @@ class DataController extends Controller
'settings' => $request->user()->is_admin ? $this->settingRepository->getAllAsKeyValueArray() : [], 'settings' => $request->user()->is_admin ? $this->settingRepository->getAllAsKeyValueArray() : [],
'playlists' => $this->playlistRepository->getAllByCurrentUser(), 'playlists' => $this->playlistRepository->getAllByCurrentUser(),
'interactions' => $this->interactionRepository->getAllByCurrentUser(), 'interactions' => $this->interactionRepository->getAllByCurrentUser(),
'recentlyPlayed' => $this->interactionRepository->getRecentlyPlayed(
$request->user(),
self::RECENTLY_PLAYED_EXCERPT_COUNT
),
'users' => $request->user()->is_admin ? $this->userRepository->getAll() : [], 'users' => $request->user()->is_admin ? $this->userRepository->getAll() : [],
'currentUser' => $request->user(), 'currentUser' => $request->user(),
'useLastfm' => $this->lastfmService->used(), 'useLastfm' => $this->lastfmService->used(),

View file

@ -0,0 +1,23 @@
<?php
namespace App\Http\Controllers\API\Interaction;
use App\Repositories\InteractionRepository;
use App\Services\InteractionService;
use Illuminate\Http\Request;
class RecentlyPlayedController extends Controller
{
private $interactionRepository;
public function __construct(InteractionService $interactionService, InteractionRepository $interactionRepository)
{
parent::__construct($interactionService);
$this->interactionRepository = $interactionRepository;
}
public function index(Request $request, ?int $count = null)
{
return response()->json($this->interactionRepository->getRecentlyPlayed($request->user(), $count));
}
}

View file

@ -5,6 +5,7 @@ namespace App\Repositories;
use App\Models\Interaction; use App\Models\Interaction;
use App\Models\User; use App\Models\User;
use App\Repositories\Traits\ByCurrentUser; use App\Repositories\Traits\ByCurrentUser;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
class InteractionRepository extends AbstractRepository class InteractionRepository extends AbstractRepository
@ -29,4 +30,24 @@ class InteractionRepository extends AbstractRepository
->get() ->get()
->pluck('song'); ->pluck('song');
} }
/**
* @return Interaction[]
*/
public function getRecentlyPlayed(User $user, ?int $count = null): array
{
/** @var Builder $query */
$query = $this->model
->where('user_id', $user->id)
->orderBy('updated_at', 'DESC');
if ($count) {
$query = $query->take($count);
}
return $query
->get()
->pluck('song_id')
->all();
}
} }

View file

@ -4,7 +4,9 @@ namespace App\Services;
use App\Events\SongLikeToggled; use App\Events\SongLikeToggled;
use App\Models\Interaction; use App\Models\Interaction;
use App\Models\Song;
use App\Models\User; use App\Models\User;
use Illuminate\Database\Query\Builder;
class InteractionService class InteractionService
{ {

@ -1 +1 @@
Subproject commit 2053ab3f60958838147b1db529aee6d8e3bea5bb Subproject commit e5a80b03133498f3a9c1887ba4853c9e8bd27ec1

View file

@ -40,6 +40,9 @@ Route::group(['namespace' => 'API'], function () {
Route::post('interaction/like', 'Interaction\LikeController@store'); Route::post('interaction/like', 'Interaction\LikeController@store');
Route::post('interaction/batch/like', 'Interaction\BatchLikeController@store'); Route::post('interaction/batch/like', 'Interaction\BatchLikeController@store');
Route::post('interaction/batch/unlike', 'Interaction\BatchLikeController@destroy'); Route::post('interaction/batch/unlike', 'Interaction\BatchLikeController@destroy');
Route::get('interaction/recently-played/{count?}', 'Interaction\RecentlyPlayedController@index')->where([
'count' => '\d+',
]);
// Playlist routes // Playlist routes
Route::resource('playlist', 'PlaylistController'); Route::resource('playlist', 'PlaylistController');