Skip to content
Open
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
46 changes: 21 additions & 25 deletions src/Contracts/Api/CrudBehaviorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Baka\Http\Contracts\Api;

use Phalcon\Http\Response;
use Phalcon\Http\RequestInterface;
use ArgumentCountError;
use Baka\Http\Converter\RequestUriToSql;
use Exception;
use PDO;
use Phalcon\Http\RequestInterface;
use Phalcon\Http\Response;
use Phalcon\Mvc\ModelInterface;
use ArgumentCountError;
use Phalcon\Mvc\Model\Resultset\Simple as SimpleRecords;
use PDO;
use Exception;

trait CrudBehaviorTrait
{
Expand All @@ -24,11 +24,11 @@ trait CrudBehaviorTrait
abstract protected function response($content, int $statusCode = 200, string $statusMessage = 'OK'): Response;

/**
* Given a request it will give you the SQL to process.
*
* @param RequestInterface $request
* @return string
*/
* Given a request it will give you the SQL to process.
*
* @param RequestInterface $request
* @return string
*/
protected function processRequest(RequestInterface $request): array
{
//parse the rquest
Expand Down Expand Up @@ -106,8 +106,8 @@ protected function getRecords(array $processedRequest): array
sprintf(
'Request no processed. Missing following params : %s.',
implode(', ', $diff)
)
);
)
);
}

$results = new SimpleRecords(
Expand All @@ -123,7 +123,7 @@ protected function getRecords(array $processedRequest): array

return [
'results' => $results,
'total' => $count
'total' => $count,
];
}

Expand Down Expand Up @@ -178,16 +178,12 @@ protected function processIndex()
*/
public function getById($id): Response
{
//find the info
$record = $this->model::findFirstOrFail([
'conditions' => $this->model->getPrimaryKey() . '= ?0',
'bind' => [$id]
]);

//get the results and append its relationships
$result = $this->appendRelationshipsToResult($this->request, $record);

return $this->response($this->processOutput($result));
$this->additionalSearchFields[] = [$this->model->getPrimaryKey(), ':', $id];
$results = $this->processIndex();
if (!$result) {
throw new Exception('record not found');
}
return $this->response($results);
}

/**
Expand Down Expand Up @@ -229,7 +225,7 @@ public function edit($id): Response
{
$record = $this->model::findFirstOrFail([
'conditions' => $this->model->getPrimaryKey() . '= ?0',
'bind' => [$id]
'bind' => [$id],
]);

//process the input
Expand Down Expand Up @@ -266,7 +262,7 @@ public function delete($id): Response
{
$record = $this->model::findFirstOrFail([
'conditions' => $this->model->getPrimaryKey() . '= ?0',
'bind' => [$id]
'bind' => [$id],
]);

if ($this->softDelete == 1) {
Expand Down