mirror of
https://github.com/koel/koel
synced 2024-11-10 06:34:14 +00:00
refactor: prefer more invokeable controllers
This commit is contained in:
parent
1ab8d7590f
commit
881ca574ae
12 changed files with 65 additions and 67 deletions
|
@ -6,7 +6,7 @@ use App\Models\Song;
|
|||
use App\Models\User;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class SongStartedPlaying extends Event
|
||||
class PlaybackStarted extends Event
|
||||
{
|
||||
use SerializesModels;
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API\Interaction;
|
||||
|
||||
use App\Events\PlaybackStarted;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\API\Interaction\IncreasePlayCountRequest;
|
||||
use App\Http\Resources\InteractionResource;
|
||||
use App\Models\User;
|
||||
use App\Services\InteractionService;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
|
||||
class HandlePlaybackStartedController extends Controller
|
||||
{
|
||||
/** @param User $user */
|
||||
public function __invoke(
|
||||
IncreasePlayCountRequest $request,
|
||||
InteractionService $interactionService,
|
||||
Authenticatable $user
|
||||
) {
|
||||
$interaction = $interactionService->increasePlayCount($request->song, $user);
|
||||
event(new PlaybackStarted($interaction->song, $interaction->user));
|
||||
|
||||
return InteractionResource::make($interaction);
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API\Interaction;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\API\SongLikeRequest;
|
||||
use App\Models\User;
|
||||
use App\Services\InteractionService;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
|
||||
class LikeController extends Controller
|
||||
{
|
||||
/** @param User $user */
|
||||
public function __construct(private InteractionService $interactionService, private ?Authenticatable $user)
|
||||
{
|
||||
}
|
||||
|
||||
public function store(SongLikeRequest $request)
|
||||
{
|
||||
return response()->json($this->interactionService->toggleLike($request->song, $this->user));
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API\Interaction;
|
||||
|
||||
use App\Events\SongStartedPlaying;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\API\Interaction\StorePlayCountRequest;
|
||||
use App\Http\Resources\InteractionResource;
|
||||
use App\Models\User;
|
||||
use App\Services\InteractionService;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
|
||||
class PlayCountController extends Controller
|
||||
{
|
||||
/** @param User $user */
|
||||
public function __construct(private InteractionService $interactionService, private ?Authenticatable $user)
|
||||
{
|
||||
}
|
||||
|
||||
public function store(StorePlayCountRequest $request)
|
||||
{
|
||||
$interaction = $this->interactionService->increasePlayCount($request->song, $this->user);
|
||||
event(new SongStartedPlaying($interaction->song, $interaction->user));
|
||||
|
||||
return InteractionResource::make($interaction);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API\Interaction;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\API\ToggleLikeSongRequest;
|
||||
use App\Models\User;
|
||||
use App\Services\InteractionService;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
|
||||
class ToggleLikeSongController extends Controller
|
||||
{
|
||||
/** @param User $user */
|
||||
public function __invoke(
|
||||
ToggleLikeSongRequest $request,
|
||||
InteractionService $interactionService,
|
||||
Authenticatable $user
|
||||
) {
|
||||
return response()->json($interactionService->toggleLike($request->song, $user));
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ use App\Http\Requests\API\Request;
|
|||
/**
|
||||
* @property string $song The song's ID
|
||||
*/
|
||||
class StorePlayCountRequest extends Request
|
||||
class IncreasePlayCountRequest extends Request
|
||||
{
|
||||
/** @return array<mixed> */
|
||||
public function rules(): array
|
|
@ -7,6 +7,6 @@ use App\Models\Song;
|
|||
/**
|
||||
* @property Song $song
|
||||
*/
|
||||
class SongLikeRequest extends Request
|
||||
class ToggleLikeSongRequest extends Request
|
||||
{
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Events\SongStartedPlaying;
|
||||
use App\Events\PlaybackStarted;
|
||||
use App\Services\LastfmService;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
|
@ -12,7 +12,7 @@ class UpdateLastfmNowPlaying implements ShouldQueue
|
|||
{
|
||||
}
|
||||
|
||||
public function handle(SongStartedPlaying $event): void
|
||||
public function handle(PlaybackStarted $event): void
|
||||
{
|
||||
if (!LastfmService::enabled() || !$event->user->lastfm_session_key || $event->song->artist->is_unknown) {
|
||||
return;
|
||||
|
|
|
@ -4,10 +4,10 @@ namespace App\Providers;
|
|||
|
||||
use App\Events\LibraryChanged;
|
||||
use App\Events\MediaSyncCompleted;
|
||||
use App\Events\PlaybackStarted;
|
||||
use App\Events\SongLikeToggled;
|
||||
use App\Events\SongsBatchLiked;
|
||||
use App\Events\SongsBatchUnliked;
|
||||
use App\Events\SongStartedPlaying;
|
||||
use App\Listeners\ClearMediaCache;
|
||||
use App\Listeners\DeleteNonExistingRecordsPostSync;
|
||||
use App\Listeners\LoveMultipleTracksOnLastfm;
|
||||
|
@ -35,7 +35,7 @@ class EventServiceProvider extends BaseServiceProvider
|
|||
UnloveMultipleTracksOnLastfm::class,
|
||||
],
|
||||
|
||||
SongStartedPlaying::class => [
|
||||
PlaybackStarted::class => [
|
||||
UpdateLastfmNowPlaying::class,
|
||||
],
|
||||
|
||||
|
|
|
@ -22,8 +22,8 @@ use App\Http\Controllers\API\FetchSongsForQueueController;
|
|||
use App\Http\Controllers\API\GenreController;
|
||||
use App\Http\Controllers\API\GenreSongController;
|
||||
use App\Http\Controllers\API\Interaction\BatchLikeController;
|
||||
use App\Http\Controllers\API\Interaction\LikeController;
|
||||
use App\Http\Controllers\API\Interaction\PlayCountController;
|
||||
use App\Http\Controllers\API\Interaction\HandlePlaybackStartedController;
|
||||
use App\Http\Controllers\API\Interaction\ToggleLikeSongController;
|
||||
use App\Http\Controllers\API\ObjectStorage\S3\SongController as S3SongController;
|
||||
use App\Http\Controllers\API\PlaylistController;
|
||||
use App\Http\Controllers\API\PlaylistFolderController;
|
||||
|
@ -101,8 +101,8 @@ Route::prefix('api')->middleware('api')->group(static function (): void {
|
|||
Route::post('upload', UploadController::class);
|
||||
|
||||
// Interaction routes
|
||||
Route::post('interaction/play', [PlayCountController::class, 'store']);
|
||||
Route::post('interaction/like', [LikeController::class, 'store']);
|
||||
Route::post('interaction/play', HandlePlaybackStartedController::class);
|
||||
Route::post('interaction/like', ToggleLikeSongController::class);
|
||||
Route::post('interaction/batch/like', [BatchLikeController::class, 'store']);
|
||||
Route::post('interaction/batch/unlike', [BatchLikeController::class, 'destroy']);
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Events\SongStartedPlaying;
|
||||
use App\Events\PlaybackStarted;
|
||||
use App\Models\Interaction;
|
||||
use App\Models\Song;
|
||||
use App\Models\User;
|
||||
|
@ -12,7 +12,7 @@ class PlayCountTest extends TestCase
|
|||
{
|
||||
public function testStoreExistingEntry(): void
|
||||
{
|
||||
Event::fake(SongStartedPlaying::class);
|
||||
Event::fake(PlaybackStarted::class);
|
||||
|
||||
/** @var Interaction $interaction */
|
||||
$interaction = Interaction::factory()->create([
|
||||
|
@ -29,12 +29,12 @@ class PlayCountTest extends TestCase
|
|||
]);
|
||||
|
||||
self::assertSame(11, $interaction->refresh()->play_count);
|
||||
Event::assertDispatched(SongStartedPlaying::class);
|
||||
Event::assertDispatched(PlaybackStarted::class);
|
||||
}
|
||||
|
||||
public function testStoreNewEntry(): void
|
||||
{
|
||||
Event::fake(SongStartedPlaying::class);
|
||||
Event::fake(PlaybackStarted::class);
|
||||
|
||||
/** @var Song $song */
|
||||
$song = Song::factory()->create();
|
||||
|
@ -58,6 +58,6 @@ class PlayCountTest extends TestCase
|
|||
->first();
|
||||
|
||||
self::assertSame(1, $interaction->play_count);
|
||||
Event::assertDispatched(SongStartedPlaying::class);
|
||||
Event::assertDispatched(PlaybackStarted::class);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Tests\Unit\Listeners;
|
||||
|
||||
use App\Events\SongStartedPlaying;
|
||||
use App\Events\PlaybackStarted;
|
||||
use App\Listeners\UpdateLastfmNowPlaying;
|
||||
use App\Models\Song;
|
||||
use App\Models\User;
|
||||
|
@ -26,6 +26,6 @@ class UpdateLastfmNowPlayingTest extends TestCase
|
|||
->with($song, $user)
|
||||
->once();
|
||||
|
||||
(new UpdateLastfmNowPlaying($lastfm))->handle(new SongStartedPlaying($song, $user));
|
||||
(new UpdateLastfmNowPlaying($lastfm))->handle(new PlaybackStarted($song, $user));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue