From 346b8c82e1ecb986ab0dffd9ddd40855268ea255 Mon Sep 17 00:00:00 2001 From: paolo Date: Fri, 22 Apr 2016 11:08:38 +0200 Subject: [PATCH] JSON output improved --- application/controllers/api/v1/Nation.php | 8 +- .../controllers/api/v1/person/Person.php | 78 ++++++++----------- .../controllers/api/v1/studies/Course.php | 6 +- .../controllers/api/v1/studies/Plan.php | 4 +- application/models/Nation_model.php | 2 +- application/models/person/Person_model.php | 42 ++++++---- 6 files changed, 71 insertions(+), 69 deletions(-) diff --git a/application/controllers/api/v1/Nation.php b/application/controllers/api/v1/Nation.php index 4756c227f..60af416e7 100644 --- a/application/controllers/api/v1/Nation.php +++ b/application/controllers/api/v1/Nation.php @@ -35,12 +35,12 @@ class Nation extends REST_Controller $result = $this->NationModel->getAll($notLocked, $orderEnglish); - if(!is_null($result) && $result->num_rows() > 0) + if(is_object($result)) { $payload = [ 'success' => TRUE, 'message' => 'Nation found', - 'data' => $result->result()[0] + 'data' => $result->result() ]; $httpstatus = REST_Controller::HTTP_OK; } @@ -60,12 +60,12 @@ class Nation extends REST_Controller { $result = $this->NationModel->getFederalState(); - if(!is_null($result) && $result->num_rows() > 0) + if(is_object($result)) { $payload = [ 'success' => TRUE, 'message' => 'Bundesland found', - 'data' => $result->result_array() + 'data' => $result->result() ]; $httpstatus = REST_Controller::HTTP_OK; } diff --git a/application/controllers/api/v1/person/Person.php b/application/controllers/api/v1/person/Person.php index 47f63e498..a75981a32 100644 --- a/application/controllers/api/v1/person/Person.php +++ b/application/controllers/api/v1/person/Person.php @@ -39,26 +39,14 @@ class Person extends REST_Controller $result = $this->PersonModel->getPerson($personID, $code, $email); - if(!is_null($result) && $result->num_rows() > 0) + if(is_object($result)) { - if($result->num_rows() > 1) - { - $payload = [ - 'success' => TRUE, - 'message' => 'People found', - 'data' => $result->result()[0] - ]; - $httpstatus = REST_Controller::HTTP_OK; - } - else if($result->num_rows() == 1) - { - $payload = [ - 'success' => TRUE, - 'message' => 'Person found', - 'data' => $result->result()[0] - ]; - $httpstatus = REST_Controller::HTTP_OK; - } + $payload = [ + 'success' => TRUE, + 'message' => 'People found', + 'data' => $result->result() + ]; + $httpstatus = REST_Controller::HTTP_OK; } else { @@ -98,6 +86,32 @@ class Person extends REST_Controller } $this->response($payload, $httpstatus); } + + /** + * + */ + public function postInterestedStudent() + { + $result = $this->PersonModel->saveInterestedStudent($this->post()); + + if($result === TRUE) + { + $httpstatus = REST_Controller::HTTP_OK; + $payload = [ + 'success' => true, + 'message' => 'Interested student saved.' + ]; + } + else + { + $payload = [ + 'success' => false, + 'message' => 'Could not save interested student.' + ]; + $httpstatus = REST_Controller::HTTP_OK; + } + $this->response($payload, $httpstatus); + } /** * @return void @@ -140,30 +154,4 @@ class Person extends REST_Controller $this->response($payload, $httpstatus); } - - /** - * - */ - public function postInterestedStudent() - { - $result = $this->PersonModel->saveInterestedStudent($this->post()); - - if($result === TRUE) - { - $httpstatus = REST_Controller::HTTP_OK; - $payload = [ - 'success' => true, - 'message' => 'Interested student saved.' - ]; - } - else - { - $payload = [ - 'success' => false, - 'message' => 'Could not save interested student.' - ]; - $httpstatus = REST_Controller::HTTP_OK; - } - $this->response($payload, $httpstatus); - } } \ No newline at end of file diff --git a/application/controllers/api/v1/studies/Course.php b/application/controllers/api/v1/studies/Course.php index 75d329ce1..36b911a8a 100644 --- a/application/controllers/api/v1/studies/Course.php +++ b/application/controllers/api/v1/studies/Course.php @@ -32,12 +32,12 @@ class Course extends REST_Controller { $result = $this->CourseModel->getEnabledCourses(); - if(!is_null($result) && $result->num_rows() > 0) + if(is_object($result)) { $payload = [ 'success' => TRUE, 'message' => 'Courses found', - 'data' => $result->result()[0] + 'data' => $result->result() ]; $httpstatus = REST_Controller::HTTP_OK; } @@ -45,7 +45,7 @@ class Course extends REST_Controller { $payload = [ 'success' => FALSE, - 'message' => 'Person not found' + 'message' => 'No courses found' ]; $httpstatus = REST_Controller::HTTP_OK; } diff --git a/application/controllers/api/v1/studies/Plan.php b/application/controllers/api/v1/studies/Plan.php index 909a7b6a5..b25d502cf 100644 --- a/application/controllers/api/v1/studies/Plan.php +++ b/application/controllers/api/v1/studies/Plan.php @@ -34,12 +34,12 @@ class Plan extends REST_Controller $result = $this->PlanModel->getCurricula($courseOfStudiesID); - if(!is_null($result) && $result->num_rows() > 0) + if(is_object($result)) { $payload = [ 'success' => TRUE, 'message' => 'Curricula found', - 'data' => $result->result()[0] + 'data' => $result->result() ]; $httpstatus = REST_Controller::HTTP_OK; } diff --git a/application/models/Nation_model.php b/application/models/Nation_model.php index 396e44242..e06605e80 100644 --- a/application/models/Nation_model.php +++ b/application/models/Nation_model.php @@ -38,7 +38,7 @@ class Nation_model extends DB_Model if($notLocked) { - $qry .= " WHERE sperre is null"; + $qry .= " WHERE sperre IS NULL"; } if(!$orderEnglish) { diff --git a/application/models/person/Person_model.php b/application/models/person/Person_model.php index 9c03e4921..5402dc1eb 100644 --- a/application/models/person/Person_model.php +++ b/application/models/person/Person_model.php @@ -56,11 +56,11 @@ class Person_model extends DB_Model // All the code should be put inside this if statement if($this->_checkPermissions()) { - if((!is_null($code)) && (!is_null($email))) + if((isset($code)) && (isset($email))) { $result = $this->_getPersonByCodeAndEmail($code, $email); } - elseif(!is_null($code)) + elseif(isset($code)) { $result = $this->_getPersonByCode($code); } @@ -77,11 +77,11 @@ class Person_model extends DB_Model * @param int $personID Person ID * @return object */ - private function _getPersonByID($personID) + private function _getPersonByID($personID = NULL) { $result = NULL; - if(!is_null($personID)) + if(isset($personID)) { $result = $this->db->query($this->_loadQuery, array($personID)); } @@ -92,24 +92,38 @@ class Person_model extends DB_Model /** * */ - private function _getPersonByCodeAndEmail($code, $email) + private function _getPersonByCodeAndEmail($code = NULL, $email = NULL) { - $this->db->select("*") - ->from('public.tbl_person p') - ->join("public.tbl_kontakt k", "k.person_id=p.person_id") - ->where("p.zugangscode", $code) - ->where("k.kontakt", $email); + $result = NULL; + $query = "SELECT * + FROM public.tbl_person p JOIN public.tbl_kontakt k USING (person_id) + WHERE p.zugangscode = ? + AND k.kontakt = ?"; + + if((isset($code)) && (isset($email))) + { + $result = $this->db->query($query, array($code, $email)); + } - return $this->db->get()->result_object(); + return $result; } /** * */ - private function _getPersonByCode($code) + private function _getPersonByCode($code = NULL) { - $query = $this->db->get_where('public.tbl_person', array('zugangscode' => $code)); - return $query->result_object(); + $result = NULL; + $query = "SELECT * + FROM public.tbl_person p + WHERE p.zugangscode = ?"; + + if(isset($code)) + { + $result = $this->db->query($query, array($code)); + } + + return $result; } /**