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

View File

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

View File

@ -25,6 +25,7 @@ class TestResource extends JsonResource
* type="array",
* @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="author_id", type="integer", nullable=true, example=2),
@ -46,6 +47,7 @@ class TestResource extends JsonResource
'category' => new CategoryResource($this->whenLoaded('category')),
'questions' => QuestionResource::collection($this->whenLoaded('questions')),
'questions_count' => $this->when(isset($this->questions_count), $this->questions_count, 0),
'is_available' => $this->isAvailable(),
'author_id' => $this->author_id,

View File

@ -43,6 +43,4 @@ class Test extends Model
return $this->belongsToMany(Question::class)
->withTimestamps();
}
}

View File

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