Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination feature #98

Merged
merged 5 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions shared/Commands/DemoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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':
Expand Down
31 changes: 19 additions & 12 deletions shared/Services/PostService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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;
}

/**
Expand Down
34 changes: 25 additions & 9 deletions tests/unit/PostServiceTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

use Quantum\Libraries\Database\PaginatorInterface;
use Quantum\Libraries\Storage\UploadedFile;
use Quantum\Libraries\Storage\FileSystem;
use Quantum\Factory\ServiceFactory;
use Shared\Services\AuthService;
use Shared\Services\PostService;
use PHPUnit\Framework\TestCase;
use Quantum\Http\Request;
use Quantum\Di\Di;
use Quantum\App;

Expand Down Expand Up @@ -62,6 +64,11 @@ class PostServiceTest extends TestCase
private $fs;
private $user;

/**
* @var Request
*/
private $request;

public function setUp(): void
{
App::loadCoreFunctions(dirname(__DIR__, 2) . DS . 'vendor' . DS . 'quantum' . DS . 'framework' . DS . 'src' . DS . 'Helpers');
Expand All @@ -70,6 +77,8 @@ public function setUp(): void

Di::loadDefinitions();

$this->request = new Request();

$this->fs = Di::get(FileSystem::class);

$this->authService = ServiceFactory::get(AuthService::class, ['shared' . DS . 'store', 'users']);
Expand All @@ -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);
}
Expand All @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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.',
Expand All @@ -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,
Expand All @@ -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()
Expand Down
Loading