Skip to content

Commit

Permalink
Add bookmarkable object to response and bookmarkable ids filter (#7)
Browse files Browse the repository at this point in the history
* Add bookmarable object to response, bookmarkable ids filter

* fix tests

---------

Co-authored-by: Tomasz Smolarek <tomasz.smolarek@escolasoft.com>
  • Loading branch information
dyfero and dyfero committed Aug 24, 2023
1 parent 76ca8cc commit 37e0480
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 11 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"require-dev": {
"phpunit/phpunit": "^9.0",
"orchestra/testbench": ">=5.0",
"darkaonline/l5-swagger": "8.1.0"
"darkaonline/l5-swagger": "8.1.0",
"escolalms/courses": "^0.4"
},
"license": "MIT",
"authors": [
Expand Down
7 changes: 6 additions & 1 deletion src/Dtos/CriteriaDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use EscolaLms\Core\Dtos\Contracts\InstantiateFromRequest;
use EscolaLms\Core\Dtos\CriteriaDto as BaseCriteriaDto;
use EscolaLms\Core\Repositories\Criteria\Primitives\EqualCriterion;
use EscolaLms\Core\Repositories\Criteria\Primitives\InCriterion;
use EscolaLms\Core\Repositories\Criteria\Primitives\IsNullCriterion;
use EscolaLms\Core\Repositories\Criteria\Primitives\NotNullCriterion;
use Illuminate\Http\Request;
Expand All @@ -25,11 +26,15 @@ public static function instantiateFromRequest(Request $request): self
);

}
if ($request->get('bookmarkable_type') && $request->get('bookmarkable_ids')) {
$criteria->push(new EqualCriterion('bookmarkable_type', $request->get('bookmarkable_type')));
$criteria->push(new InCriterion('bookmarkable_id', $request->get('bookmarkable_ids')));
}
if ($request->get('bookmarkable_type') && $request->get('bookmarkable_id')) {
$criteria->push(new EqualCriterion('bookmarkable_type', $request->get('bookmarkable_type')));
$criteria->push(new EqualCriterion('bookmarkable_id', $request->get('bookmarkable_id')));
}
if ($request->get('bookmarkable_type') && ! $request->get('bookmarkable_id')) {
if (($request->get('bookmarkable_type') && !$request->get('bookmarkable_id')) && ($request->get('bookmarkable_type') && !$request->get('bookmarkable_ids'))) {
$criteria[] = new EqualCriterion('bookmarkable_type', $request->get('bookmarkable_type'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ interface AdminBookmarkControllerSwagger
* ),
* ),
* @OA\Parameter(
* name="bookmarkable_ids[]",
* description="An array of bookmark IDs.",
* required=false,
* in="query",
* @OA\Schema(type="array", @OA\Items(type="number")),
* ),
* @OA\Parameter(
* name="bookmarkable_type",
* description="Bookmarkalbe type",
* required=false,
Expand Down
7 changes: 7 additions & 0 deletions src/Http/Controllers/Swagger/BookmarkControllerSwagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ public function delete(DeleteBookmarkRequest $request): JsonResponse;
* ),
* ),
* @OA\Parameter(
* name="bookmarkable_ids[]",
* description="An array of bookmark IDs.",
* required=false,
* in="query",
* @OA\Schema(type="array", @OA\Items(type="number")),
* ),
* @OA\Parameter(
* name="bookmarkable_type",
* description="Bookmarkalbe type",
* required=false,
Expand Down
3 changes: 3 additions & 0 deletions src/Http/Resources/BookmarkResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ class BookmarkResource extends JsonResource
{
public function toArray($request): array
{
$resourceClass = ('\EscolaLms\Bookmarks\Http\Resources\\' . class_basename($this->bookmarkable_type) . 'Resource');

return [
'id' => $this->id,
'value' => $this->value,
'user' => UserResource::make($this->user),
'bookmarkable' => class_exists($resourceClass) && $this->bookmarkable ? $resourceClass::make($this->bookmarkable) : null,
'bookmarkable_id' => $this->bookmarkable_id,
'bookmarkable_type' => $this->bookmarkable_type,
];
Expand Down
19 changes: 19 additions & 0 deletions src/Http/Resources/TopicResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace EscolaLms\Bookmarks\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class TopicResource extends JsonResource
{
public function toArray($request): array
{
return [
'id' => $this->id,
'title' => $this->title,
'type' => $this->topicable_type,
'lesson_id' => $this->lesson?->getKey(),
'course_id' => $this->lesson?->course?->getKey()
];
}
}
19 changes: 19 additions & 0 deletions tests/Api/AdminBookmarkIndexApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function testIndexBookmarkFilters(array $filters, callable $generator, in
'first_name',
'last_name',
],
'bookmarkable',
'bookmarkable_id',
'bookmarkable_type',
]]]);
Expand Down Expand Up @@ -165,6 +166,24 @@ public function filterDataProvider(): array
}),
'filterCount' => 1
],
[
'filter' => [
'bookmarkable_ids' => [123, 456],
'bookmarkable_type' => 'EscolaLms\\Courses\\Models\\Topic',
],
'data' => (function () {
$items = collect();
$items->push(Bookmark::factory());
$items->push(Bookmark::factory());
$items->push(Bookmark::factory()->state(['bookmarkable_type' => 'EscolaLms\\Courses\\Models\\Topic', 'bookmarkable_id' => 123]));
$items->push(Bookmark::factory()->state(['bookmarkable_type' => 'EscolaLms\\Courses\\Models\\Topic', 'bookmarkable_id' => 456]));
$items->push(Bookmark::factory()->state(['bookmarkable_type' => 'EscolaLms\\Courses\\Models\\Topic']));
$items->push(Bookmark::factory()->state(['bookmarkable_type' => 'EscolaLms\\Courses\\Models\\Course']));

return $items;
}),
'filterCount' => 2
],
];
}

Expand Down
77 changes: 69 additions & 8 deletions tests/Api/BookmarkIndexApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use EscolaLms\Bookmarks\Tests\BookmarkTesting;
use EscolaLms\Bookmarks\Tests\TestCase;
use EscolaLms\Core\Tests\CreatesUsers;
use EscolaLms\Courses\Models\Course;
use EscolaLms\Courses\Models\Lesson;
use EscolaLms\Courses\Models\Topic;
use Illuminate\Support\Arr;

class BookmarkIndexApiTest extends TestCase
Expand Down Expand Up @@ -39,6 +42,46 @@ public function testIndexBookmarkFilters(array $filters, callable $generator, in
'first_name',
'last_name',
],
'bookmarkable',
'bookmarkable_id',
'bookmarkable_type',
]]]);
}

public function testIndexBookmarkTopic(): void
{
$user = $this->makeStudent();
$course = Course::factory()->create();
$lesson = Lesson::factory()->create([
'course_id' => $course->getKey(),
]);
$topic = Topic::factory()->create([
'lesson_id' => $lesson->getKey()
]);
Bookmark::factory()->create([
'user_id' => $user->getKey(),
'bookmarkable_id' => $topic->getKey(),
'bookmarkable_type' => 'EscolaLms\Courses\Models\Topic'
]);

$this->actingAs($user, 'api')
->getJson('api/bookmarks')
->assertOk()
->assertJsonStructure(['data' => [[
'id',
'value',
'user' => [
'id',
'first_name',
'last_name',
],
'bookmarkable' => [
'id',
'title',
'type',
'lesson_id',
'course_id',
],
'bookmarkable_id',
'bookmarkable_type',
]]]);
Expand Down Expand Up @@ -170,6 +213,24 @@ public function filterDataProvider(): array
}),
'filterCount' => 1
],
[
'filter' => [
'bookmarkable_ids' => [123, 456],
'bookmarkable_type' => 'EscolaLms\\Courses\\Models\\Topic',
],
'data' => (function (int $userId) {
$items = collect();
$items->push(Bookmark::factory());
$items->push(Bookmark::factory()->state(['user_id' => $userId]));
$items->push(Bookmark::factory()->state(['user_id' => $userId, 'bookmarkable_type' => 'EscolaLms\\Courses\\Models\\Topic', 'bookmarkable_id' => 123]));
$items->push(Bookmark::factory()->state(['user_id' => $userId, 'bookmarkable_type' => 'EscolaLms\\Courses\\Models\\Topic', 'bookmarkable_id' => 456]));
$items->push(Bookmark::factory()->state(['user_id' => $userId, 'bookmarkable_type' => 'EscolaLms\\Courses\\Models\\Topic']));
$items->push(Bookmark::factory()->state(['user_id' => $userId, 'bookmarkable_type' => 'EscolaLms\\Courses\\Models\\Course']));

return $items;
}),
'filterCount' => 2
],
];
}

Expand All @@ -181,15 +242,15 @@ public function orderDataProvider(): array
'order_by' => 'id',
'order' => 'asc',
],
'data' => (function(int $userId) {
'data' => (function (int $userId) {
$items = collect();
$items->push(Bookmark::factory()->state(['id' => 1, 'user_id' => $userId]));
$items->push(Bookmark::factory()->state(['id' => 2, 'user_id' => $userId]));
$items->push(Bookmark::factory()->state(['id' => 3, 'user_id' => $userId]));

return $items;
}),
'assert' => (function($data) {
'assert' => (function ($data) {
$this->assertEquals(1, Arr::first($data->getData()->data)->id);
$this->assertEquals(3, Arr::last($data->getData()->data)->id);
})
Expand All @@ -199,15 +260,15 @@ public function orderDataProvider(): array
'order_by' => 'id',
'order' => 'desc',
],
'data' => (function(int $userId) {
'data' => (function (int $userId) {
$items = collect();
$items->push(Bookmark::factory()->state(['id' => 1, 'user_id' => $userId]));
$items->push(Bookmark::factory()->state(['id' => 2, 'user_id' => $userId]));
$items->push(Bookmark::factory()->state(['id' => 3, 'user_id' => $userId]));

return $items;
}),
'assert' => (function($data) {
'assert' => (function ($data) {
$this->assertEquals(1, Arr::last($data->getData()->data)->id);
$this->assertEquals(3, Arr::first($data->getData()->data)->id);
})
Expand All @@ -217,15 +278,15 @@ public function orderDataProvider(): array
'order_by' => 'value',
'order' => 'asc',
],
'data' => (function(int $userId) {
'data' => (function (int $userId) {
$items = collect();
$items->push(Bookmark::factory()->state(['user_id' => $userId, 'value' => 'aaa']));
$items->push(Bookmark::factory()->state(['user_id' => $userId, 'value' => 'bbb']));
$items->push(Bookmark::factory()->state(['user_id' => $userId, 'value' => 'ccc']));

return $items;
}),
'assert' => (function($data) {
'assert' => (function ($data) {
$this->assertEquals('aaa', Arr::first($data->getData()->data)->value);
$this->assertEquals('ccc', Arr::last($data->getData()->data)->value);
})
Expand All @@ -235,15 +296,15 @@ public function orderDataProvider(): array
'order_by' => 'value',
'order' => 'desc',
],
'data' => (function(int $userId) {
'data' => (function (int $userId) {
$items = collect();
$items->push(Bookmark::factory()->state(['user_id' => $userId, 'value' => 'aaa']));
$items->push(Bookmark::factory()->state(['user_id' => $userId, 'value' => 'bbb']));
$items->push(Bookmark::factory()->state(['user_id' => $userId, 'value' => 'ccc']));

return $items;
}),
'assert' => (function($data) {
'assert' => (function ($data) {
$this->assertEquals('aaa', Arr::last($data->getData()->data)->value);
$this->assertEquals('ccc', Arr::first($data->getData()->data)->value);
})
Expand Down
9 changes: 8 additions & 1 deletion tests/BookmarkTesting.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ public function prepareUri(string $prefix, array $filters): string {
$uri = $prefix . '?';

foreach ($filters as $key => $value) {
$uri .= $key . '=' . $value . '&';
if (is_array($value)) {
foreach ($value as $v) {
$uri .= $key . '[]=' . $v . '&';
}
}
else {
$uri .= $key . '=' . $value . '&';
}
}

return $uri;
Expand Down

0 comments on commit 37e0480

Please sign in to comment.