Skip to content

Commit

Permalink
Retrieving Posts Using a Post Collection Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedHelalAhmed committed Jan 1, 2022
1 parent 810c3da commit a4e4572
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
namespace App\Http\Controllers;

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());
}

public function store()
{

Expand Down
24 changes: 24 additions & 0 deletions app/Http/Resources/PostCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class PostCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => $this->collection,
'links' => [
'self' => url('/posts')
]
];
}
}
15 changes: 15 additions & 0 deletions database/factories/PostFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/** @var Factory $factory */

use App\Post;
use App\User;
use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Factory;

$factory->define(Post::class, function (Faker $faker) {
return [
'user_id' => factory(User::class),
'body' => $faker->sentence
];
});
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
return $request->user();
});

Route::get('/posts', 'PostController@index');

Route::post('/posts', 'PostController@store');
});
Expand Down
55 changes: 55 additions & 0 deletions tests/Feature/RetrievePostsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Tests\Feature;

use App\Post;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Response;
use Tests\TestCase;

class RetrievePostsTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function a_user_can_retrieve_posts()
{
$this->withoutExceptionHandling();
$this->actingAs($user = factory(User::class)->create(), 'api');
$posts = factory(Post::class, 2)->create();


$response = $this->get('/api/posts');
$response->assertStatus(Response::HTTP_OK)
->assertJson([
'data' => [
[
'data' =>
[
'type' => 'posts',
'post_id' => $posts->first()->id,
'attributes' => [
'body' => $posts->first()->body
]
]
],
[
'data' =>
[
'type' => 'posts',
'post_id' => $posts->last()->id,
'attributes' => [
'body' => $posts->last()->body
]
]
]

],
'links' => [
'self' => url('/posts')
],
]);

}
}

0 comments on commit a4e4572

Please sign in to comment.