Skip to content

Commit

Permalink
Added like action to threads overview
Browse files Browse the repository at this point in the history
  • Loading branch information
musa11971 committed Sep 10, 2019
1 parent fc07642 commit 494e452
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
24 changes: 3 additions & 21 deletions app/Http/Controllers/ForumSectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Helpers\JSONResult;
use App\Http\Requests\PostThread;
use App\Http\Resources\ForumSectionResource;
use App\Http\Resources\ForumThreadResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
Expand Down Expand Up @@ -41,7 +42,7 @@ public function details(ForumSection $section) {
}

/**
* Returns the threads of a section
* Returns the threads of a section.
*
* @param Request $request
* @param ForumSection $section
Expand Down Expand Up @@ -72,30 +73,11 @@ public function threads(Request $request, ForumSection $section) {

$threads = $threads->paginate(ForumSection::THREADS_PER_PAGE);

// Format the threads
$displayThreads = [];

foreach($threads as $thread)
$displayThreads[] = [
'id' => $thread->id,
'title' => $thread->title,
'content_teaser' =>
substr(strip_tags($thread->content), 0, 100) .
((strlen($thread->content) > 100) ? '...' : '')
,
'locked' => (bool) $thread->locked,
'poster_user_id' => $thread->user->id,
'poster_username' => $thread->user->username,
'creation_date' => $thread->created_at->format('Y-m-d H:i:s'),
'reply_count' => $thread->replies->count(),
'score' => $thread->likesDiffDislikesCount
];

// Show threads in response
return JSONResult::success([
'page' => (int) $givenPage,
'thread_pages' => $section->getPageCount(),
'threads' => $displayThreads
'threads' => ForumThreadResource::collection($threads)
]);
}

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

namespace App\Http\Resources;

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

class ForumThreadResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
$resource = [
'id' => $this->id,
'title' => $this->title,
'content' => $this->content,
'locked' => (bool) $this->locked,
'poster_user_id' => $this->user->id,
'poster_username' => $this->user->username,
'creation_date' => $this->created_at->format('Y-m-d H:i:s'),
'reply_count' => $this->replies->count(),
'score' => $this->likesDiffDislikesCount
];

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)
]
];
}
}
6 changes: 6 additions & 0 deletions public/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,12 @@
},
"/forum-sections/{sectionID}/threads": {
"get": {
"security": [
{},
{
"kurozoraBearer": []
}
],
"tags": [
"forum-sections"
],
Expand Down
3 changes: 2 additions & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@

Route::get('/{section}', [ForumSectionController::class, 'details']);

Route::get('/{section}/threads', [ForumSectionController::class, 'threads']);
Route::get('/{section}/threads', [ForumSectionController::class, 'threads'])
->middleware('kurozora.userauth:optional');

Route::post('/{section}/threads', [ForumSectionController::class, 'postThread'])
->middleware('kurozora.userauth');
Expand Down

0 comments on commit 494e452

Please sign in to comment.