mirror of
https://github.com/koel/koel
synced 2024-11-10 06:34:14 +00:00
Refactor
This commit is contained in:
parent
2ca5047a9d
commit
aba3740567
11 changed files with 159 additions and 113 deletions
54
app/Http/Controllers/API/AuthController.php
Normal file
54
app/Http/Controllers/API/AuthController.php
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\API;
|
||||||
|
|
||||||
|
use App\Http\Requests\API\UserLoginRequest;
|
||||||
|
use App\Models\User;
|
||||||
|
use Exception;
|
||||||
|
use Hash;
|
||||||
|
use JWTAuth;
|
||||||
|
use Log;
|
||||||
|
use Tymon\JWTAuth\Exceptions\JWTException;
|
||||||
|
|
||||||
|
class AuthController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Log a user in.
|
||||||
|
*
|
||||||
|
* @param UserLoginRequest $request
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
public function login(UserLoginRequest $request)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if (!$token = JWTAuth::attempt($request->only('email', 'password'))) {
|
||||||
|
return response()->json(['error' => 'invalid_credentials'], 401);
|
||||||
|
}
|
||||||
|
} catch (JWTException $e) {
|
||||||
|
Log:error($e);
|
||||||
|
|
||||||
|
return response()->json(['error' => 'could_not_create_token'], 500);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json(compact('token'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Log the current user out.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
public function logout()
|
||||||
|
{
|
||||||
|
if ($token = JWTAuth::getToken()) {
|
||||||
|
try {
|
||||||
|
JWTAuth::invalidate($token);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
Log::error($e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json();
|
||||||
|
}
|
||||||
|
}
|
28
app/Http/Controllers/API/ProfileController.php
Normal file
28
app/Http/Controllers/API/ProfileController.php
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\API;
|
||||||
|
|
||||||
|
use App\Http\Requests\API\ProfileUpdateRequest;
|
||||||
|
use App\Models\User;
|
||||||
|
use Hash;
|
||||||
|
|
||||||
|
class ProfileController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Update the current user's profile.
|
||||||
|
*
|
||||||
|
* @param ProfileUpdateRequest $request
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
public function update(ProfileUpdateRequest $request)
|
||||||
|
{
|
||||||
|
$data = $request->only('name', 'email');
|
||||||
|
|
||||||
|
if ($password = $request->input('password')) {
|
||||||
|
$data['password'] = Hash::make($password);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json(auth()->user()->update($data));
|
||||||
|
}
|
||||||
|
}
|
21
app/Http/Controllers/API/ScrobbleController.php
Normal file
21
app/Http/Controllers/API/ScrobbleController.php
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\API;
|
||||||
|
|
||||||
|
use App\Models\Song;
|
||||||
|
|
||||||
|
class ScrobbleController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create a Last.fm scrobble entry for a song.
|
||||||
|
*
|
||||||
|
* @param Song $song
|
||||||
|
* @param string $timestamp The UNIX timestamp when the song started playing.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
public function store(Song $song, $timestamp)
|
||||||
|
{
|
||||||
|
return response()->json($song->scrobble($timestamp));
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,7 +15,7 @@ class SettingController extends Controller
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Http\JsonResponse
|
* @return \Illuminate\Http\JsonResponse
|
||||||
*/
|
*/
|
||||||
public function save(SettingRequest $request)
|
public function store(SettingRequest $request)
|
||||||
{
|
{
|
||||||
// For right now there's only one setting to be saved
|
// For right now there's only one setting to be saved
|
||||||
Setting::set('media_path', rtrim(trim($request->input('media_path')), '/'));
|
Setting::set('media_path', rtrim(trim($request->input('media_path')), '/'));
|
||||||
|
|
|
@ -41,7 +41,7 @@ class SongController extends Controller
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Http\JsonResponse
|
* @return \Illuminate\Http\JsonResponse
|
||||||
*/
|
*/
|
||||||
public function getInfo(Song $song)
|
public function show(Song $song)
|
||||||
{
|
{
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'lyrics' => $song->lyrics,
|
'lyrics' => $song->lyrics,
|
||||||
|
@ -50,19 +50,6 @@ class SongController extends Controller
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Scrobble a song.
|
|
||||||
*
|
|
||||||
* @param Song $song
|
|
||||||
* @param string $timestamp The UNIX timestamp when the song started playing.
|
|
||||||
*
|
|
||||||
* @return \Illuminate\Http\JsonResponse
|
|
||||||
*/
|
|
||||||
public function scrobble(Song $song, $timestamp)
|
|
||||||
{
|
|
||||||
return response()->json($song->scrobble($timestamp));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update songs info.
|
* Update songs info.
|
||||||
*
|
*
|
||||||
|
|
|
@ -2,59 +2,13 @@
|
||||||
|
|
||||||
namespace App\Http\Controllers\API;
|
namespace App\Http\Controllers\API;
|
||||||
|
|
||||||
use App\Http\Requests\API\ProfileUpdateRequest;
|
|
||||||
use App\Http\Requests\API\UserLoginRequest;
|
|
||||||
use App\Http\Requests\API\UserStoreRequest;
|
use App\Http\Requests\API\UserStoreRequest;
|
||||||
use App\Http\Requests\API\UserUpdateRequest;
|
use App\Http\Requests\API\UserUpdateRequest;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Exception;
|
|
||||||
use Hash;
|
use Hash;
|
||||||
use JWTAuth;
|
|
||||||
use Log;
|
|
||||||
use Tymon\JWTAuth\Exceptions\JWTException;
|
|
||||||
|
|
||||||
class UserController extends Controller
|
class UserController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Log a user in.
|
|
||||||
*
|
|
||||||
* @param UserLoginRequest $request
|
|
||||||
*
|
|
||||||
* @return \Illuminate\Http\JsonResponse
|
|
||||||
*/
|
|
||||||
public function login(UserLoginRequest $request)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
if (!$token = JWTAuth::attempt($request->only('email', 'password'))) {
|
|
||||||
return response()->json(['error' => 'invalid_credentials'], 401);
|
|
||||||
}
|
|
||||||
} catch (JWTException $e) {
|
|
||||||
Log:error($e);
|
|
||||||
|
|
||||||
return response()->json(['error' => 'could_not_create_token'], 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response()->json(compact('token'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Log the current user out.
|
|
||||||
*
|
|
||||||
* @return \Illuminate\Http\JsonResponse
|
|
||||||
*/
|
|
||||||
public function logout()
|
|
||||||
{
|
|
||||||
if ($token = JWTAuth::getToken()) {
|
|
||||||
try {
|
|
||||||
JWTAuth::invalidate($token);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
Log::error($e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return response()->json();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new user.
|
* Create a new user.
|
||||||
*
|
*
|
||||||
|
@ -103,22 +57,4 @@ class UserController extends Controller
|
||||||
|
|
||||||
return response()->json($user->delete());
|
return response()->json($user->delete());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Update the current user's profile.
|
|
||||||
*
|
|
||||||
* @param ProfileUpdateRequest $request
|
|
||||||
*
|
|
||||||
* @return \Illuminate\Http\JsonResponse
|
|
||||||
*/
|
|
||||||
public function updateProfile(ProfileUpdateRequest $request)
|
|
||||||
{
|
|
||||||
$data = $request->only('name', 'email');
|
|
||||||
|
|
||||||
if ($password = $request->input('password')) {
|
|
||||||
$data['password'] = Hash::make($password);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response()->json(auth()->user()->update($data));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,8 @@ Route::get('/♫', function () {
|
||||||
|
|
||||||
Route::group(['prefix' => 'api', 'namespace' => 'API'], function () {
|
Route::group(['prefix' => 'api', 'namespace' => 'API'], function () {
|
||||||
|
|
||||||
Route::post('me', 'UserController@login');
|
Route::post('me', 'AuthController@login');
|
||||||
Route::delete('me', 'UserController@logout');
|
Route::delete('me', 'AuthController@logout');
|
||||||
|
|
||||||
Route::group(['middleware' => 'jwt.auth'], function () {
|
Route::group(['middleware' => 'jwt.auth'], function () {
|
||||||
Route::get('/', function () {
|
Route::get('/', function () {
|
||||||
|
@ -21,14 +21,13 @@ Route::group(['prefix' => 'api', 'namespace' => 'API'], function () {
|
||||||
|
|
||||||
Route::get('data', 'DataController@index');
|
Route::get('data', 'DataController@index');
|
||||||
|
|
||||||
Route::post('settings', 'SettingController@save');
|
Route::post('settings', 'SettingController@store');
|
||||||
|
|
||||||
Route::get('{song}/play', 'SongController@play');
|
Route::get('{song}/play', 'SongController@play');
|
||||||
Route::post('{song}/scrobble/{timestamp}', 'SongController@scrobble')->where([
|
Route::post('{song}/scrobble/{timestamp}', 'ScrobbleController@store')->where([
|
||||||
'timestamp' => '\d+',
|
'timestamp' => '\d+',
|
||||||
]);
|
]);
|
||||||
Route::get('{song}/info', 'SongController@getInfo');
|
Route::get('{song}/info', 'SongController@show');
|
||||||
|
|
||||||
Route::put('songs', 'SongController@update');
|
Route::put('songs', 'SongController@update');
|
||||||
|
|
||||||
Route::post('interaction/play', 'InteractionController@play');
|
Route::post('interaction/play', 'InteractionController@play');
|
||||||
|
@ -40,7 +39,7 @@ Route::group(['prefix' => 'api', 'namespace' => 'API'], function () {
|
||||||
Route::put('playlist/{playlist}/sync', 'PlaylistController@sync')->where(['playlist' => '\d+']);
|
Route::put('playlist/{playlist}/sync', 'PlaylistController@sync')->where(['playlist' => '\d+']);
|
||||||
|
|
||||||
Route::resource('user', 'UserController', ['only' => ['store', 'update', 'destroy']]);
|
Route::resource('user', 'UserController', ['only' => ['store', 'update', 'destroy']]);
|
||||||
Route::put('me', 'UserController@updateProfile');
|
Route::put('me', 'ProfileController@update');
|
||||||
|
|
||||||
Route::get('lastfm/connect', 'LastfmController@connect');
|
Route::get('lastfm/connect', 'LastfmController@connect');
|
||||||
Route::post('lastfm/session-key', 'LastfmController@setSessionKey');
|
Route::post('lastfm/session-key', 'LastfmController@setSessionKey');
|
||||||
|
|
|
@ -203,21 +203,4 @@ class LastfmTest extends TestCase
|
||||||
|
|
||||||
(new UpdateLastfmNowPlaying($lastfm))->handle(new SongStartedPlaying($song, $user));
|
(new UpdateLastfmNowPlaying($lastfm))->handle(new SongStartedPlaying($song, $user));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testScrobble()
|
|
||||||
{
|
|
||||||
$this->withoutEvents();
|
|
||||||
$this->createSampleMediaSet();
|
|
||||||
|
|
||||||
$user = factory(User::class)->create(['preferences' => ['lastfm_session_key' => 'bar']]);
|
|
||||||
$song = Song::first();
|
|
||||||
|
|
||||||
$ts = time();
|
|
||||||
|
|
||||||
$lastfm = m::mock(Lastfm::class, ['enabled' => true]);
|
|
||||||
$lastfm->shouldReceive('scrobble')
|
|
||||||
->with($song->album->artist->name, $song->title, $ts, $song->album->name, 'bar');
|
|
||||||
|
|
||||||
$this->post("/api/{$song->id}/scrobble/$ts");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
18
tests/ProfileTest.php
Normal file
18
tests/ProfileTest.php
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
||||||
|
|
||||||
|
class ProfileTest extends TestCase
|
||||||
|
{
|
||||||
|
use WithoutMiddleware, DatabaseTransactions;
|
||||||
|
|
||||||
|
public function testUpdate()
|
||||||
|
{
|
||||||
|
$this->actingAs(factory(User::class)->create())
|
||||||
|
->put('api/me', ['name' => 'Foo', 'email' => 'bar@baz.com']);
|
||||||
|
|
||||||
|
$this->seeInDatabase('users', ['name' => 'Foo', 'email' => 'bar@baz.com']);
|
||||||
|
}
|
||||||
|
}
|
30
tests/ScrobbleTest.php
Normal file
30
tests/ScrobbleTest.php
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\Song;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Services\Lastfm;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
||||||
|
use Mockery as m;
|
||||||
|
|
||||||
|
class ScrobbleTest extends TestCase
|
||||||
|
{
|
||||||
|
use DatabaseTransactions, WithoutMiddleware;
|
||||||
|
|
||||||
|
public function testScrobble()
|
||||||
|
{
|
||||||
|
$this->withoutEvents();
|
||||||
|
$this->createSampleMediaSet();
|
||||||
|
|
||||||
|
$user = factory(User::class)->create(['preferences' => ['lastfm_session_key' => 'bar']]);
|
||||||
|
$song = Song::first();
|
||||||
|
|
||||||
|
$ts = time();
|
||||||
|
|
||||||
|
$lastfm = m::mock(Lastfm::class, ['enabled' => true]);
|
||||||
|
$lastfm->shouldReceive('scrobble')
|
||||||
|
->with($song->album->artist->name, $song->title, $ts, $song->album->name, 'bar');
|
||||||
|
|
||||||
|
$this->post("/api/{$song->id}/scrobble/$ts");
|
||||||
|
}
|
||||||
|
}
|
|
@ -30,16 +30,6 @@ class UserTest extends TestCase
|
||||||
$this->seeInDatabase('users', ['name' => 'Foo']);
|
$this->seeInDatabase('users', ['name' => 'Foo']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUpdateProfile()
|
|
||||||
{
|
|
||||||
$user = factory(User::class)->create();
|
|
||||||
|
|
||||||
$this->actingAs($user)
|
|
||||||
->put('api/me', ['name' => 'Foo', 'email' => 'bar@baz.com']);
|
|
||||||
|
|
||||||
$this->seeInDatabase('users', ['name' => 'Foo', 'email' => 'bar@baz.com']);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testUpdateUser()
|
public function testUpdateUser()
|
||||||
{
|
{
|
||||||
$user = factory(User::class)->create();
|
$user = factory(User::class)->create();
|
||||||
|
|
Loading…
Reference in a new issue