optimizations

This commit is contained in:
2026-03-28 19:15:39 +01:00
parent 0b25d9570a
commit cab4fbd83e
509 changed files with 1016804 additions and 1605 deletions

View File

@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use App\Models\Collection;
use App\Models\CollectionComment;
use App\Services\CollectionCommentService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class CollectionCommentController extends Controller
{
public function __construct(
private readonly CollectionCommentService $comments,
) {
}
public function index(Request $request, Collection $collection): JsonResponse
{
abort_unless($collection->canBeViewedBy($request->user()), 404);
return response()->json([
'data' => $this->comments->mapComments($collection, $request->user()),
]);
}
public function store(Request $request, Collection $collection): JsonResponse
{
$this->authorize('comment', $collection);
$data = $request->validate([
'body' => ['required', 'string', 'min:2', 'max:4000'],
'parent_id' => ['nullable', 'integer', 'min:1'],
]);
$parent = null;
if (! empty($data['parent_id'])) {
$parent = CollectionComment::query()->findOrFail((int) $data['parent_id']);
abort_unless((int) $parent->collection_id === (int) $collection->id, 404);
}
$this->comments->create($collection->loadMissing('user'), $request->user(), (string) $data['body'], $parent);
return response()->json([
'ok' => true,
'comments' => $this->comments->mapComments($collection, $request->user()),
'comments_count' => (int) $collection->fresh()->comments_count,
]);
}
public function destroy(Request $request, Collection $collection, CollectionComment $comment): JsonResponse
{
abort_unless((int) $comment->collection_id === (int) $collection->id, 404);
$this->comments->delete($comment->load('collection'), $request->user());
return response()->json([
'ok' => true,
'comments' => $this->comments->mapComments($collection, $request->user()),
'comments_count' => (int) $collection->fresh()->comments_count,
]);
}
}