feat: change some routes

This commit is contained in:
Phan An 2022-07-27 11:30:04 +02:00
parent 334c53727e
commit 91f38084eb
No known key found for this signature in database
GPG key ID: A81E4477F0BB6FDC
13 changed files with 80 additions and 32 deletions

View file

@ -8,7 +8,7 @@ use App\Models\User;
use App\Repositories\SongRepository;
use Illuminate\Contracts\Auth\Authenticatable;
class FavoriteController extends Controller
class FavoriteSongController extends Controller
{
/** @param User $user */
public function __construct(private SongRepository $songRepository, private ?Authenticatable $user)

View file

@ -25,8 +25,8 @@ class QueueController extends Controller
$this->songRepository->getForQueue(
$request->sort,
$request->order,
$this->user,
$request->limit,
$this->user,
)
);
}

View file

@ -3,6 +3,7 @@
namespace App\Http\Controllers\V6\Requests;
use App\Http\Requests\API\Request;
use App\Repositories\SongRepository;
use Illuminate\Validation\Rule;
/**
@ -18,7 +19,10 @@ class QueueFetchSongRequest extends Request
return [
'order' => ['required', Rule::in('asc', 'desc', 'rand')],
'limit' => 'required|integer|min:1',
'sort' => 'required_unless:order,rand',
'sort' => [
'required_unless:order,rand',
Rule::in(array_keys(SongRepository::SORT_COLUMNS_NORMALIZE_MAP)),
],
];
}
}

View file

@ -18,7 +18,7 @@ class SongRepository extends AbstractRepository
{
use Searchable;
private const SORT_COLUMNS_NORMALIZE_MAP = [
public const SORT_COLUMNS_NORMALIZE_MAP = [
'title' => 'songs.title',
'track' => 'songs.track',
'length' => 'songs.length',
@ -89,8 +89,8 @@ class SongRepository extends AbstractRepository
public function getForQueue(
string $sortColumn,
string $sortDirection,
int $limit = self::DEFAULT_QUEUE_LIMIT,
?User $scopedUser = null,
int $limit = self::DEFAULT_QUEUE_LIMIT
): Collection {
return self::applySort(
Song::withMeta($scopedUser ?? $this->auth->user()),

View file

@ -73,7 +73,7 @@ new class extends UnitTestCase {
await favoriteStore.fetch()
expect(getMock).toHaveBeenCalledWith(`favorites`)
expect(getMock).toHaveBeenCalledWith('songs/favorite')
expect(favoriteStore.state.songs).toEqual(songs)
})
}

View file

@ -43,6 +43,6 @@ export const favoriteStore = {
},
async fetch () {
this.state.songs = songStore.syncWithVault(await httpService.get<Song[]>('favorites'))
this.state.songs = songStore.syncWithVault(await httpService.get<Song[]>('songs/favorite'))
}
}

View file

@ -14,7 +14,7 @@ new class extends UnitTestCase {
await recentlyPlayedStore.fetch()
expect(getMock).toHaveBeenCalledWith('recently-played')
expect(getMock).toHaveBeenCalledWith('songs/recently-played')
expect(syncMock).toHaveBeenCalledWith(songs)
expect(recentlyPlayedStore.state.songs).toEqual(songs)
})

View file

@ -15,7 +15,7 @@ export const recentlyPlayedStore = {
}),
async fetch () {
this.state.songs = songStore.syncWithVault(await httpService.get<Song[]>('recently-played'))
this.state.songs = songStore.syncWithVault(await httpService.get<Song[]>('songs/recently-played'))
},
async add (song: Song) {

View file

@ -9,7 +9,7 @@ use App\Http\Controllers\V6\API\ArtistController;
use App\Http\Controllers\V6\API\ArtistSongController;
use App\Http\Controllers\V6\API\DataController;
use App\Http\Controllers\V6\API\ExcerptSearchController;
use App\Http\Controllers\V6\API\FavoriteController;
use App\Http\Controllers\V6\API\FavoriteSongController;
use App\Http\Controllers\V6\API\FetchAlbumInformationController;
use App\Http\Controllers\V6\API\FetchArtistInformationController;
use App\Http\Controllers\V6\API\OverviewController;
@ -40,16 +40,15 @@ Route::prefix('api')->middleware('api')->group(static function (): void {
Route::post('playlists/{playlist}/songs', [PlaylistSongController::class, 'add']);
Route::delete('playlists/{playlist}/songs', [PlaylistSongController::class, 'remove']);
Route::apiResource('songs', SongController::class);
Route::apiResource('songs', SongController::class)->where(['song' => '[a-f0-9]{32}']);
Route::get('songs/recently-played', [RecentlyPlayedSongController::class, 'index']);
Route::get('songs/favorite', [FavoriteSongController::class, 'index']);
Route::apiResource('users', UserController::class);
Route::get('search', ExcerptSearchController::class);
Route::get('search/songs', SongSearchController::class);
Route::get('favorites', [FavoriteController::class, 'index']);
Route::get('recently-played', [RecentlyPlayedSongController::class, 'index']);
Route::get('queue/fetch', [QueueController::class, 'fetchSongs']);
if (YouTube::enabled()) {

View file

@ -5,18 +5,16 @@ namespace Tests\Feature\V6;
use App\Models\Interaction;
use App\Models\User;
class FavoriteTest extends TestCase
class FavoriteSongTest extends TestCase
{
public function testIndex(): void
{
/** @var User $user */
$user = User::factory()->create();
Interaction::factory(5)->create([
'user_id' => $user->id,
]);
Interaction::factory(5)->for($user)->create(['liked' => true]);
$this->getAs('api/favorites', $user)
$this->getAs('api/songs/favorite', $user)
->assertJsonStructure(['*' => SongTest::JSON_STRUCTURE]);
}
}

View file

@ -3,28 +3,47 @@
namespace Tests\Feature\V6;
use App\Models\Interaction;
use App\Models\Song;
use App\Models\User;
class PlayCountTest extends TestCase
{
public function testStore(): void
public function testStoreExistingEntry(): void
{
/** @var Interaction $interaction */
$interaction = Interaction::factory()->create([
'play_count' => 10,
]);
$response = $this->postAs('/api/interaction/play', [
'song' => $interaction->song->id,
], $interaction->user);
$response->assertJsonStructure([
'type',
'id',
'song_id',
'liked',
'play_count',
]);
$this->postAs('/api/interaction/play', ['song' => $interaction->song->id], $interaction->user)
->assertJsonStructure([
'type',
'id',
'song_id',
'liked',
'play_count',
]);
self::assertEquals(11, $interaction->refresh()->play_count);
}
public function testStoreNewEntry(): void
{
/** @var Song $song */
$song = Song::factory()->create();
/** @var User $user */
$user = User::factory()->create();
$this->postAs('/api/interaction/play', ['song' => $song->id], $user)
->assertJsonStructure([
'type',
'id',
'song_id',
'liked',
'play_count',
]);
self::assertEquals(1, Interaction::whereSongIdAndUserId($song->id, $user->id)->first()->play_count);
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace Tests\Feature\V6;
use App\Models\Song;
class QueueTest extends TestCase
{
public function testFetchSongs(): void
{
Song::factory(10)->create();
$this->getAs('api/queue/fetch?order=rand&limit=5')
->assertJsonStructure(['*' => SongTest::JSON_STRUCTURE])
->assertJsonCount(5, '*');
$this->getAs('api/queue/fetch?order=asc&sort=title&limit=5')
->assertJsonStructure(['*' => SongTest::JSON_STRUCTURE])
->assertJsonCount(5, '*');
}
}

View file

@ -0,0 +1,7 @@
<?php
namespace Tests\Feature\V6;
class RecentlyPlayedSongTest extends TestCase
{
}