62 lines
1.6 KiB
PHP
62 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
/**
|
|
* @OA\Schema(
|
|
* schema="CategoryResource",
|
|
* type="object",
|
|
* title="Category",
|
|
* description="Category resource with dynamic count",
|
|
* @OA\Property(
|
|
* property="id",
|
|
* type="integer",
|
|
* example=1
|
|
* ),
|
|
* @OA\Property(
|
|
* property="name",
|
|
* type="string",
|
|
* example="Books"
|
|
* ),
|
|
* @OA\Property(
|
|
* property="created_at",
|
|
* type="string",
|
|
* format="date-time",
|
|
* example="2025-11-10T18:00:00Z"
|
|
* ),
|
|
* @OA\Property(
|
|
* property="questions_count",
|
|
* type="integer",
|
|
* example=5,
|
|
* description="Dynamic count of questions by category"
|
|
* ),
|
|
* @OA\Property(
|
|
* property="user_tests_count",
|
|
* type="integer",
|
|
* example=5,
|
|
* description="Dynamic count of user tests"
|
|
* )
|
|
* )
|
|
*/
|
|
class CategoryResource extends JsonResource
|
|
{
|
|
/**
|
|
* Transform the resource into an array.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function toArray(Request $request): array
|
|
{
|
|
return [
|
|
'id' => $this->id,
|
|
'name' => $this->name,
|
|
'created_at' => $this->created_at,
|
|
'questions_count' => $this->when(isset($this->questions_count), $this->questions_count, 0),
|
|
'user_tests_count' => $this->when(isset($this->user_tests_count), $this->user_tests_count, 0),
|
|
];
|
|
}
|
|
}
|