From e5a3069ebdccb9fbe0cb8a7631b7de677f08e22d Mon Sep 17 00:00:00 2001
From: Joshua Perry <45966243+jpez-development@users.noreply.github.com>
Date: Mon, 23 Jan 2023 16:08:51 +0000
Subject: [PATCH] fixed error with DatabaseTable
---
CSY2028/DatabaseTable.php | 22 ++++++++++++++--------
jobs/Routes.php | 4 ++--
jobs/controllers/Jobs.php | 30 +++++++++++++++++++++++++++---
pages/sales.php | 14 +-------------
4 files changed, 44 insertions(+), 26 deletions(-)
diff --git a/CSY2028/DatabaseTable.php b/CSY2028/DatabaseTable.php
index ed1f060..1002e48 100644
--- a/CSY2028/DatabaseTable.php
+++ b/CSY2028/DatabaseTable.php
@@ -25,27 +25,33 @@ class DatabaseTable {
$keys = \array_keys($record);
$columns = \implode(', ', $keys);
$values = \implode(', :', $keys);
- startDB()->prepare('INSERT INTO '. $this->table . ' (' . $columns . ') VALUES (:' . $values . ')')->execute($record);
+ $this->startDB()->prepare('INSERT INTO '. $this->table . ' (' . $columns . ') VALUES (:' . $values . ')')->execute($record);
}
private function update($record) {
- $params = [];
- foreach ($record as $key => $value) {
+ $params = [];
+ foreach ($record as $key => $value) {
$params[] = $key . ' = :' .$key;
- }
- $record['primaryKey'] = $record[$this->pk];
- startDB()->prepare('UPDATE '. $this->table .' SET '. \implode(', ', $params) .' WHERE '. $this->pk .' = :primaryKey')->execute($record);
+ }
+ $record['primaryKey'] = $record[$this->pk];
+ $this->startDB()->prepare('UPDATE '. $this->table .' SET '. \implode(', ', $params) .' WHERE '. $this->pk .' = :primaryKey')->execute($record);
}
public function find($column, $value) {
$values = [
'value' => $value
];
- return startDB()->prepare('SELECT * FROM '. $this->table . ' WHERE '. $field . ' = :value')->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor)->execute($values)->fetchAll();
+ $stmt = $this->startDB()->prepare('SELECT * FROM '. $this->table . ' WHERE '. $column . ' = :value');
+ $stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor);
+ $stmt->execute($values);
+ return $stmt->fetchAll();
}
public function findAll() {
- return startDB()->prepare('SELECT * FROM ' . $this->table)->execute()->fetchAll();
+ $stmt = $this->startDB()->prepare('SELECT * FROM ' . $this->table);
+ $stmt->setFetchMode(\PDO::FETCH_CLASS, $this->entityClass, $this->entityConstructor);
+ $stmt->execute();
+ return $stmt->fetchAll();
}
public function delete($column, $value) {
diff --git a/jobs/Routes.php b/jobs/Routes.php
index 6c3ebbd..a69113f 100644
--- a/jobs/Routes.php
+++ b/jobs/Routes.php
@@ -3,12 +3,12 @@ namespace jobs;
class Routes implements \CSY2028\Routes {
public function getController($name) {
$catsTable = new \CSY2028\DatabaseTable('category', 'id', '\jobs\Entity\Category');
- $jobsTable = new \CSY2028\DatabaseTable('jobs', 'id', '\jobs\Entity\Job', [$catsTable]);
+ $jobsTable = new \CSY2028\DatabaseTable('job', 'id', '\jobs\Entity\Job', [$catsTable]);
$appsTable = new \CSY2028\DatabaseTable('applicants', 'id', '\jobs\Entity\Applicant', [$jobsTable]);
$controllers = [];
//TODO: Add Controllers
- $controllers['jobs'] = new \jobs\controllers\Jobs($jobsTable);
+ $controllers['jobs'] = new \jobs\controllers\Jobs($jobsTable, $catsTable);
return $controllers[$name];
}
diff --git a/jobs/controllers/Jobs.php b/jobs/controllers/Jobs.php
index 7cb37ca..facdb4d 100644
--- a/jobs/controllers/Jobs.php
+++ b/jobs/controllers/Jobs.php
@@ -1,15 +1,39 @@
jobTable = $jobTable;
+ public function __construct(\CSY2028\DatabaseTable $jobsTable, \CSY2028\DatabaseTable $catsTable) {
+ $this->jobsTable = $jobsTable;
+ $this->catsTable = $catsTable;
}
public function home() {
return ['template' => 'home.html.php',
'title' => 'Jo\'s Jobs- Home',
+ 'vars' => ['cats' => $this->catsTable->findAll()]
+ ];
+ }
+
+ public function category() {
+ $cat = $this->catsTable->find('name', $_GET['page']);
+ if ($cat == null) {
+ return $this->notFound();
+ }
+ else {
+ return ['template' => 'category.html.php',
+ 'title' => 'Jo\'s Jobs- '. $_GET['page'],
+ 'vars' => ['jobs' => $this->jobsTable->find('categoryId', $cat[0]->id),
+ 'cats' => $this->catsTable->findAll(),
+ 'heading' => $cat[0]->name]
+ ];
+ }
+ }
+
+ public function notFound() {
+ return ['template' => 'notFound.html.php',
+ 'title' => 'Jo\'s Jobs- 404 Not Found',
'vars' => []
];
}
diff --git a/pages/sales.php b/pages/sales.php
index 29fe78e..ae8476d 100644
--- a/pages/sales.php
+++ b/pages/sales.php
@@ -35,19 +35,7 @@