Skip to content

Commit

Permalink
Make controllers slim
Browse files Browse the repository at this point in the history
  • Loading branch information
phanan committed Aug 6, 2017
1 parent 7ec5cfb commit f827c63
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 77 deletions.
Expand Up @@ -5,7 +5,7 @@
use App\Models\Album;
use Illuminate\Http\JsonResponse;

class AlbumController extends Controller
class AlbumInfoController extends Controller
{
/**
* Get extra information about an album via Last.fm.
Expand All @@ -14,7 +14,7 @@ class AlbumController extends Controller
*
* @return JsonResponse
*/
public function getInfo(Album $album)
public function show(Album $album)
{
return response()->json($album->getInfo());
}
Expand Down
Expand Up @@ -2,10 +2,11 @@

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Models\Artist;
use Illuminate\Http\JsonResponse;

class ArtistController extends Controller
class ArtistInfoController extends Controller
{
/**
* Get extra information about an artist via Last.fm.
Expand All @@ -14,7 +15,7 @@ class ArtistController extends Controller
*
* @return JsonResponse
*/
public function getInfo(Artist $artist)
public function show(Artist $artist)
{
return response()->json($artist->getInfo());
}
Expand Down
34 changes: 34 additions & 0 deletions app/Http/Controllers/API/Interaction/BatchLikeController.php
@@ -0,0 +1,34 @@
<?php

namespace App\Http\Controllers\API\Interaction;

use App\Http\Requests\API\BatchInteractionRequest;
use App\Models\Interaction;
use Illuminate\Http\JsonResponse;

class BatchLikeController extends Controller
{
/**
* Like several songs at once as the currently authenticated user.
*
* @param BatchInteractionRequest $request
*
* @return JsonResponse
*/
function store(BatchInteractionRequest $request)
{
return response()->json(Interaction::batchLike((array) $request->songs, $request->user()));
}

/**
* Unlike several songs at once as the currently authenticated user.
*
* @param BatchInteractionRequest $request
*
* @return JsonResponse
*/
public function destroy(BatchInteractionRequest $request)
{
return response()->json(Interaction::batchUnlike((array) $request->songs, $request->user()));
}
}
9 changes: 9 additions & 0 deletions app/Http/Controllers/API/Interaction/Controller.php
@@ -0,0 +1,9 @@
<?php

namespace App\Http\Controllers\API\Interaction;

use App\Http\Controllers\Controller as BaseController;

class Controller extends BaseController
{
}
22 changes: 22 additions & 0 deletions app/Http/Controllers/API/Interaction/LikeController.php
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Controllers\API\Interaction;

use App\Models\Interaction;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class LikeController extends Controller
{
/**
* Like or unlike a song as the currently authenticated user.
*
* @param Request $request
*
* @return JsonResponse
*/
public function store(Request $request)
{
return response()->json(Interaction::toggleLike($request->song, $request->user()));
}
}
28 changes: 28 additions & 0 deletions app/Http/Controllers/API/Interaction/PlayCountController.php
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Controllers\API\Interaction;

use App\Events\SongStartedPlaying;
use App\Models\Interaction;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PlayCountController extends Controller
{
/**
* Increase a song's play count as the currently authenticated user.
*
* @param Request $request
*
* @return JsonResponse
*/
public function store(Request $request)
{
$interaction = Interaction::increasePlayCount($request->song, $request->user());
if ($interaction) {
event(new SongStartedPlaying($interaction->song, $interaction->user));
}

return response()->json($interaction);
}
}
65 changes: 0 additions & 65 deletions app/Http/Controllers/API/InteractionController.php

This file was deleted.

12 changes: 6 additions & 6 deletions routes/api.php
Expand Up @@ -21,10 +21,10 @@
Route::put('songs', 'SongController@update');

// Interaction routes
Route::post('interaction/play', 'InteractionController@play');
Route::post('interaction/like', 'InteractionController@like');
Route::post('interaction/batch/like', 'InteractionController@batchLike');
Route::post('interaction/batch/unlike', 'InteractionController@batchUnlike');
Route::post('interaction/play', 'Interaction\PlayCountController@store');
Route::post('interaction/like', 'Interaction\LikeController@store');
Route::post('interaction/batch/like', 'Interaction\BatchLikeController@store');
Route::post('interaction/batch/unlike', 'Interaction\BatchLikeController@destroy');

// Playlist routes
Route::resource('playlist', 'PlaylistController');
Expand Down Expand Up @@ -59,8 +59,8 @@

// Info routes
if (Lastfm::used()) {
Route::get('album/{album}/info', 'AlbumController@getInfo');
Route::get('artist/{artist}/info', 'ArtistController@getInfo');
Route::get('album/{album}/info', 'AlbumInfoController@show');
Route::get('artist/{artist}/info', 'ArtistInfoController@show');
}

// iTunes routes
Expand Down
5 changes: 3 additions & 2 deletions tests/Integration/SongTest.php
Expand Up @@ -62,6 +62,8 @@ public function it_scrobbles_if_the_user_is_connected_to_lastfm()
/** @test */
public function it_does_not_scrobble_if_the_user_is_not_connected_to_lastfm()
{
Lastfm::shouldNotReceive('scrobble');

// Given there's a song
/** @var Song $song */
$song = factory(Song::class)->create();
Expand All @@ -72,11 +74,10 @@ public function it_does_not_scrobble_if_the_user_is_not_connected_to_lastfm()
$user->setPreference('lastfm_session_key', false);

// When I call the scrobble method
Lastfm::shouldNotReceive('scrobble');

$song->scrobble($user, time());

// The the song shouldn't be scrobbled
// Then the song shouldn't be scrobbled
}

/** @test */
Expand Down

0 comments on commit f827c63

Please sign in to comment.