Skip to content

Commit

Permalink
refactor: prefer more invokeable controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
phanan committed Jan 2, 2024
1 parent 1ab8d75 commit 881ca57
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use App\Models\User;
use Illuminate\Queue\SerializesModels;

class SongStartedPlaying extends Event
class PlaybackStarted extends Event
{
use SerializesModels;

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
22 changes: 0 additions & 22 deletions app/Http/Controllers/API/Interaction/LikeController.php

This file was deleted.

27 changes: 0 additions & 27 deletions app/Http/Controllers/API/Interaction/PlayCountController.php

This file was deleted.

21 changes: 21 additions & 0 deletions app/Http/Controllers/API/Interaction/ToggleLikeSongController.php
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @property string $song The song's ID
*/
class StorePlayCountRequest extends Request
class IncreasePlayCountRequest extends Request
{
/** @return array<mixed> */
public function rules(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
/**
* @property Song $song
*/
class SongLikeRequest extends Request
class ToggleLikeSongRequest extends Request
{
}
4 changes: 2 additions & 2 deletions app/Listeners/UpdateLastfmNowPlaying.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Listeners;

use App\Events\SongStartedPlaying;
use App\Events\PlaybackStarted;
use App\Services\LastfmService;
use Illuminate\Contracts\Queue\ShouldQueue;

Expand All @@ -12,7 +12,7 @@ public function __construct(private LastfmService $lastfm)
{
}

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;
Expand Down
4 changes: 2 additions & 2 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

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;
Expand Down Expand Up @@ -35,7 +35,7 @@ class EventServiceProvider extends BaseServiceProvider
UnloveMultipleTracksOnLastfm::class,
],

SongStartedPlaying::class => [
PlaybackStarted::class => [
UpdateLastfmNowPlaying::class,
],

Expand Down
8 changes: 4 additions & 4 deletions routes/api.base.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
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;
Expand Down Expand Up @@ -101,8 +101,8 @@
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']);

Expand Down
10 changes: 5 additions & 5 deletions tests/Feature/PlayCountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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([
Expand All @@ -29,12 +29,12 @@ public function testStoreExistingEntry(): void
]);

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();
Expand All @@ -58,6 +58,6 @@ public function testStoreNewEntry(): void
->first();

self::assertSame(1, $interaction->play_count);
Event::assertDispatched(SongStartedPlaying::class);
Event::assertDispatched(PlaybackStarted::class);
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Listeners/UpdateLastfmNowPlayingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +26,6 @@ public function testUpdateNowPlayingStatus(): void
->with($song, $user)
->once();

(new UpdateLastfmNowPlaying($lastfm))->handle(new SongStartedPlaying($song, $user));
(new UpdateLastfmNowPlaying($lastfm))->handle(new PlaybackStarted($song, $user));
}
}

0 comments on commit 881ca57

Please sign in to comment.