Skip to content

Commit

Permalink
Using ForumReplyResource to display user specific data
Browse files Browse the repository at this point in the history
  • Loading branch information
musa11971 committed Sep 10, 2019
1 parent 78058a4 commit 36f6be6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
14 changes: 1 addition & 13 deletions app/Http/Controllers/ForumThreadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,23 +158,11 @@ public function replies(Request $request, ForumThread $thread) {
// Paginate the replies
$replies = $replies->paginate(ForumThread::REPLIES_PER_PAGE);

// Instantiate a CollectionLikeChecker to get the current liked status for the user
$likeChecker = CollectionLikeChecker::retrieve($request->user()->id, $replies);

// Format the replies
$formattedReplies = [];

foreach($replies as $reply) {
$formattedReplies[] = array_merge(ForumReplyResource::make($reply)->toArray($request), [
'current_like_action' => $likeChecker->getCurrentLikeAction($reply)
]);
}

// Show successful response
return JSONResult::success([
'page' => $givenPage,
'reply_pages' => $thread->getPageCount(),
'replies' => $formattedReplies
'replies' => ForumReplyResource::collection($replies)
]);
}

Expand Down
23 changes: 22 additions & 1 deletion app/Http/Resources/ForumReplyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Auth;

class ForumReplyResource extends JsonResource
{
Expand All @@ -14,7 +15,7 @@ class ForumReplyResource extends JsonResource
*/
public function toArray($request)
{
return [
$resource = [
'id' => $this->id,
'posted_at' => $this->created_at->format('Y-m-d H:i:s'),
'poster' => [
Expand All @@ -25,5 +26,25 @@ public function toArray($request)
'score' => $this->likesDiffDislikesCount,
'content' => $this->content
];

if(Auth::check())
$resource = array_merge($resource, $this->getUserSpecificDetails());

return $resource;
}

/**
* Returns the user specific details for the resource.
*
* @return array
*/
protected function getUserSpecificDetails() {
$user = Auth::user();

return [
'current_user' => [
'like_action' => $user->likeAction($this->resource)
]
];
}
}
3 changes: 2 additions & 1 deletion public/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1471,14 +1471,15 @@
"/forum-threads/{threadID}/replies": {
"get": {
"security": [
{},
{
"kurozoraBearer": []
}
],
"tags": [
"forum-threads"
],
"summary": "Gets the replies on a thread.",
"summary": "(optional authentication) Gets the replies on a thread.",
"description": "This endpoint will retrieve the replies on a thread.",
"parameters": [
{
Expand Down
2 changes: 1 addition & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
->middleware('kurozora.userauth');

Route::get('/{thread}/replies', [ForumThreadController::class, 'replies'])
->middleware('kurozora.userauth');
->middleware('kurozora.userauth:optional');

Route::post('/{thread}/replies', [ForumThreadController::class, 'postReply'])
->middleware('kurozora.userauth');
Expand Down

0 comments on commit 36f6be6

Please sign in to comment.