Continue work on CRUD operations

This commit is contained in:
sctn4elk 2024-04-30 22:54:04 -05:00
parent 2313766b58
commit 6e50ad5eab
3 changed files with 84 additions and 50 deletions

View File

@ -44,7 +44,7 @@ class CustomerController extends BaseController{
break;
case "insert":
$customerModel->first = $arrQueryStringParams['first'];
/*$customerModel->first = $arrQueryStringParams['first'];
$customerModel->last = $arrQueryStringParams['last'];
$customerModel->email = $arrQueryStringParams['email'];
$customerModel->phone = $arrQueryStringParams['phone'];
@ -53,11 +53,11 @@ class CustomerController extends BaseController{
$customerModel->city = $arrQueryStringParams['city'];
$customerModel->state = $arrQueryStringParams['state'];
$customerModel->zip = $arrQueryStringParams['zip'];
$customerModel->loyalty = $arrQueryStringParams['loyalty'];
$customerModel->loyalty = $arrQueryStringParams['loyalty'];*/
$arrCustomer = $customerModel->insertCustomer();
$response = $this->insertCustomer();
unset($customerModel->first);
/*unset($customerModel->first);
unset($customerModel->last);
unset($customerModel->email);
unset($customerModel->phone);
@ -66,15 +66,18 @@ class CustomerController extends BaseController{
unset($customerModel->city);
unset($customerModel->state);
unset($customerModel->zip);
unset($customerModel->loyalty);
unset($customerModel->loyalty);*/
break;
case "update":
$arrCustomer = $customerModel->updateCustomer($arrQueryStringParams);
$response = $this->updateCustomer();
/*$arrCustomer = $this->customerModel->updateCustomer($arrQueryStringParams);*/
break;
case "delete":
$arrCustomer = $customerModel->deleteCustomer($arrQueryStringParams);
/*$arrCustomer = $this->customerModel->deleteCustomer($arrQueryStringParams);*/
$this->customerModel->customerId = $this->arrQueryStringParams['customer_id'];
$response = $this->deleteCustomer();
break;
default:
@ -128,8 +131,8 @@ class CustomerController extends BaseController{
return;
}
if (isset($this->arrQueryStringParams['id'])) {
$this->customerModel->customerId = $this->arrQueryStringParams['id'];
if (isset($this->arrQueryStringParams['customerId'])) {
$this->customerModel->customerId = $this->arrQueryStringParams['customerId'];
$response = $this->customerModel->findByCustomerId();
unset($this->customerModel->customerId);
} else {
@ -149,52 +152,71 @@ class CustomerController extends BaseController{
return $response;
}
private function createUserFromRequest()
private function insertCustomer()
{
if ($this->checkRequestType('POST') == 'false') {
$this->strErrorDesc = 'Request Method not supported for processAction';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
return;
}
// reads the raw POST data and returns it as a string.
$jsonPayload = (array) json_decode(file_get_contents('php://input'), TRUE);
if (! $this->validatePerson($jsonPayload)) {
return $this->unprocessableEntityResponse();
}
$response = $this->customerModel->insertCustomer($jsonPayload);
return $response;
}
private function updateCustomer()
{
if ($this->checkRequestType('PUT') == 'false') {
$this->strErrorDesc = 'Request Method not supported for processAction';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
return;
}
if (isset($this->arrQueryStringParams['customerId'])) {
$this->customerModel->customerId = $this->arrQueryStringParams['customerId'];
$result = $this->customerModel->findByCustomerId();
if (! $result) {
return $this->notFoundResponse();
}
$input = (array) json_decode(file_get_contents('php://input'), TRUE);
if (! $this->validatePerson($input)) {
return $this->unprocessableEntityResponse();
}
$this->personGateway->insert($input);
$response['status_code_header'] = 'HTTP/1.1 201 Created';
$response['body'] = null;
$response = $this->customerModel->updateCustomer($input);
unset($this->customerModel->customerId);
} else {
return $this->notFoundResponse();
}
return $response;
}
private function updateUserFromRequest($id)
private function deleteCustomer()
{
$result = $this->personGateway->find($id);
if (isset($this->arrQueryStringParams['customerId'])) {
$this->customerModel->customerId = $this->arrQueryStringParams['customerId'];
$result = $this->customerModel->findByCustomerId();
if (! $result) {
return $this->notFoundResponse();
}
$input = (array) json_decode(file_get_contents('php://input'), TRUE);
if (! $this->validatePerson($input)) {
return $this->unprocessableEntityResponse();
}
$this->personGateway->update($id, $input);
$response['status_code_header'] = 'HTTP/1.1 200 OK';
$response['body'] = null;
return $response;
}
private function deleteUser($id)
{
$result = $this->personGateway->find($id);
if (! $result) {
$response = $this->customerModel->deleteCustomer();
unset($this->customerModel->customerId);
} else {
return $this->notFoundResponse();
}
$this->personGateway->delete($id);
$response['status_code_header'] = 'HTTP/1.1 200 OK';
$response['body'] = null;
return $response;
}
private function validatePerson($input)
{
if (! isset($input['firstname'])) {
if (! isset($input['first'])) {
return false;
}
if (! isset($input['lastname'])) {
if (! isset($input['last'])) {
return false;
}
return true;

View File

@ -76,29 +76,40 @@ class CustomerModel extends Database {
return $this->processQuery("SELECT * FROM customer_view WHERE customer_id = ?", ["i", $this->customerId]);
}
public function insertCustomer()
public function insertCustomer(Array $jsonPayLoad)
{
$rowCount = $this->processStatement("CALL insert_new_customer_proc(?,?,?,?,?,?,?,?,?,?)",
[$this->first,
$this->last,
$this->email,
$this->phone,
$this->birthday,
$this->street,
$this->city,
$this->state,
$this->zip,
$this->loyalty]);
[$jsonPayLoad->first,
$jsonPayLoad->last,
$jsonPayLoad->email,
$jsonPayLoad->phone,
$jsonPayLoad->birthday,
$jsonPayLoad->street,
$jsonPayLoad->city,
$jsonPayLoad->state,
$jsonPayLoad->zip,
$jsonPayLoad->loyalty]);
return $rowCount;
}
public function updateCustomer($id, $param_name, $param_value)
public function updateCustomer(Array $jsonPayLoad)
{
return $this->processStatement("UPDATE customer SET ? = ? WHERE customer_id = ?", [$param_name, $param_value, $id]);
$rowCount = $this->processStatement("CALL update_customer_proc(?,?,?,?,?,?,?,?,?,?)",
[$jsonPayLoad->first,
$jsonPayLoad->last,
$jsonPayLoad->email,
$jsonPayLoad->phone,
$jsonPayLoad->birthday,
$jsonPayLoad->street,
$jsonPayLoad->city,
$jsonPayLoad->state,
$jsonPayLoad->zip,
$jsonPayLoad->loyalty]);
return $rowCount;
}
public function deleteCustomer($id)
public function deleteCustomer()
{
return $this->processStatement("DELETE FROM customer WHERE customer_id = ?", [$id]);
return $this->processStatement("DELETE FROM customer WHERE customer_id = ?", [$this->customerId]);
}
}

View File

@ -15,8 +15,9 @@ header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers
*/
require __DIR__ . "/include/bootstrap.php";
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = explode( '/', $uri );
$requestMethod = filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_STRING);
$parsedUri = parse_url($requestMethod, PHP_URL_PATH);
$uri = explode( '/', $parsedUri );
//Set uri module location position to 1 for production, 2 for testing
$uri_pos = 2;