Files
gamelog/app/Http/Controllers/GameCommentController.php
2026-05-30 23:47:32 +03:00

50 lines
1.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Game;
use App\Models\GameComment;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class GameCommentController extends Controller
{
public function store(Request $request, Game $game): JsonResponse
{
$data = $request->validate([
'body' => 'required|string|max:1000',
'parent_id' => 'nullable|integer|exists:game_comments,id',
]);
if (isset($data['parent_id'])) {
$parent = GameComment::findOrFail($data['parent_id']);
abort_if($parent->game_id !== $game->id, 422, 'Parent comment does not belong to this game.');
}
$comment = $game->comments()->create([
'user_id' => auth()->id(),
'body' => $data['body'],
'parent_id' => $data['parent_id'] ?? null,
]);
$comment->load('user');
return response()->json([
'id' => $comment->id,
'body' => $comment->body,
'user' => ['name' => $comment->user->name, 'slug' => $comment->user->slug],
'user_id' => $comment->user_id,
'created_at' => $comment->created_at->diffForHumans(),
'replies_count' => 0,
], 201);
}
public function destroy(GameComment $comment): JsonResponse
{
$this->authorize('delete', $comment);
$comment->delete();
return response()->json(['deleted' => true]);
}
}