Several methods for API

This commit is contained in:
An Phan 2016-01-26 14:32:29 +08:00
parent 278fb7c8bf
commit 8453c8e30b
8 changed files with 46 additions and 12 deletions

View file

@ -18,7 +18,7 @@ class InteractionController extends Controller
*/
public function play(Request $request)
{
if ($interaction = Interaction::increasePlayCount($request->input('id'), $request->user())) {
if ($interaction = Interaction::increasePlayCount($request->input('song'), $request->user())) {
event(new SongStartedPlaying($interaction->song, $interaction->user));
}
@ -34,7 +34,7 @@ class InteractionController extends Controller
*/
public function like(Request $request)
{
return response()->json(Interaction::toggleLike($request->input('id'), $request->user()));
return response()->json(Interaction::toggleLike($request->input('song'), $request->user()));
}
/**
@ -46,7 +46,7 @@ class InteractionController extends Controller
*/
public function batchLike(BatchInteractionRequest $request)
{
return response()->json(Interaction::batchLike((array) $request->input('ids'), $request->user()));
return response()->json(Interaction::batchLike((array) $request->input('songs'), $request->user()));
}
/**
@ -58,6 +58,6 @@ class InteractionController extends Controller
*/
public function batchUnlike(BatchInteractionRequest $request)
{
return response()->json(Interaction::batchUnlike((array) $request->input('ids'), $request->user()));
return response()->json(Interaction::batchUnlike((array) $request->input('songs'), $request->user()));
}
}

View file

@ -80,6 +80,16 @@ class LastfmController extends Controller
return view('api.lastfm.callback');
}
/**
* Set the Last.fm session key of the current user.
*/
public function setSessionKey(Request $request)
{
$this->auth->user()->savePreference('lastfm_session_key', $request->input('key'));
return response()->json();
}
/**
* Disconnect the current user from Last.fm.
*

View file

@ -8,6 +8,16 @@ use Illuminate\Http\Request;
class PlaylistController extends Controller
{
/**
* Gets all playlists by the current user.
*
* @return \Illuminate\Http\JsonResponse
*/
public function index()
{
return response()->json(Playlist::byCurrentUser()->orderBy('name')->with('songs')->get());
}
/**
* Create a new playlist.
*

View file

@ -22,7 +22,7 @@ class BatchInteractionRequest extends Request
public function rules()
{
return [
'ids' => 'required|array',
'songs' => 'required|array',
];
}
}

View file

@ -40,7 +40,8 @@ Route::group(['prefix' => 'api', 'namespace' => 'API'], function () {
Route::put('me', 'UserController@updateProfile');
Route::delete('me', 'UserController@logout');
Route::get('lastfm/connect', 'LastfmController@connect');
Route::post('lastfm/session-key', 'LastfmController@setSessionKey');
Route::get('lastfm/callback', [
'as' => 'lastfm.callback',
'uses' => 'LastfmController@callback',

View file

@ -25,7 +25,7 @@ class InteractionTest extends TestCase
$song = Song::orderBy('id')->first();
$this->actingAs($user)
->post('api/interaction/play', ['id' => $song->id]);
->post('api/interaction/play', ['song' => $song->id]);
$this->seeInDatabase('interactions', [
'user_id' => $user->id,
@ -35,7 +35,7 @@ class InteractionTest extends TestCase
// Try again
$this->actingAs($user)
->post('api/interaction/play', ['id' => $song->id]);
->post('api/interaction/play', ['song' => $song->id]);
$this->seeInDatabase('interactions', [
'user_id' => $user->id,
@ -52,7 +52,7 @@ class InteractionTest extends TestCase
$song = Song::orderBy('id')->first();
$this->actingAs($user)
->post('api/interaction/like', ['id' => $song->id]);
->post('api/interaction/like', ['song' => $song->id]);
$this->seeInDatabase('interactions', [
'user_id' => $user->id,
@ -62,7 +62,7 @@ class InteractionTest extends TestCase
// Try again
$this->actingAs($user)
->post('api/interaction/like', ['id' => $song->id]);
->post('api/interaction/like', ['song' => $song->id]);
$this->seeInDatabase('interactions', [
'user_id' => $user->id,
@ -81,7 +81,7 @@ class InteractionTest extends TestCase
$songIds = array_pluck($songs->toArray(), 'id');
$this->actingAs($user)
->post('api/interaction/batch/like', ['ids' => $songIds]);
->post('api/interaction/batch/like', ['songs' => $songIds]);
foreach ($songs as $song) {
$this->seeInDatabase('interactions', [
@ -92,7 +92,7 @@ class InteractionTest extends TestCase
}
$this->actingAs($user)
->post('api/interaction/batch/unlike', ['ids' => $songIds]);
->post('api/interaction/batch/unlike', ['songs' => $songIds]);
foreach ($songs as $song) {
$this->seeInDatabase('interactions', [

View file

@ -129,6 +129,13 @@ class LastfmTest extends TestCase
$this->assertEquals('foo', $api->getSessionKey('bar'));
}
public function testSetSessionKey()
{
$user = factory(User::class)->create();
$this->actingAs($user)->post('api/lastfm/session-key', ['key' => 'foo']);
$this->assertEquals('foo', $user->getLastfmSessionKey());
}
public function testControllerConnect()
{
$redirector = m::mock(Redirector::class);

View file

@ -44,6 +44,12 @@ class PlaylistTest extends TestCase
'song_id' => $song->id,
]);
}
$this->actingAs($user)->get('api/playlist')
->seeJson([
'id' => $playlist->id,
'name' => 'Foo Bar',
]);
}
public function testUpdatePlaylistName()