From 136aac803da50e99ebae2be37f5b26f224861bd4 Mon Sep 17 00:00:00 2001 From: Joshua Perry <45966243+jpez-development@users.noreply.github.com> Date: Sat, 4 Feb 2023 22:19:29 +0000 Subject: [PATCH] refactored db find --- CSY2028/DatabaseTable.php | 24 +++++++----------------- jobs/Entity/Applicant.php | 2 +- jobs/Entity/Job.php | 2 +- jobs/controllers/Jobs.php | 16 +++++++++++----- jobs/controllers/Portal.php | 12 ++++++------ jobs/controllers/User.php | 2 +- 6 files changed, 27 insertions(+), 31 deletions(-) diff --git a/CSY2028/DatabaseTable.php b/CSY2028/DatabaseTable.php index 56c2997..868ce66 100644 --- a/CSY2028/DatabaseTable.php +++ b/CSY2028/DatabaseTable.php @@ -36,26 +36,16 @@ class DatabaseTable { $this->pdo->prepare('UPDATE '. $this->table .' SET '. \implode(', ', $params) .' WHERE '. $this->pk .' = :primaryKey')->execute($record); } - public function find($column, $value, $column2 = "", $value2 = "", $comparator = "=", $comparator2 = "=", $order = "ASC", $orderColumn = "id") { - if ($column2 == "" || $value2 == "") { - $values = [ - 'value' => $value - ]; - $stmt = $this->pdo->prepare('SELECT * FROM '.$this->table.' WHERE '.$column.' '.$comparator.' :value ORDER BY '.$orderColumn." ".$order); + public function find($columns, $values, $comparators = ['=', '='], $order = "ASC", $orderColumn = "id") { + $string = 'SELECT * FROM '.$this->table.' WHERE '; + for ($i = 0; $i < count($values); $i++) { + $string .= $columns[$i].' '.$comparators[$i].' :value'.$i.' '; + } + $string .= 'ORDER BY '.$orderColumn.' '.$order; + $stmt = $this->pdo->prepare($string); $stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor); $stmt->execute($values); return $stmt->fetchAll(); - } - else { - $values = [ - 'value' => $value, - 'value2' => $value2 - ]; - $stmt = $this->pdo->prepare('SELECT * FROM '.$this->table.' WHERE '.$column.' '.$comparator.' :value AND '.$column2.' '.$comparator2.' :value2 ORDER BY '.$orderColumn." ".$order); - $stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor); - $stmt->execute($values); - return $stmt->fetchAll(); - } } public function findAll() { diff --git a/jobs/Entity/Applicant.php b/jobs/Entity/Applicant.php index 3a3e02d..bd8b0bb 100644 --- a/jobs/Entity/Applicant.php +++ b/jobs/Entity/Applicant.php @@ -14,7 +14,7 @@ class Applicant { } public function getJob() { - return $this->jobsTable->find('id', $this->jobId)[0]; + return $this->jobsTable->find(['id'], ['value0' => $this->jobId])[0]; } } ?> \ No newline at end of file diff --git a/jobs/Entity/Job.php b/jobs/Entity/Job.php index 51e163b..cfb4119 100644 --- a/jobs/Entity/Job.php +++ b/jobs/Entity/Job.php @@ -16,7 +16,7 @@ class Job { } public function getCat() { - return $this->catsTable->find('id', $this->categoryId)[0]; + return $this->catsTable->find(['id'], ['value0' => $this->categoryId])[0]; } } ?> \ No newline at end of file diff --git a/jobs/controllers/Jobs.php b/jobs/controllers/Jobs.php index e16fb00..9451be2 100644 --- a/jobs/controllers/Jobs.php +++ b/jobs/controllers/Jobs.php @@ -14,7 +14,7 @@ class Jobs { } public function home() { - $this->vars['jobs'] = $this->jobsTable->find("closingDate", date("y-m-d"), "", "", ">", "", "DESC", "closingDate"); + $this->vars['jobs'] = $this->jobsTable->find(["closingDate"], ['value0' => date('y-m-d')], ['>'], "DESC", "closingDate"); return ['template' => 'home.html.php', 'title' => 'Jo\'s Jobs- Home', 'vars' => $this->vars @@ -22,16 +22,22 @@ class Jobs { } public function category() { - $cat = $this->catsTable->find('name', $_GET['page']); + $cat = $this->catsTable->find(['name'], ['value0' => $_GET['page']]); if ($cat == null) { return $this->notFound(); } else { if (isset($_GET['filter'])) { - $this->vars['jobs'] = $this->jobsTable->find('categoryId', $cat[0]->id, "location", $_GET['filter']); + $columns = ['categoryId', "location", 'closingDate']; + $values = ['value0' => $cat[0]->id, + 'value1' => $_GET['filter'], + 'value2' => date('y-m-d') + ]; + $comparators = ["=","=",">"]; + $this->vars['jobs'] = $this->jobsTable->find($columns, $values, $comparators); } else { - $this->vars['jobs'] = $this->jobsTable->find('categoryId', $cat[0]->id, "closingDate", date("y-m-d"), "=", ">"); + $this->vars['jobs'] = $this->jobsTable->find(['categoryId', "closingDate"], ["value0" => $cat[0]->id, "value1" => date("y-m-d")], ["=", ">"]); } $this->vars['heading'] = $cat[0]->name; @@ -59,7 +65,7 @@ class Jobs { } public function apply() { - $this->vars['job'] = $this->jobsTable->find('id', $_GET['id'])[0]; + $this->vars['job'] = $this->jobsTable->find(['id'], ["value0" => $_GET['id']])[0]; return ['template' => 'apply.html.php', 'title' => 'Jo\'s Jobs- Apply', 'vars' => $this->vars]; diff --git a/jobs/controllers/Portal.php b/jobs/controllers/Portal.php index a3a446c..dd2efed 100644 --- a/jobs/controllers/Portal.php +++ b/jobs/controllers/Portal.php @@ -18,15 +18,15 @@ class Portal { $this->vars['table'] = 'job_table.html.php'; if (isset($_GET['filter'])) { if ($_SESSION['userType'] == 'client') { - $this->vars['jobs'] = $this->jobsTable->find('clientId', $_SESSION['loggedin'], "categoryId", $_GET['filter']); + $this->vars['jobs'] = $this->jobsTable->find(['clientId', 'categoryId'], ['value0' => $_SESSION['loggedin'],'value1' => $_GET['filter']]); } else { - $this->vars['jobs'] = $this->jobsTable->find("categoryId", $_GET['filter']); + $this->vars['jobs'] = $this->jobsTable->find(["categoryId"], ['value0' => $_GET['filter']]); } } else { if ($_SESSION['userType'] == 'client') { - $this->vars['jobs'] = $this->jobsTable->find('clientId', $_SESSION['loggedin']); + $this->vars['jobs'] = $this->jobsTable->find(['clientId'], ['value0' => $_SESSION['loggedin']]); } else { $this->vars['jobs'] = $this->jobsTable->findAll(); @@ -66,7 +66,7 @@ class Portal { public function applicants() { $this->vars['table'] = 'applicant_table.html.php'; - $this->vars['apps'] = $this->appsTable->find('jobId', $_GET['app_id']); + $this->vars['apps'] = $this->appsTable->find(['jobId'], ['value0' => $_GET['app_id']]); return ['template' => 'portal.html.php', 'title' => 'Jo\'s Jobs- Applicants', 'vars' => $this->vars]; @@ -74,10 +74,10 @@ class Portal { public function edit() { //TODO: finish this function if (isset($_GET['job_id'])) { - $this->vars['job'] = $this->jobsTable->find("id", $_GET['jod_id']); + $this->vars['job'] = $this->jobsTable->find(["id"], ['value0' => $_GET['jod_id']]); } if (isset($_GET['cat_id'])) { - $this->vars['cat'] = $this->catsTable->find("id", $_GET['cat_id']); + $this->vars['cat'] = $this->catsTable->find(["id"], ['value0' => $_GET['cat_id']]); } } diff --git a/jobs/controllers/User.php b/jobs/controllers/User.php index 777b61c..01bd25c 100644 --- a/jobs/controllers/User.php +++ b/jobs/controllers/User.php @@ -20,7 +20,7 @@ class User { public function loginSubmit() { if ($_POST['username'] != '' && $_POST['password'] != '') { - $user = $this->usersTable->find("username", $_POST['username']); + $user = $this->usersTable->find(["username"], ['value0' => $_POST['username']]); if (count($user) > 0 && $_POST['submit'] == 'Register') { $this->vars['response'] = "Account already exists";