Skip to content

Commit

Permalink
feat(plus): add song interaction tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phanan committed Jul 6, 2024
1 parent de44bc7 commit 928a2eb
Show file tree
Hide file tree
Showing 20 changed files with 264 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;

class SongsBatchLiked extends Event
class MultipleSongsLiked extends Event
{
use SerializesModels;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;

class SongsBatchUnliked extends Event
class MultipleSongsUnliked extends Event
{
use SerializesModels;

Expand Down
42 changes: 0 additions & 42 deletions app/Http/Controllers/API/Interaction/BatchLikeController.php

This file was deleted.

25 changes: 25 additions & 0 deletions app/Http/Controllers/API/LikeMultipleSongsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Http\Requests\API\InteractWithMultipleSongsRequest;
use App\Models\Song;
use App\Models\User;
use App\Services\InteractionService;
use Illuminate\Contracts\Auth\Authenticatable;

class LikeMultipleSongsController extends Controller
{
/** @param User $user */
public function __invoke(
InteractWithMultipleSongsRequest $request,
InteractionService $interactionService,
Authenticatable $user
) {
$songs = Song::query()->findMany($request->songs);
$songs->each(fn (Song $song) => $this->authorize('access', $song));

return response()->json($interactionService->likeMany($songs, $user));
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/API/MakeSongsPublicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __invoke(
SongService $songService,
Authenticatable $user
) {
$songs = Song::find($request->songs);
$songs = Song::query()->find($request->songs);
$songs->each(fn ($song) => $this->authorize('own', $song));

$songService->makeSongsPublic($songs);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
<?php

namespace App\Http\Controllers\API\Interaction;
namespace App\Http\Controllers\API;

use App\Events\PlaybackStarted;
use App\Http\Controllers\Controller;
use App\Http\Requests\API\Interaction\IncreasePlayCountRequest;
use App\Http\Resources\InteractionResource;
use App\Models\Song;
use App\Models\User;
use App\Services\InteractionService;
use Illuminate\Contracts\Auth\Authenticatable;

class HandlePlaybackStartedController extends Controller
class RegisterPlayController extends Controller
{
/** @param User $user */
public function __invoke(
IncreasePlayCountRequest $request,
InteractionService $interactionService,
Authenticatable $user
?Authenticatable $user
) {
$interaction = $interactionService->increasePlayCount($request->song, $user);
/** @var Song $song */
$song = Song::query()->findOrFail($request->song);
$this->authorize('access', $song);

$interaction = $interactionService->increasePlayCount($song, $user);
event(new PlaybackStarted($interaction->song, $interaction->user));

return InteractionResource::make($interaction);
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/API/SongController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function show(Song $song)
public function update(SongUpdateRequest $request)
{
// Don't use SongRepository::findMany() because it'd be already catered to the current user.
Song::find($request->songs)->each(fn (Song $song) => $this->authorize('edit', $song));
Song::query()->find($request->songs)->each(fn (Song $song) => $this->authorize('edit', $song));

$updatedSongs = $this->songService->updateSongs($request->songs, SongUpdateData::fromRequest($request));
$albums = $this->albumRepository->getMany($updatedSongs->pluck('album_id')->toArray());
Expand All @@ -76,7 +76,7 @@ public function update(SongUpdateRequest $request)
public function destroy(DeleteSongsRequest $request)
{
// Don't use SongRepository::findMany() because it'd be already catered to the current user.
Song::find($request->songs)->each(fn (Song $song) => $this->authorize('delete', $song));
Song::query()->findMany($request->songs)->each(fn (Song $song) => $this->authorize('delete', $song));

$this->songService->deleteSongs($request->songs);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

namespace App\Http\Controllers\API\Interaction;
namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Http\Requests\API\ToggleLikeSongRequest;
use App\Http\Resources\InteractionResource;
use App\Models\Song;
use App\Models\User;
use App\Repositories\SongRepository;
use App\Services\InteractionService;
Expand All @@ -19,9 +20,10 @@ public function __invoke(
InteractionService $interactionService,
?Authenticatable $user
) {
$song = $songRepository->getOne($request->song, $user);
/** @var Song $song */
$song = Song::query()->findOrFail($request->song);
$this->authorize('access', $song);

return InteractionResource::make($interactionService->toggleLike($request->song, $user));
return InteractionResource::make($interactionService->toggleLike($song, $user));
}
}
27 changes: 27 additions & 0 deletions app/Http/Controllers/API/UnlikeMultipleSongsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Http\Requests\API\InteractWithMultipleSongsRequest;
use App\Models\Song;
use App\Models\User;
use App\Services\InteractionService;
use Illuminate\Contracts\Auth\Authenticatable;

class UnlikeMultipleSongsController extends Controller
{
/** @param User $user */
public function __invoke(
InteractWithMultipleSongsRequest $request,
InteractionService $interactionService,
Authenticatable $user
) {
$songs = Song::query()->findMany($request->songs);
$songs->each(fn (Song $song) => $this->authorize('access', $song));

$interactionService->unlikeMany($songs, $user);

return response()->noContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* @property array<string> $songs
*/
class BatchInteractionRequest extends Request
class InteractWithMultipleSongsRequest extends Request
{
/** @return array<mixed> */
public function rules(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
namespace App\Http\Requests\API\Interaction;

use App\Http\Requests\API\Request;
use Illuminate\Validation\Rule;

/**
* @property string $song The song's ID
* @property-read string $song The song's ID
*/
class IncreasePlayCountRequest extends Request
{
/** @return array<mixed> */
public function rules(): array
{
return [
'song' => 'required',
'song' => ['required', Rule::exists('songs', 'id')],
];
}
}
7 changes: 7 additions & 0 deletions app/Http/Requests/API/ToggleLikeSongRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@
/** @property-read string $song */
class ToggleLikeSongRequest extends Request
{
/** @return array<mixed> */
public function rules(): array
{
return [
'song' => 'required|exists:songs,id',
];
}
}
4 changes: 2 additions & 2 deletions app/Listeners/LoveMultipleTracksOnLastfm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Listeners;

use App\Events\SongsBatchLiked;
use App\Events\MultipleSongsLiked;
use App\Services\LastfmService;
use Illuminate\Contracts\Queue\ShouldQueue;

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

public function handle(SongsBatchLiked $event): void
public function handle(MultipleSongsLiked $event): void
{
$this->lastfm->batchToggleLoveTracks($event->songs, $event->user, true);
}
Expand Down
4 changes: 2 additions & 2 deletions app/Listeners/UnloveMultipleTracksOnLastfm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Listeners;

use App\Events\SongsBatchUnliked;
use App\Events\MultipleSongsUnliked;
use App\Services\LastfmService;
use Illuminate\Contracts\Queue\ShouldQueue;

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

public function handle(SongsBatchUnliked $event): void
public function handle(MultipleSongsUnliked $event): void
{
$this->lastfm->batchToggleLoveTracks($event->songs, $event->user, false);
}
Expand Down
8 changes: 4 additions & 4 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\MediaScanCompleted;
use App\Events\MultipleSongsLiked;
use App\Events\MultipleSongsUnliked;
use App\Events\PlaybackStarted;
use App\Events\SongLikeToggled;
use App\Events\SongsBatchLiked;
use App\Events\SongsBatchUnliked;
use App\Listeners\DeleteNonExistingRecordsPostSync;
use App\Listeners\LoveMultipleTracksOnLastfm;
use App\Listeners\LoveTrackOnLastfm;
Expand All @@ -26,11 +26,11 @@ class EventServiceProvider extends BaseServiceProvider
LoveTrackOnLastfm::class,
],

SongsBatchLiked::class => [
MultipleSongsLiked::class => [
LoveMultipleTracksOnLastfm::class,
],

SongsBatchUnliked::class => [
MultipleSongsUnliked::class => [
UnloveMultipleTracksOnLastfm::class,
],

Expand Down
Loading

0 comments on commit 928a2eb

Please sign in to comment.