easyappointments/application/controllers/api/v1/Admins_api_v1.php

213 lines
4.7 KiB
PHP
Raw Normal View History

<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
2020-11-14 22:36:25 +03:00
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.5.0
* ---------------------------------------------------------------------------- */
/**
* Admins API v1 controller.
*
* @package Controllers
*/
class Admins_api_v1 extends EA_Controller {
/**
* Admins_api_v1 constructor.
*/
public function __construct()
{
parent::__construct();
$this->load->model('admins_model');
$this->load->library('api');
$this->api->cors();
$this->api->auth();
$this->api->model('admins_model');
}
/**
* Get an admin collection.
*/
public function index()
{
try
{
$keyword = $this->api->request_keyword();
$limit = $this->api->request_limit();
$offset = $this->api->request_offset();
2020-12-05 12:14:13 +03:00
$order_by = $this->api->request_order_by();
$fields = $this->api->request_fields();
$admins = empty($keyword)
? $this->admins_model->get(NULL, $limit, $offset, $order_by)
: $this->admins_model->search($keyword, $limit, $offset, $order_by);
foreach ($admins as &$admin)
{
$this->admins_model->api_encode($admin);
if ( ! empty($fields))
{
$this->admins_model->only($admin, $fields);
}
}
json_response($admins);
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Get a single admin.
*
* @param int|null $id Admin ID.
*/
public function show(int $id = NULL)
{
try
{
$fields = $this->api->request_fields();
$admin = $this->admins_model->find($id);
$this->admins_model->api_encode($admin);
if ( ! empty($fields))
{
$this->admins_model->only($admin, $fields);
}
if ( ! $admin)
{
response('', 404);
return;
}
json_response($admin);
2017-09-23 02:30:22 +03:00
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Create an admin.
*/
public function store()
{
try
{
$admin = request();
$this->admins_model->api_decode($admin);
if (array_key_exists('id', $admin))
{
unset($admin['id']);
}
if ( ! array_key_exists('settings', $admin))
{
throw new InvalidArgumentException('No settings property provided.');
}
$admin_id = $this->admins_model->save($admin);
$created_admin = $this->admins_model->find($admin_id);
$this->admins_model->api_encode($created_admin);
json_response($created_admin, 201);
2017-09-23 02:30:22 +03:00
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Update an admin.
*
* @param int $id Admin ID.
*/
public function update(int $id)
{
try
{
$occurrences = $this->admins_model->get(['id' => $id]);
if (empty($occurrences))
{
response('', 404);
return;
}
$original_admin = $occurrences[0];
$admin = request();
$this->admins_model->api_decode($admin, $original_admin);
$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);
2017-09-23 02:30:22 +03:00
}
catch (Throwable $e)
{
json_exception($e);
}
}
/**
* Delete an admin.
*
* @param int $id Admin ID.
*/
public function destroy(int $id)
{
try
{
$occurrences = $this->admins_model->get(['id' => $id]);
if (empty($occurrences))
{
response('', 404);
return;
}
$this->admins_model->delete($id);
response('', 204);
2017-09-23 02:30:22 +03:00
}
catch (Throwable $e)
{
json_exception($e);
}
}
}