Skip to content

Instantly share code, notes, and snippets.

@merciof
Last active July 10, 2021 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merciof/2bb4982e24c7056608ce8ce133317df9 to your computer and use it in GitHub Desktop.
Save merciof/2bb4982e24c7056608ce8ce133317df9 to your computer and use it in GitHub Desktop.
Trait para a REST API do sebo-rural no CakePHP. Atende CRUD para as entidades Course, Book, Student e Genre.
<?php
declare(strict_types=1);
namespace App\Controller;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Datasource\ResultSetInterface;
use Cake\ORM\Table;
trait AbstractApi
{
public string $elementName;
public Table $elementTable;
public function index(): void
{
$element = $this->getAllElements();
$this->setElementToTemplate($element);
$this->serializeElement();
}
public function view($id): void
{
$element = $this->getElementById($id);
$this->setElementToTemplate($element);
$this->serializeElement();
}
public function add(): void
{
$this->request->allowMethod(['post', 'put']);
$element = $this->elementTable->newEntity( $this->request->getData() );
$this->saveElement($element);
}
public function edit($id): void
{
$this->request->allowMethod(['patch', 'post', 'put']);
$element = $this->elementTable->patchEntity( $this->elementTable->get($id), $this->request->getData() );
$this->saveElement($element);
}
public function delete($id): void
{
$this->request->allowMethod(['delete']);
$this->elementTable->delete($this->elementTable->get($id)) ? $message = 'Deletado com sucesso' : $message = 'Erro na deleçãp';
$this->set('message', $message);
$this->viewBuilder()->setOption('serialize', ['message']);
}
//******************* Métodos auxiliares ******************//
/**
* @param $id
* @return mixed
* @throws RecordNotFoundException
*/
public function getElementById($id): EntityInterface
{
switch ( $this->elementName ) {
case 'livro':
return $this->elementTable->get($id, ['contain' => ['Courses', 'Genres', 'Students'] ]);
case 'estudante':
return $this->elementTable->get($id, ['contain' => 'Courses' ]);
default:
return $this->elementTable->get($id);
}
// return $element;
}
public function getAllElements($offset = 0): ResultSetInterface {
// como fazer a paginação dos dados em JSON??
switch ( $this->elementName ) {
case 'estudante':
return $this->elementTable->find('all', ['contain' => 'Courses'])->offset($offset)->limit(10)->all();
case 'livro':
return $this->elementTable->find('all', ['contain' => ['Courses', 'Genres', 'Students'] ])->limit(10)->all();
default:
return $this->elementTable->find('all')->limit(10)->all();
}
}
/**
* @param $element
*/
public function saveElement($element): void
{
$this->elementTable->save($element)? $mensagem = 'Salvo' : $mensagem = 'Erro';
$this->set(['message' => $mensagem, $this->elementName => $element]);
$this->viewBuilder()->setOption('serialize', [$this->elementName, 'message']);
}
public function serializeElement(): void
{
$this->viewBuilder()->setOption('serialize', $this->elementName);
}
/**
* @param $element
*/
public function setElementToTemplate($element): void
{
$this->set($this->elementName, $element);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment