diff --git a/app/Http/Controllers/PostController.php b/app/Http/Controllers/PostController.php index 9c4dcbd..87e7168 100644 --- a/app/Http/Controllers/PostController.php +++ b/app/Http/Controllers/PostController.php @@ -4,13 +4,12 @@ use App\Http\Resources\Post as PostResource; use App\Http\Resources\PostCollection; -use App\Post; class PostController extends Controller { public function index() { - return new PostCollection(Post::all()); + return new PostCollection(request()->user()->posts); } public function store() diff --git a/app/Post.php b/app/Post.php index 5728387..5ce3dd1 100644 --- a/app/Post.php +++ b/app/Post.php @@ -2,12 +2,20 @@ namespace App; +use App\Scopes\ReverseScope; use Illuminate\Database\Eloquent\Model; class Post extends Model { protected $guarded = [];// disable mass assignment + protected static function boot() + { + parent::boot(); + + static::addGlobalScope(new ReverseScope()); + } + public function user() { return $this->belongsTo(User::class); diff --git a/app/Scopes/ReverseScope.php b/app/Scopes/ReverseScope.php new file mode 100644 index 0000000..e007687 --- /dev/null +++ b/app/Scopes/ReverseScope.php @@ -0,0 +1,15 @@ +orderBy('id', 'desc'); + } +} diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php deleted file mode 100644 index cdb5111..0000000 --- a/tests/Feature/ExampleTest.php +++ /dev/null @@ -1,21 +0,0 @@ -get('/'); - - $response->assertStatus(200); - } -} diff --git a/tests/Feature/RetrievePostsTest.php b/tests/Feature/RetrievePostsTest.php index 175508d..a74db14 100644 --- a/tests/Feature/RetrievePostsTest.php +++ b/tests/Feature/RetrievePostsTest.php @@ -17,7 +17,9 @@ public function a_user_can_retrieve_posts() { $this->withoutExceptionHandling(); $this->actingAs($user = factory(User::class)->create(), 'api'); - $posts = factory(Post::class, 2)->create(); + $posts = factory(Post::class, 2)->create([ + 'user_id' => $user->id + ]); $response = $this->get('/api/posts'); @@ -28,9 +30,9 @@ public function a_user_can_retrieve_posts() 'data' => [ 'type' => 'posts', - 'post_id' => $posts->first()->id, + 'post_id' => $posts->last()->id, 'attributes' => [ - 'body' => $posts->first()->body + 'body' => $posts->last()->body ] ] ], @@ -38,13 +40,12 @@ public function a_user_can_retrieve_posts() 'data' => [ 'type' => 'posts', - 'post_id' => $posts->last()->id, + 'post_id' => $posts->first()->id, 'attributes' => [ - 'body' => $posts->last()->body + 'body' => $posts->first()->body ] ] ] - ], 'links' => [ 'self' => url('/posts') @@ -52,4 +53,21 @@ public function a_user_can_retrieve_posts() ]); } + + /** @test */ + public function a_user_can_only_retrieve_their_posts() + { + $this->actingAs($user = factory(User::class)->create(), 'api'); + $posts = factory(Post::class)->create(); + $response = $this->get('/api/posts'); + $response->assertStatus(Response::HTTP_OK) + ->assertExactJson( + [ + 'data' => [], + 'links' => [ + 'self' => url('/posts') + ] + ] + ); + } } diff --git a/tests/Unit/ExampleTest.php b/tests/Unit/ExampleTest.php deleted file mode 100644 index 358cfc8..0000000 --- a/tests/Unit/ExampleTest.php +++ /dev/null @@ -1,18 +0,0 @@ -assertTrue(true); - } -}