Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 14 additions & 57 deletions app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
use App\Http\Resources\CommentResource;
use App\Models\Comment;
use App\Services\ModelResolverService;
use Illuminate\Database\Eloquent\Collection;
use App\Http\Resources\UserResource;
use App\Services\CommentService;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Log;


class CommentController extends Controller
{
protected $modelResolver;
Expand Down Expand Up @@ -79,7 +79,7 @@ public function store(StoreCommentRequest $request)

// Ensure that they are commenting to the same root
// And the depth is not exceeded
validator(
Validator::make(
[
'commentable_id' => $commentableId,
'commentable_type' => $commentableType,
Expand Down Expand Up @@ -140,61 +140,18 @@ public function store(StoreCommentRequest $request)
/**
* Display the specified comment, with pagination.
*/
public function show(string $commentableType, int $commentableId, int $index)
public function show(Request $request, string $commentableType, int $commentableId, int $index, int $paginationLimit = -1)
{
validator(
[
'index' => $index,
'commentable_type' => $commentableType,
],
[
'index' => 'required|integer|min:0',
'commentable_type' => ['required', Rule::in(config('comment.commentable_types'))]
]
)->validate();

Log::debug("Request is, commentable_type: " . $commentableType . ". id: " . $commentableId . ". index: " . $index);

$commentableType = $this->modelResolver->getModelClass($commentableType);
$paginatedResults = $this->commentService->getPaginatedComments($commentableType, $commentableId, $index);
$nestedComments = new Collection($paginatedResults['comments']);

// Lazy eager load the user for the root comment and for each reply.
$nestedComments->load(['user', 'replies.user']);

// Flatten the comments and replies into the desired format.
$flattenedComments = collect();

foreach ($nestedComments as $comment) {
// Transform the root comment.
$flattenedComments->push(
new CommentResource($comment)
);

// Transform any loaded replies.
if ($comment->relationLoaded('replies')) {
foreach ($comment->replies as $reply) {
$flattenedComments->push(
new CommentResource($reply)
);
}
}
}

// Extract unique users into a separate collection.
$users = collect();

foreach ($flattenedComments as $comment) {
if ($comment->relationLoaded('user') && $comment->user) {
$users->put($comment->user->id, new UserResource($comment->user));
}
if ($paginationLimit == -1)
{
$paginationLimit = config('comment.default_pagination_limit');
}

Log::debug("Returned comments: " . json_encode($flattenedComments));
return [
'comments' => $flattenedComments,
'users' => $users->values(),
'has_more_comments' => $paginatedResults['has_more_comments'],
];
$sortBy = $request->query('sort_by', 'top');

Log::debug("Request is, commentable_type: " . $commentableType . ". id: " . $commentableId . ". index: " . $index . ". Sorting: " . $sortBy);
$paginatedResults = $this->commentService->getPaginatedComments($commentableType, $commentableId, $index, $paginationLimit, $sortBy);

return $paginatedResults;
}
}
54 changes: 41 additions & 13 deletions app/Http/Controllers/ComputerScienceResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

use App\Http\Requests\ComputerScienceResource\StoreResourceRequest;
use App\Models\ComputerScienceResource;
use App\Models\ResourceReview;
use Illuminate\Database\Console\DumpCommand;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Services\CommentService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Inertia\Inertia;
use Auth;

class ComputerScienceResourceController extends Controller
{
Expand All @@ -20,7 +18,8 @@ class ComputerScienceResourceController extends Controller
public function index()
{
// Eager load topic tags and other tag types as needed
$resources = ComputerScienceResource::paginate(10);
$resources = ComputerScienceResource::with(['tags', 'votes', 'upvoteSummary', 'reviewSummary', 'commentsCountRelationship'])
->paginate(10);
return Inertia::render('Resources/Index', [
'resources' => $resources,
]);
Expand Down Expand Up @@ -75,15 +74,44 @@ public function store(StoreResourceRequest $request)
/**
* Display the specified resource.
*/
public function show(ComputerScienceResource $computerScienceResource)
public function show(Request $request, CommentService $commentService, ComputerScienceResource $computerScienceResource, string $tab = 'reviews')
{
$reviews = $computerScienceResource->reviews()->orderByDesc('created_at')->get();

return Inertia::render('Resources/Show', [
'resource' => fn() => $computerScienceResource->load('user'),
'reviews' => fn () => $reviews,
]);
$validTabs = ['reviews', 'discussion', 'edits'];

if (!in_array($tab, $validTabs)) {
// Redirect to default if invalid
return redirect()->route('resources.show', [
'computerScienceResource' => $computerScienceResource->id,
'tab' => 'reviews',
]);
}

// return the resource and tab
$data = [
'tab' => $tab,
'resource' => $computerScienceResource,
];

// Load only the necessary tab data
if ($tab === 'reviews') {
$data['reviews'] = Inertia::defer(fn () =>
$computerScienceResource->reviews()->orderByDesc('created_at')->get()
);
} elseif ($tab === 'edits') {
$data['resourceEdits'] = Inertia::defer(fn () =>
$computerScienceResource->edits
);
} elseif ($tab === 'discussion') {
$sortBy = $request->query('sort_by', 'top');
$data['discussion'] = Inertia::defer(fn () =>
$commentService->getPaginatedComments('resource', $computerScienceResource->id, 0, 150, $sortBy)
);
$data['discussionSortByValue'] = $sortBy;
}

return Inertia::render('Resources/Show', $data);
}


/**
* Remove the specified resource from storage.
Expand Down
18 changes: 1 addition & 17 deletions app/Http/Controllers/ResourceEditsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,12 @@
use App\Services\ResourceEditsService;
use App\Http\Requests\ResourceEdit\StoreResourceEdit;
use App\Http\Resources\ComputerScienceResourceResource;
use Arr;
use Auth;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;
use Log;

class ResourceEditsController extends Controller
{
/**
* Show all the edits for a given index.
*/
public function index(ComputerScienceResource $computerScienceResource)
{
$edits = $computerScienceResource->edits;

Log::debug("The edits: " . json_encode($edits));

return Inertia::render('ResourceEdits/Index', [
'resourceId' => $computerScienceResource->id,
'resourceEdits' => fn() => $edits,
]);
}

/**
* Return the form to create a edit.
*/
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/ResourceReviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use App\Http\Requests\ResourceReview\StoreResourceReview;
use App\Models\ComputerScienceResource;
use App\Models\ResourceReview;
use Auth;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Redirect;

Expand Down
10 changes: 7 additions & 3 deletions app/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ public function share(Request $request): array
'location' => $request->url(),
],
'flash' => [
'success' => fn () => $request->session()->get('success'),
'warning' => fn () => $request->session()->get('warning'),
'error' => fn () => $request->session()->get('error'),
'success' => $request->session()->get('success'),
'warning' => $request->session()->get('warning'),
'error' => $request->session()->get('error'),
],
'config' => [
'COMMENT_MAX_DEPTH' => config('comment.max_depth'),
'COMMENT_PAGINATION_LIMIT' => config('comment.pagination_limit'),
]
];
}
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Requests/Comment/StoreCommentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Services\ModelResolverService;
use Illuminate\Foundation\Http\FormRequest;
use Auth;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

class StoreCommentRequest extends FormRequest
Expand Down Expand Up @@ -37,7 +37,7 @@ public function rules(): array
"commentable_type" => [
'required',
'string',
Rule::in(config('comment.commentable_types')),
Rule::in(config('comment.commentable_types_shorthand')),
],
"content" => ["required", "string", "max:4000"],
"parent_comment_id" => ["nullable", "exists:App\Models\Comment,id"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Http\Requests\ComputerScienceResource;

use App\Http\Requests\Shared\ComputerScienceResourceRequest;
use Auth;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Http\FormRequest;

class StoreResourceRequest extends FormRequest
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/ResourceEdit/StoreResourceEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Http\Requests\ResourceEdit;

use App\Http\Requests\Shared\ComputerScienceResourceRequest;
use Auth;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Http\FormRequest;

class StoreResourceEdit extends FormRequest
Expand Down
4 changes: 1 addition & 3 deletions app/Models/ComputerScienceResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Spatie\Tags\HasTags;
use Auth;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

Expand All @@ -24,8 +24,6 @@ class ComputerScienceResource extends Model
protected $table = "computer_science_resources";

protected $guarded = [];

protected $with = ['tags', 'votes', 'upvoteSummary', 'reviewSummary', 'commentsCountRelationship'];

protected $appends = ['topic_tags', 'programming_language_tags', 'general_tags', 'vote_score', 'user_vote', 'comments_count'];

Expand Down
2 changes: 1 addition & 1 deletion app/Models/ResourceEdits.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ResourceEdits extends Model

protected $guarded = [];

protected $with = ['votes','upvoteSummary', 'commentsCountRelationship'];
protected $with = ['votes','upvoteSummary', 'commentsCountRelationship', 'resource'];

protected $appends = ['user_vote', 'vote_score', 'comments_count', 'can_merge_edits'];

Expand Down
Loading
Loading