diff --git a/Controller/API/AttributeController.php b/Controller/API/AttributeController.php index 7f538ad..0d9c73c 100644 --- a/Controller/API/AttributeController.php +++ b/Controller/API/AttributeController.php @@ -32,15 +32,15 @@ class AttributeController extends BaseController { break; case "insert": - $response = $this->insertAttribute(); + $response = $this->insertMemberAttribute(); break; case "update": - $response = $this->updateAttribute(); + $response = $this->updateMemberAttribute(); break; case "delete": - $response = $this->deleteAttribute(); + $response = $this->deleteMemberAttribute(); break; default: @@ -97,4 +97,62 @@ class AttributeController extends BaseController { return $response; } + + private function updateMemberAttribute(){ + if ($this->checkRequestType('PUT') == 'false') { + $response = $this->unprocessableRequestResponse("updateMemberAttribute"); + return $response; + } + $jsonPayload = file_get_contents('php://input'); + $input = json_decode($jsonPayload, TRUE); + + if (! $this->validateAttribute($input)) { + return $this->unprocessableEntityResponse("validateAttribute"); + } + + if ($input['LOYALTY_MEMBER_ID'] != null) { + $this->attributeModel->memberId = $input['LOYALTY_MEMBER_ID']; + $this->attributeModel->attributeId = $input['LOYALTY_ATTRIBUTE_ID']; + $result = $this->attributeModel->findMemberAttribute(); + if (! $result) { + return $this->notFoundResponse("attributeModel->findMemberAttribute"); + } + + $response = $this->attributeModel->updateMemberAttribute($input); + unset($this->attributeModel->memberId); + } else { + return $this->notFoundResponse("attributeModel->LOYALTY_MEMBER_ID"); + } + return $response; + } + + private function insertMemberAttribute(){ + if ($this->checkRequestType('POST') == 'false') { + $response = $this->unprocessableRequestResponse("insertMemberAttribute"); + return $response; + } + + return $this->unprocessableRequestResponse("insertMemberAttribute Not Implemented"); + } + + private function deleteMemberAttribute(){ + if ($this->checkRequestType('DELETE') == 'false') { + $response = $this->unprocessableRequestResponse("deleteMemberAttribute"); + return $response; + } + + return $this->unprocessableRequestResponse("deleteMemberAttribute Not Implemented"); + } + + private function validateAttribute($input) + { + $validtion = false; + if($input['LOYALTY_MEMBER_ID'] != null){ + $validtion = true; + if($input['LOYALTY_ATTRIBUTE_ID'] == null) { + $validtion = false; + } + } + return $validtion; + } } diff --git a/Controller/API/BaseController.php b/Controller/API/BaseController.php index b7900b0..ad3ef0d 100644 --- a/Controller/API/BaseController.php +++ b/Controller/API/BaseController.php @@ -15,6 +15,9 @@ class BaseController { public function __construct() { $this->basename = 'BaseController'; + $this->strErrorDesc = ''; + $this->strErrorHeader = ''; + $this->strErrorMessage = ''; } public static function create() { return new self(); } diff --git a/Controller/API/CustomerController.php b/Controller/API/CustomerController.php index 1389353..2cfc082 100644 --- a/Controller/API/CustomerController.php +++ b/Controller/API/CustomerController.php @@ -122,7 +122,7 @@ class CustomerController extends BaseController { return $this->unprocessableEntityResponse(); } - //remove customer_id field so it doesn't break + //remove model fields so it doesn't break unset($input['customer_id']); unset($input['image_path']); @@ -150,6 +150,9 @@ class CustomerController extends BaseController { return $this->notFoundResponse("updateCustomer->findByCustomerId"); } + //remove model fields so it doesn't break + unset($input['image_path']); + $response = $this->customerModel->updateCustomer($input); unset($this->customerModel->customerId); } else { diff --git a/Model/AttributeModel.php b/Model/AttributeModel.php index 2303353..4c894ff 100644 --- a/Model/AttributeModel.php +++ b/Model/AttributeModel.php @@ -21,4 +21,60 @@ class AttributeModel extends Database { { return $this->processQuery("SELECT * FROM loyalty_member_view WHERE loyalty_member_id = ?", ["i", $this->memberId]); } + + public function findMemberAttribute() + { + return $this->processQuery("SELECT * FROM loyalty_member_view WHERE loyalty_member_id = ? AND loyalty_attribute_id = ?", ["ii", $this->memberId, $this->attributeId]); + } + + public function insertAttribute($inputModel) + { + /* + { + "loyalty_attribute_id": 6, + "loyalty_attribute_name": "image_path", + "loyalty_attribute_type_id": 4 + } + */ + //return var_dump($inputModel); + $keys = array_keys($inputModel); + $n = count($keys); + + $query = "CALL insert_new_customer_proc ("; + for($i = 0; $i < $n-1; $i++) { + $query .= "'" . $inputModel[$keys[$i]] . "', "; + } + $query .= $inputModel[$keys[$i]] . ")"; + //return var_dump($query); + + $rowCount = $this->processStatement($query); + return $rowCount; + } + + public function updateMemberAttribute($jsonPayLoad) + { + /* + { + "LOYALTY_MEMBER_ID": 1, + "FIRST_NAME": "Mike", + "LAST_NAME": "Howard", + "LOYALTY_ATTRIBUTE_ID": 6, + "LOYALTY_ITEM": "image_path", + "LOYALTY_TYPE": "varchar", + "LOYALTY_VALUE": "http://localhost/CustomerRewardsRESTAPI/public/images/1.jpg" + } + */ + $keys = array_keys($jsonPayLoad); + $n = count($keys); + + $query .= "CALL update_member_attribute_proc (" . $jsonPayLoad[$keys[0]] . ", "; + for($i = 1; $i < $n-1; $i++) { + $query .= "'" . $jsonPayLoad[$keys[$i]] . "', "; + } + $query .= $jsonPayLoad[$keys[$i]] . ")"; + + $rowCount = $this->processStatement($query); + return $rowCount; + } + } diff --git a/Model/CustomerModel.php b/Model/CustomerModel.php index 48a4db3..5e2ff3e 100644 --- a/Model/CustomerModel.php +++ b/Model/CustomerModel.php @@ -48,7 +48,7 @@ class CustomerModel extends Database { $keys = array_keys($inputModel); $n = count($keys); - $query .= "CALL insert_new_customer_proc ("; + $query = "CALL insert_new_customer_proc ("; for($i = 0; $i < $n-1; $i++) { $query .= "'" . $inputModel[$keys[$i]] . "', "; }