From 104a3042192bb366c1e73a5f256543d7ba6d97e9 Mon Sep 17 00:00:00 2001 From: Phillip Smith Date: Fri, 19 Oct 2018 10:01:47 +1100 Subject: [PATCH] expand api to additional tables --- app/API.php | 92 +++++++++++++++++++++++++++++++++++++++++++++++++- app/routes.cfg | 6 +++- 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/app/API.php b/app/API.php index 84ed8e2..fada318 100644 --- a/app/API.php +++ b/app/API.php @@ -32,8 +32,98 @@ function GetShow($f3,$params) { $db_show->next(); } + $this->__RenderJSON($data); + } + + + function GetEpisode($f3,$params) { + $db_episode = new DB\SQL\Mapper($f3->get('DB'), 'episodes'); + + // load the database record(s), with or without a filter + $episode_id = $f3->get('GET.id'); + if ($episode_id) + $db_episode->load(array('episode_id=?', $episode_id)); + else + $db_episode->load(); + + // build the response to the client + $data = array(); + while (!$db_episode->dry()) { + $data[$db_episode->episode_id] = $db_episode->cast(); + $db_episode->next(); + } + + $this->__RenderJSON($data); + } + + + function GetCategory($f3,$params) { + $db_category = new DB\SQL\Mapper($f3->get('DB'), 'categories'); + + // load the database record(s), with or without a filter + $category_id = $f3->get('GET.id'); + if ($category_id) + $db_category->load(array('category_id=?', $category_id)); + else + $db_category->load(); + + // build the response to the client + $data = array(); + while (!$db_category->dry()) { + $data[$db_category->category_id] = $db_category->cast(); + $db_category->next(); + } + + $this->__RenderJSON($data); + } + + + function GetLicense($f3,$params) { + $db_license = new DB\SQL\Mapper($f3->get('DB'), 'licenses'); + + // load the database record(s), with or without a filter + $license_id = $f3->get('GET.id'); + if ($license_id) + $db_license->load(array('license_id=?', $license_id)); + else + $db_license->load(); + + // build the response to the client + $data = array(); + while (!$db_license->dry()) { + $data[$db_license->license_id] = $db_license->cast(); + $db_license->next(); + } + + $this->__RenderJSON($data); + } + + + function GetUser($f3,$params) { + $db_user = new DB\SQL\Mapper($f3->get('DB'), 'users'); + + // load the database record(s), with or without a filter + $user_id = $f3->get('GET.id'); + if ($user_id) + $db_user->load(array('user_id=?', $user_id)); + else + $db_user->load(); + + // build the response to the client + $data = array(); + while (!$db_user->dry()) { + $data[$db_user->user_id] = $db_user->cast(); + unset($data[$db_user->user_id][passwd]); + $db_user->next(); + } + + $this->__RenderJSON($data); + } + + + private function __RenderJSON($jsonData) { header('Content-Type: application/json'); - echo json_encode($data, JSON_PRETTY_PRINT); + echo json_encode($jsonData, JSON_PRETTY_PRINT); } } diff --git a/app/routes.cfg b/app/routes.cfg index 969866a..b8ff8ba 100644 --- a/app/routes.cfg +++ b/app/routes.cfg @@ -26,7 +26,11 @@ GET @check_media: /admin/media_check= zMaintenance->ValidateMediaSto GET @fix_durations: /admin/fix_durations= zMaintenance->FixDurations # API v1 -GET @api_show: /api/v1/show= API->GetShow +GET @api_show: /api/v1/show= API->GetShow +GET @api_episode: /api/v1/episode= API->GetEpisode +GET @api_category:/api/v1/category= API->GetCategory +GET @api_license: /api/v1/license= API->GetLicense +GET @api_user: /api/v1/user= API->GetUser # Installation GET|POST @install: /install= Install->Main