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..00482ed 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; @@ -54,17 +56,21 @@ public function __init(PostTransformer $transformer) $this->transformer = $transformer; } - /** - * Get posts - * @return array - * @throws ConfigException - * @throws DatabaseException - * @throws DiException - * @throws ModelException - * @throws ReflectionException - */ - public function getPosts(): array + /** + * 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); + $page = $request->get('page', 1); + $posts = ModelFactory::get(Post::class) ->joinThrough(ModelFactory::get(User::class)) ->select( @@ -78,9 +84,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; } /** 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()