Skip to content

Commit

Permalink
Refactor to Post & User Resources
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedHelalAhmed committed Dec 31, 2021
1 parent 3475280 commit 810c3da
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 14 deletions.
15 changes: 2 additions & 13 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Http\Controllers;

use Illuminate\Http\Response;
use App\Http\Resources\Post as PostResource;

class PostController extends Controller
{
Expand All @@ -15,17 +15,6 @@ public function store()
]);

$post = request()->user()->posts()->create($data['data']['attributes']);
return response([
'data' => [
'type' => 'posts',
'post_id' => $post->id,
'attributes' => [
'body' => $post->body
],
],
'links' => [
'self' => url('/posts/' . $post->id)
]
], Response::HTTP_CREATED);
return new PostResource($post);
}
}
32 changes: 32 additions & 0 deletions app/Http/Resources/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Resources;

use App\Http\Resources\User as UserResource;
use Illuminate\Http\Resources\Json\JsonResource;

class Post extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => [
'type' => 'posts',
'post_id' => $this->id,
'attributes' => [
'posted_by' => new UserResource($this->user),
'body' => $this->body
],
],
'links' => [
'self' => url('/posts/' . $this->id)
]
];
}
}
30 changes: 30 additions & 0 deletions app/Http/Resources/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class User extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => [
'type' => 'users',
'user_id' => $this->id,
'attributes' => [
'name' => $this->name
],
],
'links' => [
'self' => url('/users/' . $this->id)
]
];;
}
}
5 changes: 5 additions & 0 deletions app/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
class Post extends Model
{
protected $guarded = [];// disable mass assignment

public function user()
{
return $this->belongsTo(User::class);
}
}
10 changes: 9 additions & 1 deletion tests/Feature/PostToTimelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ public function a_user_can_post_a_text_post()
'type' => 'posts',
'post_id' => $post->id,
'attributes' => [
'body' => $body
'posted_by' => [
'data' => [
'attributes' => [
'name' => $user->name
],
]
],
'body' => $body,

],
],
'links' => [
Expand Down

0 comments on commit 810c3da

Please sign in to comment.