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

201 lines
4.8 KiB
PHP
Raw Normal View History

<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
2022-01-18 15:05:42 +03:00
* Easy!Appointments - Online Appointment Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
2021-12-18 19:43:45 +03:00
* @copyright Copyright (c) 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->auth();
$this->api->model('admins_model');
}
/**
* Get an admin collection.
*/
2024-01-05 21:12:25 +03:00
public function index(): void
{
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();
$with = $this->api->request_with();
$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);
}
if (!empty($with)) {
$this->admins_model->load($admin, $with);
}
}
json_response($admins);
} catch (Throwable $e) {
json_exception($e);
}
}
/**
* Get a single admin.
*
* @param int|null $id Admin ID.
*/
2024-01-05 21:12:25 +03:00
public function show(int $id = null): void
{
try {
$fields = $this->api->request_fields();
$with = $this->api->request_with();
$admin = $this->admins_model->find($id);
$this->admins_model->api_encode($admin);
if (!empty($fields)) {
$this->admins_model->only($admin, $fields);
}
if (!empty($with)) {
$this->admins_model->load($admin, $with);
}
if (!$admin) {
response('', 404);
return;
}
json_response($admin);
} catch (Throwable $e) {
json_exception($e);
}
}
/**
* Store a new admin.
*/
2024-01-05 21:12:25 +03:00
public function store(): void
{
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);
} catch (Throwable $e) {
json_exception($e);
}
}
/**
* Update an admin.
*
* @param int $id Admin ID.
*/
2024-01-05 21:12:25 +03:00
public function update(int $id): void
{
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);
} catch (Throwable $e) {
json_exception($e);
}
}
/**
* Delete an admin.
*
* @param int $id Admin ID.
*/
2024-01-05 21:12:25 +03:00
public function destroy(int $id): void
{
try {
$occurrences = $this->admins_model->get(['id' => $id]);
if (empty($occurrences)) {
response('', 404);
return;
}
$this->admins_model->delete($id);
response('', 204);
} catch (Throwable $e) {
json_exception($e);
}
}
}