More work on image processing

This commit is contained in:
sctn4elk 2024-05-09 20:25:56 -05:00
parent 97207ea536
commit 79ad9030bc
7 changed files with 163 additions and 188 deletions

View File

@ -6,10 +6,14 @@
* @author Mike Howard * @author Mike Howard
*/ */
class BaseController { class BaseController {
public $name; public $basename;
public $requestMethod;
public $arrQueryStringParams;
public $strErrorDesc;
public $strErrorHeader;
public function __construct() { public function __construct() {
$this->name = 'BaseController'; $this->basename = 'BaseController';
} }
public static function create() { return new self(); } public static function create() { return new self(); }
@ -21,6 +25,36 @@ class BaseController {
{ {
$this->sendOutput('', array('HTTP/1.1 404 Non-Existant method or inaccessible method called')); $this->sendOutput('', array('HTTP/1.1 404 Non-Existant method or inaccessible method called'));
} }
public function checkRequestType($request)
{
$response = 'false';
if (strtoupper($this->requestMethod) == $request) {
$response = 'true';
}
return $response;
}
public function unprocessableEntityResponse()
{
$response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Payload';
$response['body'] = json_encode([
'error' => 'Invalid input'
]);
$this->strErrorDesc = 'Unprocessable Payload';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Payload';
return $response;
}
public function notFoundResponse()
{
$response['status_code_header'] = 'HTTP/1.1 404 Entity Not Found';
$response['body'] = null;
$this->strErrorDesc = 'Request Entity Not Found';
$this->strErrorHeader = 'HTTP/1.1 422 Entity Not Found';
return $response;
}
/** /**
* Get URI elements. * Get URI elements.
* *

View File

@ -1,9 +1,5 @@
<?php <?php
header("Content-Type: application/json"); header("Content-Type: application/json");
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
*/
/** /**
* Description of CustomerController * Description of CustomerController
@ -16,12 +12,7 @@ class CustomerController extends BaseController {
/** /**
* "/customer/list" Endpoint - Get list of users * "/customer/list" Endpoint - Get list of users
*/ */
private $customerModel; private $customerModel;
private $requestMethod;
private $arrQueryStringParams;
private $strErrorDesc;
private $strErrorHeader;
public $action; public $action;
function __construct() { function __construct() {
@ -183,15 +174,6 @@ class CustomerController extends BaseController {
return $response; return $response;
} }
private function checkRequestType($request)
{
$response = 'false';
if (strtoupper($this->requestMethod) == $request) {
$response = 'true';
}
return $response;
}
private function validatePerson($input) private function validatePerson($input)
{ {
$validtion = false; $validtion = false;
@ -203,24 +185,4 @@ class CustomerController extends BaseController {
} }
return $validtion; return $validtion;
} }
private function unprocessableEntityResponse()
{
$response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Payload';
$response['body'] = json_encode([
'error' => 'Invalid input'
]);
$this->strErrorDesc = 'Unprocessable Payload';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Payload';
return $response;
}
private function notFoundResponse()
{
$response['status_code_header'] = 'HTTP/1.1 404 Entity Not Found';
$response['body'] = null;
$this->strErrorDesc = 'Request Entity Not Found';
$this->strErrorHeader = 'HTTP/1.1 422 Entity Not Found';
return $response;
}
} }

View File

@ -9,11 +9,6 @@ header("Content-Type: application/json");
class ImageController extends BaseController { class ImageController extends BaseController {
private $imageModel; private $imageModel;
private $requestMethod;
private $arrQueryStringParams;
private $strErrorDesc;
private $strErrorHeader;
public $action; public $action;
function __construct() { function __construct() {
@ -79,14 +74,26 @@ class ImageController extends BaseController {
if (! $this->validatePerson($input)) { if (! $this->validatePerson($input)) {
return $this->unprocessableEntityResponse(); return $this->unprocessableEntityResponse();
} }
//remove customer_id field so it doesn't break
unset($input['customer_id']);
$response = $this->customerModel->insertCustomer($input); $response = $this->insertImage();
return $response; return $response;
} }
private function insertImage() {
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
header("Content-Type: application/json");
if (isset($_FILES['image'])) {
$uploadImage = $this->processImage();
$success = false;
if (!$uploadImage['error']) {
}
}
}
private function processImage() { private function processImage() {
$error = false; $error = false;
$msg = null; $msg = null;
@ -123,47 +130,4 @@ class ImageController extends BaseController {
]; ];
return $imageInfo; return $imageInfo;
} }
private function insert() {
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
header("Content-Type: application/json");
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_FILES['image'])) {
$uploadImage = $this->processImage();
$success = false;
if (!$uploadImage['error']) {
// table name for admin profiles
$query = "INSERT INTO " . $this->imageTable;
$query .= " (filename, filepath) VALUES (?,?)";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("ss", $uploadImage['filename'], $uploadImage['filepath']);
if ($stmt->execute()) {
$success = true;
$stmt->close();
}
}
$data = [
'Errormsg' => $uploadImage['msg'] ?? '',
'success' => $success
];
return json_encode($data);
}
}
}
private function notFoundResponse()
{
$response['status_code_header'] = 'HTTP/1.1 404 Entity Not Found';
$response['body'] = null;
$this->strErrorDesc = 'Request Entity Not Found';
$this->strErrorHeader = 'HTTP/1.1 422 Entity Not Found';
return $response;
}
} }

View File

@ -30,51 +30,7 @@
require_once PROJECT_ROOT_PATH . "/Model/Database.php"; require_once PROJECT_ROOT_PATH . "/Model/Database.php";
class CustomerModel extends Database { class CustomerModel extends Database {
private $params = array(); use ModelTraits;
/*
* @assert ('name', 'value')
*/
public function __set($name, $value)
{
//echo "Setting '$name' to '$value'\n";
$this->params[$name] = $value;
}
/*
* @assert ('name') == 'value'
*/
public function __get($name)
{
//echo "Getting '$name'\n";
if (array_key_exists($name, $this->params)) {
return $this->params[$name];
}
$trace = debug_backtrace();
trigger_error(
'Undefined property via __get(): ' . $name .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return null;
}
/*
* @assert ('name') == 'true'
* @assert ('test') == 'false'
*/
public function __isset($name)
{
//echo "Is '$name' set?\n";
return isset($this->params[$name]);
}
public function __unset($name)
{
//echo "Unsetting '$name'\n";
unset($this->params[$name]);
}
public function findAllCustomers() public function findAllCustomers()
{ {

View File

@ -1,10 +1,4 @@
<?php <?php
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
*/
/** /**
* Description of Database * Description of Database
* *
@ -67,9 +61,8 @@ class Database {
public function processStatement($query = "") public function processStatement($query = "")
{ {
try { try {
//return var_dump($query);
//Prepare the statement //Prepare the statement
$stmt = $this->connection->stmt_init(); //$stmt = $this->connection->stmt_init();
$stmt = $this->connection->prepare($query); $stmt = $this->connection->prepare($query);
if($stmt === false) { if($stmt === false) {
throw New Exception("Unable to prepare the statement: " . $query); throw New Exception("Unable to prepare the statement: " . $query);
@ -93,4 +86,32 @@ class Database {
} }
return false; return false;
} }
public function processImage($query = "" , $params = []) {
try {
$stmt = $this->connection->prepare($query);
if($stmt === false) {
throw New Exception("Unable to prepare the statement: " . $query);
}
$stmt->bind_param("ss", $params['filename'], $params['filepath']);
$result = $stmt->execute();
if($result === false) {
throw New Exception("Unable to execute the statement: " . $query);
}
$rowCount = $this->connection->affected_rows;
if($rowCount < 1)
{
throw New Exception("Statement did not return any rows: " . $query);
}
$stmt->close();
return $rowCount;
} catch(Exception $e) {
throw New Exception( $e->getMessage() );
}
return false;
}
} }

View File

@ -11,51 +11,35 @@
* @author SCTN4 * @author SCTN4
*/ */
class ImageModel extends Database { class ImageModel extends Database {
use ModelTraits;
private $params = array(); public function insertImage($inputModel)
/*
* @assert ('name', 'value')
*/
public function __set($name, $value)
{ {
//echo "Setting '$name' to '$value'\n"; // table name for admin profiles
$this->params[$name] = $value; $query = "INSERT INTO " . $this->imageTable;
} $query .= " (filename, filepath) VALUES (?,?)";
}
/* $data = [
* @assert ('name') == 'value' 'Errormsg' => $uploadImage['msg'] ?? '',
*/ 'success' => $success
public function __get($name) ];
{
//echo "Getting '$name'\n"; return json_encode($data);
if (array_key_exists($name, $this->params)) {
return $this->params[$name]; //return var_dump($jsonPayLoad);
$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]] . ")";
$trace = debug_backtrace(); //return var_dump($query);
trigger_error(
'Undefined property via __get(): ' . $name . $rowCount = $this->processImage($query);
' in ' . $trace[0]['file'] . return $rowCount;
' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return null;
} }
/*
* @assert ('name') == 'true'
* @assert ('test') == 'false'
*/
public function __isset($name)
{
//echo "Is '$name' set?\n";
return isset($this->params[$name]);
}
public function __unset($name)
{
//echo "Unsetting '$name'\n";
unset($this->params[$name]);
}
} }

54
Model/ModelTraits.php Normal file
View File

@ -0,0 +1,54 @@
<?php
/**
* Description of ModelTraits
*
* @author SCTN4
*/
Trait ModelTraits {
private $params = array();
/*
* @assert ('name', 'value')
*/
public function __set($name, $value)
{
//echo "Setting '$name' to '$value'\n";
$this->params[$name] = $value;
}
/*
* @assert ('name') == 'value'
*/
public function __get($name)
{
//echo "Getting '$name'\n";
if (array_key_exists($name, $this->params)) {
return $this->params[$name];
}
$trace = debug_backtrace();
trigger_error(
'Undefined property via __get(): ' . $name .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_NOTICE);
return null;
}
/*
* @assert ('name') == 'true'
* @assert ('test') == 'false'
*/
public function __isset($name)
{
//echo "Is '$name' set?\n";
return isset($this->params[$name]);
}
public function __unset($name)
{
//echo "Unsetting '$name'\n";
unset($this->params[$name]);
}
}