added questions_count to UserTest

This commit is contained in:
Stepan 2026-01-06 22:52:04 +01:00
parent e832391b85
commit 3f8667cb98
5 changed files with 13 additions and 17 deletions

View File

@ -71,7 +71,7 @@ class TestController extends Controller
*/ */
public function index(Request $request) public function index(Request $request)
{ {
$query = Test::with(['author', 'category']); $query = Test::with(['author', 'category'])->withCount('questions');
if ($request->has('category_id')) { if ($request->has('category_id')) {
$query->where('category_id', $request->query('category_id')); $query->where('category_id', $request->query('category_id'));
@ -161,7 +161,7 @@ class TestController extends Controller
*/ */
public function show(Test $test) public function show(Test $test)
{ {
$test->load(['author', 'category', 'questions']); $test->load(['author', 'category', 'questions'])->loadCount('questions');
return new TestResource($test); return new TestResource($test);
} }

View File

@ -130,10 +130,8 @@ class UserTestController extends Controller
* ), * ),
* @OA\Response( * @OA\Response(
* response=200, * response=200,
* description="User test created successfully", * description="UserTest created successfully",
* @OA\JsonContent( * @OA\JsonContent(ref="#/components/schemas/UserTestResource")
* @OA\Property(property="message", type="string", example="This test isn't available now!")
* )
* ), * ),
* @OA\Response( * @OA\Response(
* response=422, * response=422,

View File

@ -25,6 +25,7 @@ class TestResource extends JsonResource
* type="array", * type="array",
* @OA\Items(ref="#/components/schemas/QuestionResource") * @OA\Items(ref="#/components/schemas/QuestionResource")
* ), * ),
* @OA\Property(property="questions_count", type="integer", example=2),
* *
* @OA\Property(property="is_available", type="boolean", example=true), * @OA\Property(property="is_available", type="boolean", example=true),
* @OA\Property(property="author_id", type="integer", nullable=true, example=2), * @OA\Property(property="author_id", type="integer", nullable=true, example=2),
@ -46,6 +47,7 @@ class TestResource extends JsonResource
'category' => new CategoryResource($this->whenLoaded('category')), 'category' => new CategoryResource($this->whenLoaded('category')),
'questions' => QuestionResource::collection($this->whenLoaded('questions')), 'questions' => QuestionResource::collection($this->whenLoaded('questions')),
'questions_count' => $this->when(isset($this->questions_count), $this->questions_count, 0),
'is_available' => $this->isAvailable(), 'is_available' => $this->isAvailable(),
'author_id' => $this->author_id, 'author_id' => $this->author_id,

View File

@ -42,7 +42,5 @@ class Test extends Model
{ {
return $this->belongsToMany(Question::class) return $this->belongsToMany(Question::class)
->withTimestamps(); ->withTimestamps();
} }
} }

View File

@ -2442,17 +2442,11 @@
}, },
"responses": { "responses": {
"200": { "200": {
"description": "User test created successfully", "description": "UserTest created successfully",
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": {
"properties": { "$ref": "#/components/schemas/UserTestResource"
"message": {
"type": "string",
"example": "This test isn't available now!"
}
},
"type": "object"
} }
} }
} }
@ -2995,6 +2989,10 @@
"$ref": "#/components/schemas/QuestionResource" "$ref": "#/components/schemas/QuestionResource"
} }
}, },
"questions_count": {
"type": "integer",
"example": 2
},
"is_available": { "is_available": {
"type": "boolean", "type": "boolean",
"example": true "example": true