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

Show total count #13

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2641166
Adding row count to ModelSet.
Feb 26, 2018
879f794
Adding rowCount to DBReadTrait.
Feb 26, 2018
9dc0031
Adding include_count detection in filters.
Feb 26, 2018
90d5043
Testing for total count to show up on data.
Feb 27, 2018
e39e1eb
Preliminary version of showing total count with date range.
Feb 28, 2018
54ea7bf
Refactoring TotalCount as its own PHP class.
Mar 1, 2018
478096b
Adding conditionals to reflect only showing total count by query para…
Mar 1, 2018
1238708
Fixed formatting.
Mar 1, 2018
62793f0
Showing totalCount only as a single number or null on API response.
Mar 1, 2018
c6f6651
First refactoring of obtainTotalCount.
Mar 1, 2018
b633d20
Renaming variable for easier reading and fitting local scope.
Mar 1, 2018
c21ee68
Removing PHPStorm autogenerated text.
Mar 1, 2018
15cc005
Removing APILogger not used in DBTrait.
Mar 1, 2018
9bb45bf
Removing TotalCount class. Moving total count related data back to Mo…
Mar 2, 2018
69958d0
Removing mentions of TotalCount class. Saving ModelSet filter for nex…
Mar 2, 2018
44443f6
DBReadTrait::obtainTotalCount() refactored to use Slim\PDO\Statement\…
Mar 2, 2018
a961cb3
Refactoring SuccessResponse to detect Model, Model[] and ModelSet.
Mar 2, 2018
b0240de
Removing Debug messages.
Mar 2, 2018
239099d
Only exposing data from ModelSet.
Mar 2, 2018
c8b6ce7
Refactor setData() to type specific initialization.
Mar 2, 2018
92596a2
Show current size of ModelSet in count.
Mar 2, 2018
fd3e7a4
Adding detection of ModelSet in SuccessResponse's constructor.
Mar 2, 2018
0bc7451
Remove redundant checking of true value for .
Mar 5, 2018
6f59192
Database lookup only returns count in obtainTotalCount().
Mar 5, 2018
6eb5fee
Refactoring nested if-else structure, using early return from functio…
Mar 5, 2018
4d82517
Typecasting to ensure safe assignments.
Mar 5, 2018
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
9 changes: 7 additions & 2 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ protected function getDefaultReadResponse(

$model->setLimit($this->getRequest()->getParam('limit'));

$includeTotalCount = $this->getRequest()->getParam('includeTotalCount') === 'true' ? true : false ;

if ($includeTotalCount === true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove checking for true here if you're sure it's going to be boolean

$model->setIncludeTotalCount($includeTotalCount);
}

if ($filter) {
$model->addFilter($filter);
}
Expand All @@ -260,8 +266,7 @@ protected function getDefaultReadResponse(
}

$model->read();

$response->initializeResponse($model->getData());
$response->initializeResponse($model);
} else {
if ($filter) {
if ($filter->getId()) {
Expand Down
29 changes: 29 additions & 0 deletions src/Model/ModelTrait/DBReadTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace NYPL\Starter\Model\ModelTrait;

use NYPL\Starter\APIException;
use NYPL\Starter\APILogger;
use NYPL\Starter\DB;
use NYPL\Starter\Filter;
use NYPL\Starter\Filter\OrFilter;
Expand Down Expand Up @@ -289,6 +290,10 @@ protected function setSet($ignoreNoRecord = false)
}

if ($selectStatement->rowCount()) {
if ($this->isIncludeTotalCount() === true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove checking for true

$this->obtainTotalCount();
}

$className = get_class($this->getBaseModel());

foreach ($selectStatement->fetchAll() as $result) {
Expand Down Expand Up @@ -336,6 +341,30 @@ protected function addOrderBy(SelectStatement $selectStatement)
return true;
}

/**
* @return bool
*/
protected function obtainTotalCount()
{
/**
* @var DBTrait $baseModel
*/
$baseModel = $this->getBaseModel();

$selectStatement = DB::getDatabase()->select(['id'])
->from($baseModel->translateDbName($baseModel->getTableName()))
->groupBy('id');
if ($this->getFilters()) {
$this->applyFilters($this->getFilters(), $selectStatement);
}

$selectStatement = $selectStatement->count('id')->execute();

$this->setTotalCount($selectStatement->rowCount());

return true;
}

/**
* @param bool $ignoreNoRecord
*
Expand Down
64 changes: 58 additions & 6 deletions src/Model/Response/SuccessResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use NYPL\Starter\Model;
use NYPL\Starter\Model\Response;
use NYPL\Starter\ModelSet;

abstract class SuccessResponse extends Response
{
Expand All @@ -16,13 +17,18 @@ abstract class SuccessResponse extends Response
*/
public $count = 0;

/**
* @var int
*/
public $totalCount;

/**
* @var int
*/
public $statusCode;

/**
* @param Model|Model[] $model
* @param Model|Model[]|ModelSet $model
* @param int $code
*/
public function __construct($model = null, $code = 200)
Expand All @@ -35,16 +41,46 @@ public function __construct($model = null, $code = 200)
}

/**
* @param Model|Model[] $model
* @param Model|Model[]|ModelSet $model
*/
public function initializeResponse($model)
{
if (is_array($model)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor out elses; you can just use return statements

$this->initializeArrayOfModels($model);
} else if ($model instanceof ModelSet) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move ModelSet to end to indicate it's the newest functionality?

$this->initializeModelSet($model);
} else if ($model instanceof Model) {
$this->initializeModel($model);
}
}

/**
* @param Model $model
*/
public function initializeModel($model)
{
$this->setData($model);
$this->setCount(1);
}

if (is_array($model)) {
$this->setCount(count($model));
} else {
$this->setCount(1);
/**
* @param Model[] $arrayOfModels
*/
public function initializeArrayOfModels($arrayOfModels)
{
$this->setData($arrayOfModels);
$this->setCount(count($arrayOfModels));
}

/**
* @param ModelSet $modelSet
*/
public function initializeModelSet($modelSet)
{
$this->setData($modelSet->getData());
$this->setCount(count($modelSet->getData()));
if ($modelSet->isIncludeTotalCount()) {
$this->setTotalCount($modelSet->getTotalCount());
}
}

Expand Down Expand Up @@ -95,4 +131,20 @@ public function setCount($count)
{
$this->count = $count;
}

/**
* @return int
*/
public function getTotalCount()
{
return $this->totalCount;
}

/**
* @param int $totalCount
*/
public function setTotalCount($totalCount)
{
$this->totalCount = $totalCount;
}
}
42 changes: 42 additions & 0 deletions src/ModelSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ class ModelSet extends Model implements ReadInterface
*/
public $limit = 25;

/**
* @var int
*/
public $totalCount = 0;

/**
* @var bool
*/
public $includeTotalCount = false;

/**
* @var bool
*/
Expand Down Expand Up @@ -175,4 +185,36 @@ public function setNoDefaultSorting($noDefaultSorting)
{
$this->noDefaultSorting = (bool) $noDefaultSorting;
}

/**
* @return bool
*/
public function isIncludeTotalCount()
{
return $this->includeTotalCount;
}

/**
* @param bool $includeTotalCount
*/
public function setIncludeTotalCount($includeTotalCount)
{
$this->includeTotalCount = $includeTotalCount;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe typecast the $includeTotalCount to (bool)?

}

/**
* @return int
*/
public function getTotalCount()
{
return $this->totalCount;
}

/**
* @param int $totalCount
*/
public function setTotalCount($totalCount = 0)
{
$this->totalCount = $totalCount;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe typecast $totalCount to (int)?

}
}