Skip to content

Commit

Permalink
Add "Recently Played" playlist (#839)
Browse files Browse the repository at this point in the history
* Add "Recently Played" playlist

* Apply fixes from StyleCI (#838)
  • Loading branch information
phanan committed Oct 20, 2018
1 parent ee6ac68 commit 6f0db16
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
6 changes: 6 additions & 0 deletions app/Http/Controllers/API/DataController.php
Expand Up @@ -17,6 +17,8 @@

class DataController extends Controller
{
private const RECENTLY_PLAYED_EXCERPT_COUNT = 7;

private $lastfmService;
private $youTubeService;
private $iTunesService;
Expand Down Expand Up @@ -60,6 +62,10 @@ public function index(Request $request)
'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(),
Expand Down
23 changes: 23 additions & 0 deletions app/Http/Controllers/API/Interaction/RecentlyPlayedController.php
@@ -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));
}
}
21 changes: 21 additions & 0 deletions app/Repositories/InteractionRepository.php
Expand Up @@ -5,6 +5,7 @@
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
Expand All @@ -29,4 +30,24 @@ public function getUserFavorites(User $user): Collection
->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();
}
}
1 change: 1 addition & 0 deletions app/Services/InteractionService.php
Expand Up @@ -4,6 +4,7 @@

use App\Events\SongLikeToggled;
use App\Models\Interaction;
use App\Models\Song;
use App\Models\User;

class InteractionService
Expand Down
3 changes: 3 additions & 0 deletions routes/api.php
Expand Up @@ -40,6 +40,9 @@
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');
Expand Down

0 comments on commit 6f0db16

Please sign in to comment.