PaperJsonController.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Paper;
  5. use App\Services\PaperPayloadService;
  6. use Illuminate\Http\Request;
  7. class PaperJsonController extends Controller
  8. {
  9. public function show(Request $request, string $paperId)
  10. {
  11. $paper = Paper::with('questions')->find($paperId);
  12. if (!$paper) {
  13. return response()->json([
  14. 'success' => false,
  15. 'message' => 'Paper not found',
  16. ], 404);
  17. }
  18. $payload = app(PaperPayloadService::class)->buildPaperApiPayload($paper);
  19. if ($request->boolean('download')) {
  20. $filename = sprintf('paper_%s.json', $paperId);
  21. return response(
  22. json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
  23. 200,
  24. [
  25. 'Content-Type' => 'application/json; charset=utf-8',
  26. 'Content-Disposition' => 'attachment; filename="' . $filename . '"',
  27. ]
  28. );
  29. }
  30. return response()->json($payload);
  31. }
  32. }