forked from mirrors/easyappointments
Refactored the admins API controller (no engine code is being used).
This commit is contained in:
parent
e775f5e0eb
commit
afc05577a4
1 changed files with 89 additions and 73 deletions
|
@ -11,81 +11,92 @@
|
||||||
* @since v1.2.0
|
* @since v1.2.0
|
||||||
* ---------------------------------------------------------------------------- */
|
* ---------------------------------------------------------------------------- */
|
||||||
|
|
||||||
require_once __DIR__ . '/API_V1_Controller.php';
|
|
||||||
|
|
||||||
use EA\Engine\Api\V1\Request;
|
|
||||||
use EA\Engine\Api\V1\Response;
|
|
||||||
use EA\Engine\Types\NonEmptyText;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Admins Controller
|
* Admins API v1 controller.
|
||||||
*
|
*
|
||||||
* @package Controllers
|
* @package Controllers
|
||||||
*/
|
*/
|
||||||
class Admins extends API_V1_Controller {
|
class Admins extends EA_Controller {
|
||||||
/**
|
|
||||||
* Admins Resource Parser
|
|
||||||
*
|
|
||||||
* @var \EA\Engine\Api\V1\Parsers\Admins
|
|
||||||
*/
|
|
||||||
protected $parser;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Constructor
|
* Class Constructor
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
$this->load->model('admins_model');
|
$this->load->model('admins_model');
|
||||||
$this->parser = new \EA\Engine\Api\V1\Parsers\Admins;
|
|
||||||
|
$this->load->library('api');
|
||||||
|
|
||||||
|
$this->api->cors();
|
||||||
|
|
||||||
|
$this->api->auth();
|
||||||
|
|
||||||
|
$this->api->model('admins_model');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET API Method
|
* Get a single admin or an admin collection.
|
||||||
*
|
*
|
||||||
* @param int $id Optional (null), the record ID to be returned.
|
* @param int|null $id Admin ID.
|
||||||
*/
|
*/
|
||||||
public function get($id = NULL)
|
public function get(int $id = NULL)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$where = $id !== NULL ? ['id' => $id] : NULL;
|
$where = $id ? ['id' => $id] : NULL;
|
||||||
|
|
||||||
$admins = $this->admins_model->get($where);
|
$keyword = $this->api->request_keyword();
|
||||||
|
|
||||||
if ($id !== NULL && count($admins) === 0)
|
$limit = $this->api->request_limit();
|
||||||
|
|
||||||
|
$offset = $this->api->request_offset();
|
||||||
|
|
||||||
|
$order_by = $this->api->request_order_by();
|
||||||
|
|
||||||
|
$fields = $this->api->request_fields();
|
||||||
|
|
||||||
|
$admins = empty($keyword)
|
||||||
|
? $this->admins_model->get($where, $limit, $offset, $order_by)
|
||||||
|
: $this->admins_model->search($keyword, $limit, $offset, $order_by);
|
||||||
|
|
||||||
|
foreach ($admins as &$admin)
|
||||||
{
|
{
|
||||||
$this->throw_record_not_found();
|
$this->admins_model->api_encode($admin);
|
||||||
|
|
||||||
|
if ( ! empty($fields))
|
||||||
|
{
|
||||||
|
$this->admins_model->only($admin, $fields);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = new Response($admins);
|
$response = $id && ! empty($admins) ? $admins[0] : $admins;
|
||||||
|
|
||||||
$response->encode($this->parser)
|
if ( ! $response)
|
||||||
->search()
|
{
|
||||||
->sort()
|
response('Not Found', 404);
|
||||||
->paginate()
|
|
||||||
->minimize()
|
|
||||||
->singleEntry($id)
|
|
||||||
->output();
|
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
json_response($response);
|
||||||
}
|
}
|
||||||
catch (Throwable $e)
|
catch (Throwable $e)
|
||||||
{
|
{
|
||||||
$this->handle_exception($e);
|
json_exception($e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* POST API Method
|
* Create an admin.
|
||||||
*/
|
*/
|
||||||
public function post()
|
public function post()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Insert the admin to the database.
|
$admin = request();
|
||||||
$request = new Request();
|
|
||||||
$admin = $request->get_body();
|
$this->admins_model->api_decode($admin);
|
||||||
$this->parser->decode($admin);
|
|
||||||
|
|
||||||
if (array_key_exists('id', $admin))
|
if (array_key_exists('id', $admin))
|
||||||
{
|
{
|
||||||
|
@ -97,76 +108,81 @@ class Admins extends API_V1_Controller {
|
||||||
throw new Exception('No settings property provided.');
|
throw new Exception('No settings property provided.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$id = $this->admins_model->save($admin);
|
$admin_id = $this->admins_model->save($admin);
|
||||||
|
|
||||||
// Fetch the new object from the database and return it to the client.
|
$created_admin = $this->admins_model->find($admin_id);
|
||||||
$batch = $this->admins_model->get(['id' => $id]);
|
|
||||||
$response = new Response($batch);
|
$this->admins_model->api_encode($created_admin);
|
||||||
$status = new NonEmptyText('201 Created');
|
|
||||||
$response->encode($this->parser)->singleEntry(TRUE)->output($status);
|
json_response($created_admin, 201);
|
||||||
}
|
}
|
||||||
catch (Throwable $e)
|
catch (Throwable $e)
|
||||||
{
|
{
|
||||||
$this->handle_exception($e);
|
json_exception($e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PUT API Method
|
* Update an admin.
|
||||||
*
|
*
|
||||||
* @param int $id The record ID to be updated.
|
* @param int $id Admin ID.
|
||||||
*/
|
*/
|
||||||
public function put($id)
|
public function put(int $id)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Update the admin record.
|
$occurrences = $this->admins_model->get(['id' => $id]);
|
||||||
$batch = $this->admins_model->get(['id' => $id]);
|
|
||||||
|
|
||||||
if ($id !== NULL && count($batch) === 0)
|
if (empty($occurrences))
|
||||||
{
|
{
|
||||||
$this->throw_record_not_found();
|
response('', 404);
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$request = new Request();
|
$original_admin = $occurrences[0];
|
||||||
$updatedAdmin = $request->get_body();
|
|
||||||
$baseAdmin = $batch[0];
|
|
||||||
$this->parser->decode($updatedAdmin, $baseAdmin);
|
|
||||||
$updatedAdmin['id'] = $id;
|
|
||||||
$id = $this->admins_model->save($updatedAdmin);
|
|
||||||
|
|
||||||
// Fetch the updated object from the database and return it to the client.
|
$admin = request();
|
||||||
$batch = $this->admins_model->get(['id' => $id]);
|
|
||||||
$response = new Response($batch);
|
$this->admins_model->api_decode($admin, $original_admin);
|
||||||
$response->encode($this->parser)->singleEntry($id)->output();
|
|
||||||
|
$admin_id = $this->admins_model->save($admin);
|
||||||
|
|
||||||
|
$updated_admin = $this->admins_model->find($admin_id);
|
||||||
|
|
||||||
|
$this->admins_model->api_encode($updated_admin);
|
||||||
|
|
||||||
|
json_response($updated_admin);
|
||||||
}
|
}
|
||||||
catch (Throwable $e)
|
catch (Throwable $e)
|
||||||
{
|
{
|
||||||
$this->handle_exception($e);
|
json_exception($e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DELETE API Method
|
* Delete an admin.
|
||||||
*
|
*
|
||||||
* @param int $id The record ID to be deleted.
|
* @param int $id Admin ID.
|
||||||
*/
|
*/
|
||||||
public function delete($id)
|
public function delete(int $id)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
$occurrences = $this->admins_model->get(['id' => $id]);
|
||||||
|
|
||||||
|
if (empty($occurrences))
|
||||||
|
{
|
||||||
|
response('', 404);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$this->admins_model->delete($id);
|
$this->admins_model->delete($id);
|
||||||
|
|
||||||
$response = new Response([
|
|
||||||
'code' => 200,
|
|
||||||
'message' => 'Record was deleted successfully!'
|
|
||||||
]);
|
|
||||||
|
|
||||||
$response->output();
|
|
||||||
}
|
}
|
||||||
catch (Throwable $e)
|
catch (Throwable $e)
|
||||||
{
|
{
|
||||||
$this->handle_exception($e);
|
json_exception($e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue