Add "Recently Played" playlist (#839)

* Add "Recently Played" playlist

* Apply fixes from StyleCI (#838)
This commit is contained in:
Phan An 2018-10-20 23:46:12 +02:00 committed by GitHub
parent ee6ac687c4
commit 6f0db1620f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 1 deletions

View file

@ -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(),

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\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();
}
}

View file

@ -4,6 +4,7 @@ namespace App\Services;
use App\Events\SongLikeToggled;
use App\Models\Interaction;
use App\Models\Song;
use App\Models\User;
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/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');