| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Controller;
- use App\Http\Resources\CommentResource;
- use App\Models\Comment;
- use App\Models\Submission;
- use Illuminate\Http\Request;
- use Illuminate\Http\JsonResponse;
- class CommentController extends Controller
- {
- // Список комментариев для заявки
- public function index(int $submissionId): JsonResponse
- {
- $submission = Submission::findOrFail($submissionId);
- $comments = $submission->comments()->with('attachments')->latest()->get();
-
- return response()->json([
- 'success' => true,
- 'data' => CommentResource::collection($comments),
- ]);
- }
- // Создание комментария
- public function store(Request $request, int $submissionId): JsonResponse
- {
- $submission = Submission::findOrFail($submissionId);
-
- $validated = $request->validate([
- 'author' => 'required|string|max:100',
- 'content' => 'required|string',
- ]);
-
- $comment = $submission->comments()->create($validated);
- $comment->load('attachments');
-
- return response()->json([
- 'success' => true,
- 'message' => 'Комментарий успешно добавлен',
- 'data' => new CommentResource($comment),
- ], 201);
- }
- // Обновление комментария
- public function update(Request $request, int $submissionId, int $id): JsonResponse
- {
- $comment = Comment::where('submission_id', $submissionId)->findOrFail($id);
-
- $validated = $request->validate([
- 'author' => 'sometimes|required|string|max:100',
- 'content' => 'sometimes|required|string',
- ]);
-
- $comment->update($validated);
- $comment->load('attachments');
-
- return response()->json([
- 'success' => true,
- 'message' => 'Комментарий успешно обновлен',
- 'data' => new CommentResource($comment),
- ]);
- }
- // Удаление комментария
- public function destroy(int $submissionId, int $id): JsonResponse
- {
- $comment = Comment::where('submission_id', $submissionId)->findOrFail($id);
- $comment->delete();
-
- return response()->json([
- 'success' => true,
- 'message' => 'Комментарий успешно удален',
- ]);
- }
- }
|