From 54d4d2a780a8e28142033fbef6ba58218783defa Mon Sep 17 00:00:00 2001 From: Vahram Date: Fri, 6 Sep 2024 17:14:04 +0400 Subject: [PATCH 1/5] Pagination feature --- shared/Commands/DemoCommand.php | 4 ++-- shared/Services/PostService.php | 12 +++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/shared/Commands/DemoCommand.php b/shared/Commands/DemoCommand.php index e7e77e5..2aef93b 100644 --- a/shared/Commands/DemoCommand.php +++ b/shared/Commands/DemoCommand.php @@ -91,7 +91,7 @@ class DemoCommand extends QtCommand /** * How many posts to create */ - const POST_COUNT_PER_USER = 4; + const POST_COUNT_PER_USER = 20; /** * Default password for generated users @@ -257,7 +257,7 @@ private function cleanUp() $migrationTable->up($tableFactory); } - $this->runExternalCommand(self::COMMAND_MIGRATE, ['direction' => 'down', '--yes' => true]); + $this->runExternalCommand(self::COMMAND_MIGRATE, ['direction' => 'down']); $this->runExternalCommand(self::COMMAND_MIGRATE, ['direction' => 'up']); break; case 'sleekdb': diff --git a/shared/Services/PostService.php b/shared/Services/PostService.php index be40285..ba31912 100644 --- a/shared/Services/PostService.php +++ b/shared/Services/PostService.php @@ -14,6 +14,8 @@ namespace Shared\Services; +use Quantum\Http\Request; +use Quantum\Libraries\Database\PaginatorInterface; use Quantum\Libraries\Transformer\TransformerInterface; use Quantum\Exceptions\FileSystemException; use Quantum\Exceptions\FileUploadException; @@ -63,8 +65,11 @@ public function __init(PostTransformer $transformer) * @throws ModelException * @throws ReflectionException */ - public function getPosts(): array + public function getPosts(Request $request): PaginatorInterface { + $per_page = $request->get('per_page', 12); + $page = $request->get('page', 1); + $posts = ModelFactory::get(Post::class) ->joinThrough(ModelFactory::get(User::class)) ->select( @@ -78,9 +83,10 @@ public function getPosts(): array ['users.uuid' => 'user_directory'] ) ->orderBy('updated_at', 'desc') - ->get(); + ->paginate($per_page, $page); - return transform($posts, $this->transformer); + $posts->data = transform($posts->data(), $this->transformer); + return $posts; } /** From 5e420b7a287ccdacba33f60f2e591318a8ce5807 Mon Sep 17 00:00:00 2001 From: Vahram Date: Thu, 12 Sep 2024 11:59:46 +0400 Subject: [PATCH 2/5] returned deleted property --- shared/Commands/DemoCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/Commands/DemoCommand.php b/shared/Commands/DemoCommand.php index 2aef93b..97f0ee2 100644 --- a/shared/Commands/DemoCommand.php +++ b/shared/Commands/DemoCommand.php @@ -257,7 +257,7 @@ private function cleanUp() $migrationTable->up($tableFactory); } - $this->runExternalCommand(self::COMMAND_MIGRATE, ['direction' => 'down']); + $this->runExternalCommand(self::COMMAND_MIGRATE, ['direction' => 'down', '--yes' => true]); $this->runExternalCommand(self::COMMAND_MIGRATE, ['direction' => 'up']); break; case 'sleekdb': From 303196d651562e33b6a5fb391ef9643845da6805 Mon Sep 17 00:00:00 2001 From: Vahram Date: Thu, 12 Sep 2024 13:13:24 +0400 Subject: [PATCH 3/5] removed unused argument --- shared/Commands/DemoCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/Commands/DemoCommand.php b/shared/Commands/DemoCommand.php index 97f0ee2..2aef93b 100644 --- a/shared/Commands/DemoCommand.php +++ b/shared/Commands/DemoCommand.php @@ -257,7 +257,7 @@ private function cleanUp() $migrationTable->up($tableFactory); } - $this->runExternalCommand(self::COMMAND_MIGRATE, ['direction' => 'down', '--yes' => true]); + $this->runExternalCommand(self::COMMAND_MIGRATE, ['direction' => 'down']); $this->runExternalCommand(self::COMMAND_MIGRATE, ['direction' => 'up']); break; case 'sleekdb': From f443c4a651f0274afc650c361b686c40d2b28a14 Mon Sep 17 00:00:00 2001 From: Vahram Date: Thu, 12 Sep 2024 13:33:49 +0400 Subject: [PATCH 4/5] correcting unit tests after adding pagination feature --- tests/unit/PostServiceTest.php | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/tests/unit/PostServiceTest.php b/tests/unit/PostServiceTest.php index d715593..1596466 100644 --- a/tests/unit/PostServiceTest.php +++ b/tests/unit/PostServiceTest.php @@ -1,11 +1,13 @@ request = new Request(); + $this->fs = Di::get(FileSystem::class); $this->authService = ServiceFactory::get(AuthService::class, ['shared' . DS . 'store', 'users']); @@ -78,6 +87,8 @@ public function setUp(): void $this->postService = ServiceFactory::get(PostService::class, ['shared' . DS . 'store', 'posts']); + $this->request->set('per_page', 5); + foreach ($this->initialPosts as $post) { $this->postService->addPost($post); } @@ -94,13 +105,14 @@ public function testGetPosts() { $this->assertIsObject($this->postService); - $posts = $this->postService->getPosts(); + $posts = $this->postService->getPosts($this->request); - $this->assertIsArray($posts); + $this->assertInstanceOf(PaginatorInterface::class, $posts); + $this->assertIsArray($posts->data); - $this->assertCount(4, $posts); + $this->assertCount(4, $posts->data); - $post = $posts[0]; + $post = $posts->data[0]; $this->assertIsArray($post); @@ -111,7 +123,9 @@ public function testGetPosts() public function testGetSinglePost() { - $uuid = $this->postService->getPosts()[0]['id']; + $posts = $this->postService->getPosts($this->request); + + $uuid = $posts->data[0]['id']; $post = $this->postService->getPost($uuid); @@ -157,7 +171,9 @@ public function testUpdatePost() { $date = date('Y-m-d H:i:s'); - $uuid = $this->postService->getPosts()[0]['id']; + $posts = $this->postService->getPosts($this->request); + + $uuid = $posts->data[0]['id']; $this->postService->updatePost($uuid, [ 'title' => 'Walt Disney Jr.', @@ -183,7 +199,7 @@ public function testUpdatePost() public function testDeletePost() { - $this->assertCount(4, $this->postService->getPosts()); + $this->assertCount(4, $this->postService->getPosts($this->request)->data); $post = $this->postService->addPost([ 'user_id' => 1, @@ -193,11 +209,11 @@ public function testDeletePost() 'updated_at' => date('Y-m-d H:i:s') ]); - $this->assertCount(5, $this->postService->getPosts()); + $this->assertCount(5, $this->postService->getPosts($this->request)->data); $this->postService->deletePost($post['uuid']); - $this->assertCount(4, $this->postService->getPosts()); + $this->assertCount(4, $this->postService->getPosts($this->request)->data); } public function testSaveDeleteImage() From 230ac417492a56d468166f69ac3ae389b4acb1fc Mon Sep 17 00:00:00 2001 From: Vahram Date: Thu, 12 Sep 2024 16:19:28 +0400 Subject: [PATCH 5/5] correcting return type --- shared/Services/PostService.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/shared/Services/PostService.php b/shared/Services/PostService.php index ba31912..00482ed 100644 --- a/shared/Services/PostService.php +++ b/shared/Services/PostService.php @@ -56,15 +56,16 @@ public function __init(PostTransformer $transformer) $this->transformer = $transformer; } - /** - * Get posts - * @return array - * @throws ConfigException - * @throws DatabaseException - * @throws DiException - * @throws ModelException - * @throws ReflectionException - */ + /** + * Get posts + * @param Request $request + * @return PaginatorInterface + * @throws ConfigException + * @throws DatabaseException + * @throws DiException + * @throws ModelException + * @throws ReflectionException + */ public function getPosts(Request $request): PaginatorInterface { $per_page = $request->get('per_page', 12);