From 239171f2e62e4a4337dfe68a7e7ce4d46d44d6e0 Mon Sep 17 00:00:00 2001 From: Phan An Date: Sat, 20 Oct 2018 23:44:11 +0200 Subject: [PATCH] Add "Recently Played" playlist --- app/Http/Controllers/API/DataController.php | 6 +++++ .../Interaction/RecentlyPlayedController.php | 23 +++++++++++++++++++ app/Repositories/InteractionRepository.php | 21 +++++++++++++++++ app/Services/InteractionService.php | 2 ++ resources/assets | 2 +- routes/api.php | 3 +++ 6 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/API/Interaction/RecentlyPlayedController.php diff --git a/app/Http/Controllers/API/DataController.php b/app/Http/Controllers/API/DataController.php index 4e7ab962..635c0ef3 100644 --- a/app/Http/Controllers/API/DataController.php +++ b/app/Http/Controllers/API/DataController.php @@ -17,6 +17,8 @@ use Illuminate\Http\Request; class DataController extends Controller { + private const RECENTLY_PLAYED_EXCERPT_COUNT = 7; + private $lastfmService; private $youTubeService; private $iTunesService; @@ -60,6 +62,10 @@ class DataController extends Controller 'settings' => $request->user()->is_admin ? $this->settingRepository->getAllAsKeyValueArray() : [], 'playlists' => $this->playlistRepository->getAllByCurrentUser(), 'interactions' => $this->interactionRepository->getAllByCurrentUser(), + 'recentlyPlayed' => $this->interactionRepository->getRecentlyPlayed( + $request->user(), + self::RECENTLY_PLAYED_EXCERPT_COUNT + ), 'users' => $request->user()->is_admin ? $this->userRepository->getAll() : [], 'currentUser' => $request->user(), 'useLastfm' => $this->lastfmService->used(), diff --git a/app/Http/Controllers/API/Interaction/RecentlyPlayedController.php b/app/Http/Controllers/API/Interaction/RecentlyPlayedController.php new file mode 100644 index 00000000..5089fd72 --- /dev/null +++ b/app/Http/Controllers/API/Interaction/RecentlyPlayedController.php @@ -0,0 +1,23 @@ +interactionRepository = $interactionRepository; + } + + public function index(Request $request, ?int $count = null) + { + return response()->json($this->interactionRepository->getRecentlyPlayed($request->user(), $count)); + } +} diff --git a/app/Repositories/InteractionRepository.php b/app/Repositories/InteractionRepository.php index 491c2a40..7a96bc14 100644 --- a/app/Repositories/InteractionRepository.php +++ b/app/Repositories/InteractionRepository.php @@ -5,6 +5,7 @@ namespace App\Repositories; use App\Models\Interaction; use App\Models\User; use App\Repositories\Traits\ByCurrentUser; +use Illuminate\Database\Query\Builder; use Illuminate\Support\Collection; class InteractionRepository extends AbstractRepository @@ -29,4 +30,24 @@ class InteractionRepository extends AbstractRepository ->get() ->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(); + } } diff --git a/app/Services/InteractionService.php b/app/Services/InteractionService.php index 83827a94..3679b947 100644 --- a/app/Services/InteractionService.php +++ b/app/Services/InteractionService.php @@ -4,7 +4,9 @@ namespace App\Services; use App\Events\SongLikeToggled; use App\Models\Interaction; +use App\Models\Song; use App\Models\User; +use Illuminate\Database\Query\Builder; class InteractionService { diff --git a/resources/assets b/resources/assets index 2053ab3f..e5a80b03 160000 --- a/resources/assets +++ b/resources/assets @@ -1 +1 @@ -Subproject commit 2053ab3f60958838147b1db529aee6d8e3bea5bb +Subproject commit e5a80b03133498f3a9c1887ba4853c9e8bd27ec1 diff --git a/routes/api.php b/routes/api.php index 13967231..dee19cf1 100644 --- a/routes/api.php +++ b/routes/api.php @@ -40,6 +40,9 @@ Route::group(['namespace' => 'API'], function () { Route::post('interaction/like', 'Interaction\LikeController@store'); Route::post('interaction/batch/like', 'Interaction\BatchLikeController@store'); Route::post('interaction/batch/unlike', 'Interaction\BatchLikeController@destroy'); + Route::get('interaction/recently-played/{count?}', 'Interaction\RecentlyPlayedController@index')->where([ + 'count' => '\d+', + ]); // Playlist routes Route::resource('playlist', 'PlaylistController');