HoshiAI-be/app/Http/Resources/QuestionResource.php
2025-11-12 23:04:55 +01:00

66 lines
2.6 KiB
PHP

<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class QuestionResource extends JsonResource
{
/**
* @OA\Schema(
* schema="QuestionResource",
* type="object",
* title="Question",
* description="Question resource",
* @OA\Property(property="id", type="integer", example=1),
* @OA\Property(property="title", type="string", example="What is the capital of Japan?"),
* @OA\Property(property="description", type="string", nullable=true, example="Choose the correct option"),
* @OA\Property(property="type", type="string", enum={"single","multiply","text"}, example="single"),
* @OA\Property(property="difficulty", type="integer", example=3),
* @OA\Property(
* property="variants",
* type="array",
* nullable=true,
* @OA\Items(
* type="object",
* @OA\Property(property="id", type="integer", example=1),
* @OA\Property(property="text", type="string", example="Tokyo")
* )
* ),
* @OA\Property(
* property="correct_answers",
* type="array",
* @OA\Items(type="integer", example=1)
* ),
* @OA\Property(property="category_id", type="integer", nullable=true, example=3),
* @OA\Property(property="category", ref="#/components/schemas/CategoryResource"),
* @OA\Property(property="author_id", type="integer", nullable=true, example=2),
* @OA\Property(property="author", ref="#/components/schemas/UserResource"),
* @OA\Property(property="created_at", type="string", format="date-time", example="2025-11-11T10:00:00.000000Z"),
* @OA\Property(property="updated_at", type="string", format="date-time", example="2025-11-11T10:05:00.000000Z")
* )
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'title' => $this->title,
'description' => $this->description,
'type' => $this->type,
'difficulty' => $this->difficulty,
'variants' => $this->variants,
'correct_answers' => $this->correct_answers,
'category_id' => $this->category_id,
'category' => new CategoryResource($this->whenLoaded('category')),
'author_id' => $this->author_id,
'author' => new UserResource($this->whenLoaded('author')),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}