basename = "CustomerController"; } public function processAction() { $this->strErrorDesc = ''; $this->strErrorHeader = ''; $this->strErrorMessage = ''; try { $this->requestMethod = $this->getServerRequestMethod(); $this->customerModel = new CustomerModel(); switch($this->action) { case "select": $response = $this->selectAction(); break; case "insert": $response = $this->insertCustomer(); break; case "update": $response = $this->updateCustomer(); break; case "delete": $response = $this->deleteCustomer(); break; default: $response = $this->unprocessableRequestResponse("processAction"); break; } $responseData = json_encode($response); } catch (Error $e) { $this->internalErrorResponse($e); } // send output if (!$this->strErrorDesc) { $this->sendOutput( $responseData, array('Content-Type: application/json', 'HTTP/1.1 200 OK') ); } else { $this->sendOutput(json_encode( array('error' => $this->strErrorDesc, 'message' => $this->strErrorMessage, 'controller' => $this->basename)), array('Content-Type: application/json', $this->strErrorHeader) ); } } private function selectAction(){ if ($this->checkRequestType('GET') == 'false') { $response = $this->unprocessableRequestResponse("selectAction"); return $response; } $this->arrQueryStringParams = $this->getQueryStringParams(); if (isset($this->arrQueryStringParams['customer_id'])) { $response = $this->selectByIdAction(); } else { $this->customerModel->limit = 20; if (isset($this->arrQueryStringParams['limit'])) { $this->customerModel->limit = $this->arrQueryStringParams['limit']; } $response = $this->customerModel->findAllCustomers(); unset($this->customerModel->limit); } return $response; } private function selectByIdAction(){ if (isset($this->arrQueryStringParams['customer_id'])) { $this->customerModel->customerId = $this->arrQueryStringParams['customer_id']; $response = $this->customerModel->findByCustomerId(); unset($this->customerModel->customerId); } else { $response = $this->notFoundResponse("selectByIdAction"); } return $response; } private function insertCustomer() { if ($this->checkRequestType('POST') == 'false') { $response = $this->unprocessableRequestResponse("insertCustomer"); return $response; } // reads the raw POST data and returns it as a string. $jsonPayload = file_get_contents('php://input'); $input = json_decode($jsonPayload, TRUE); if (! $this->validatePerson($input)) { return $this->unprocessableEntityResponse(); } //remove customer_id field so it doesn't break unset($input['customer_id']); unset($input['image_path']); $response = $this->customerModel->insertCustomer($input); return $response; } private function updateCustomer() { if ($this->checkRequestType('PUT') == 'false') { $response = $this->unprocessableRequestResponse("updateCustomer"); return $response; } $jsonPayload = file_get_contents('php://input'); $input = json_decode($jsonPayload, TRUE); if (! $this->validatePerson($input)) { return $this->unprocessableEntityResponse("validatePerson"); } if ($input['customer_id'] != null) { $this->customerModel->customerId = $input['customer_id']; $result = $this->customerModel->findByCustomerId(); if (! $result) { return $this->notFoundResponse("updateCustomer->findByCustomerId"); } $response = $this->customerModel->updateCustomer($input); unset($this->customerModel->customerId); } else { return $this->notFoundResponse("updateCustomer->customer_id"); } return $response; } private function deleteCustomer() { if ($this->checkRequestType('DELETE') == 'false') { $response = $this->unprocessableRequestResponse("deleteCustomer"); return $response; } $this->arrQueryStringParams = $this->getQueryStringParams(); if (isset($this->arrQueryStringParams['customerId'])) { $this->customerModel->customerId = $this->arrQueryStringParams['customerId']; $result = $this->customerModel->findByCustomerId(); if (!$result) { return $this->notFoundResponse("deleteCustomer->findByCustomerId"); } $response = $this->customerModel->deleteCustomer(); unset($this->customerModel->customerId); } else { return $this->notFoundResponse("deleteCustomer->customer_id"); } return $response; } private function validatePerson($input) { $validtion = false; if($input['customer_name_first'] != null){ $validtion = true; if($input['customer_name_last'] == null) { $validtion = false; } } return $validtion; } }