mirror of
https://github.com/koel/koel
synced 2024-09-20 06:11:53 +00:00
feat: change some routes
This commit is contained in:
parent
334c53727e
commit
91f38084eb
13 changed files with 80 additions and 32 deletions
|
@ -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)
|
|
@ -25,8 +25,8 @@ class QueueController extends Controller
|
|||
$this->songRepository->getForQueue(
|
||||
$request->sort,
|
||||
$request->order,
|
||||
$this->user,
|
||||
$request->limit,
|
||||
$this->user,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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)),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()),
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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'))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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()) {
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
21
tests/Feature/V6/QueueTest.php
Normal file
21
tests/Feature/V6/QueueTest.php
Normal 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, '*');
|
||||
}
|
||||
}
|
7
tests/Feature/V6/RecentlyPlayedSongTest.php
Normal file
7
tests/Feature/V6/RecentlyPlayedSongTest.php
Normal file
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\V6;
|
||||
|
||||
class RecentlyPlayedSongTest extends TestCase
|
||||
{
|
||||
}
|
Loading…
Reference in a new issue