From ba6f07dd56c6abad79b6dbcf7b989cb77195f70b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jomana=20K=C3=A4mpf?= Date: Wed, 9 Oct 2024 11:41:28 +0200 Subject: [PATCH 1/6] fix: fixed modify methode of postgresql class --- .../databases_connectors/postgresql/postgresql.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/models/databases_connectors/postgresql/postgresql.php b/src/models/databases_connectors/postgresql/postgresql.php index 52fb02c..512e172 100644 --- a/src/models/databases_connectors/postgresql/postgresql.php +++ b/src/models/databases_connectors/postgresql/postgresql.php @@ -26,19 +26,14 @@ public function modify(string $squery, array $args = []) { if ($args) { $statement = $this->db->prepare($squery); - $statement->bindParam(':id', $args[':id']); foreach ($args as $key => $value) { - if (is_int($value)) { - $statement->bindParam(':' . $key, $value); - } else { - $statement->bindParam(':' . $key, $value); - } + $statement->bindParam( $key, $value); } $statement->execute(); } else { $this->db->exec($squery); } - + return; } } From 02f808530b3137c1413acb0dbcb801f6db85268c Mon Sep 17 00:00:00 2001 From: Joumoune Date: Wed, 9 Oct 2024 11:44:18 +0200 Subject: [PATCH 2/6] feat: Implement delete an exercise --- public/index.php | 4 ++++ src/controllers/exercise_controller.php | 10 ++++++++++ src/models/databases_connectors/databases_access.php | 4 ++++ .../postgresql/postgresql_access.php | 11 +++++++++++ src/models/exercise.php | 10 ++++++++++ 5 files changed, 39 insertions(+) diff --git a/public/index.php b/public/index.php index 988f091..0f98a7b 100644 --- a/public/index.php +++ b/public/index.php @@ -30,6 +30,10 @@ function getRedirection($redirect_uri) { + if (preg_match("/^\/exercises\/([0-9]+)$/", $redirect_uri, $str)) { + deleteExercise($str[1]); + return; + } switch ($redirect_uri) { case '/': home(); diff --git a/src/controllers/exercise_controller.php b/src/controllers/exercise_controller.php index 49e9738..b07ad37 100644 --- a/src/controllers/exercise_controller.php +++ b/src/controllers/exercise_controller.php @@ -12,3 +12,13 @@ function createExercise() $exercise = Exercises::create($_POST['exercise_title']); header('Location: /exercises/' . $exercise->getId() . '/fields'); } + +function deleteExercise($id) +{ + $exercise = new Exercises($id); + if ($exercise->getExerciseStatus() == Status::Building->value || $exercise->getExerciseStatus() == Status::Closed->value) + { + $exercise->delete(); + } + header('Location: /exercises'); +} diff --git a/src/models/databases_connectors/databases_access.php b/src/models/databases_connectors/databases_access.php index 8e319e4..26dd8dc 100644 --- a/src/models/databases_connectors/databases_access.php +++ b/src/models/databases_connectors/databases_access.php @@ -9,4 +9,8 @@ public function createExercise(string $title): int; public function getExerciseTitle(int $id): string; public function getExercises(int $status = -1): array; + + public function deleteExercise(int $id): void; + + public function getExerciseStatus(int $id): string; } diff --git a/src/models/databases_connectors/postgresql/postgresql_access.php b/src/models/databases_connectors/postgresql/postgresql_access.php index 01f13bd..b0759ce 100644 --- a/src/models/databases_connectors/postgresql/postgresql_access.php +++ b/src/models/databases_connectors/postgresql/postgresql_access.php @@ -29,6 +29,12 @@ public function getExerciseTitle(int $id): string return $result[0]['title']; } + public function getExerciseStatus(int $id): string + { + $result = $this->postgresql->select('SELECT status FROM exercises WHERE id = :id', [':id' => $id]); + return $result[0]['status']; + } + public function getExercises(int $status = -1): array { if ($status < 0) { @@ -37,6 +43,11 @@ public function getExercises(int $status = -1): array return $this->postgresql->select('SELECT id FROM exercises WHERE status = :status', [':status' => $status]); } + public function deleteExercise(int $id): void + { + $this->postgresql->modify('DELETE FROM exercises WHERE id = :id', [':id' => $id]); + } + private function create_db_if_not_exist() { if (count($this->postgresql->select("SELECT 1 FROM information_schema.tables WHERE table_name = 'exercises'")) < 1) { diff --git a/src/models/exercise.php b/src/models/exercise.php index dd34a87..d33db3f 100644 --- a/src/models/exercise.php +++ b/src/models/exercise.php @@ -40,6 +40,16 @@ public function getTitle() return $this->database_access->getexerciseTitle($this->id); } + public function getExerciseStatus() + { + return $this->database_access->getexerciseStatus($this->id); + } + + public function delete() + { + $this->database_access->deleteExercise($this->id); + } + public static function getExercises(Status $status = null) { $database_access = (new DatabasesChoose())->getDatabase(); From 227d20c5d16c9fb395f3ca8d8f32840054a05afb Mon Sep 17 00:00:00 2001 From: Joumoune Date: Wed, 9 Oct 2024 09:50:19 +0000 Subject: [PATCH 3/6] [Action][php-cs-fixer] Fix styling --- src/controllers/exercise_controller.php | 5 ++--- src/models/databases_connectors/postgresql/postgresql.php | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/controllers/exercise_controller.php b/src/controllers/exercise_controller.php index b07ad37..e0529cf 100644 --- a/src/controllers/exercise_controller.php +++ b/src/controllers/exercise_controller.php @@ -16,9 +16,8 @@ function createExercise() function deleteExercise($id) { $exercise = new Exercises($id); - if ($exercise->getExerciseStatus() == Status::Building->value || $exercise->getExerciseStatus() == Status::Closed->value) - { + if ($exercise->getExerciseStatus() == Status::Building->value || $exercise->getExerciseStatus() == Status::Closed->value) { $exercise->delete(); - } + } header('Location: /exercises'); } diff --git a/src/models/databases_connectors/postgresql/postgresql.php b/src/models/databases_connectors/postgresql/postgresql.php index 512e172..24ab270 100644 --- a/src/models/databases_connectors/postgresql/postgresql.php +++ b/src/models/databases_connectors/postgresql/postgresql.php @@ -27,13 +27,13 @@ public function modify(string $squery, array $args = []) if ($args) { $statement = $this->db->prepare($squery); foreach ($args as $key => $value) { - $statement->bindParam( $key, $value); + $statement->bindParam($key, $value); } $statement->execute(); } else { $this->db->exec($squery); } - + return; } } From dbbff080211840af08b60b73589ef3f78223a2ef Mon Sep 17 00:00:00 2001 From: Joumoune Date: Wed, 9 Oct 2024 15:11:49 +0200 Subject: [PATCH 4/6] refactor: fixing typo --- src/models/exercise.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/models/exercise.php b/src/models/exercise.php index d33db3f..9b35c96 100644 --- a/src/models/exercise.php +++ b/src/models/exercise.php @@ -42,7 +42,7 @@ public function getTitle() public function getExerciseStatus() { - return $this->database_access->getexerciseStatus($this->id); + return $this->database_access->getExerciseStatus($this->id); } public function delete() From c59745e8dfa8db97ae457ffe8b52e347463ae148 Mon Sep 17 00:00:00 2001 From: Joumoune Date: Wed, 9 Oct 2024 15:14:49 +0200 Subject: [PATCH 5/6] fix: added handling of exeption if exercise doesn't exist --- src/controllers/exercise_controller.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/controllers/exercise_controller.php b/src/controllers/exercise_controller.php index b07ad37..28bebe1 100644 --- a/src/controllers/exercise_controller.php +++ b/src/controllers/exercise_controller.php @@ -15,7 +15,14 @@ function createExercise() function deleteExercise($id) { - $exercise = new Exercises($id); + try { + $exercise = new Exercises($id); + } + catch (Exception $e) { + lost(); + return; + } + if ($exercise->getExerciseStatus() == Status::Building->value || $exercise->getExerciseStatus() == Status::Closed->value) { $exercise->delete(); From fd81801859dea70fce5aee6a5b9ef3ede88d871f Mon Sep 17 00:00:00 2001 From: Joumoune Date: Wed, 9 Oct 2024 13:17:23 +0000 Subject: [PATCH 6/6] [Action][php-cs-fixer] Fix styling --- src/controllers/exercise_controller.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/controllers/exercise_controller.php b/src/controllers/exercise_controller.php index ad55b3b..96f6434 100644 --- a/src/controllers/exercise_controller.php +++ b/src/controllers/exercise_controller.php @@ -17,14 +17,12 @@ function deleteExercise($id) { try { $exercise = new Exercises($id); - } - catch (Exception $e) { + } catch (Exception $e) { lost(); return; } - if ($exercise->getExerciseStatus() == Status::Building->value || $exercise->getExerciseStatus() == Status::Closed->value) - { + if ($exercise->getExerciseStatus() == Status::Building->value || $exercise->getExerciseStatus() == Status::Closed->value) { $exercise->delete(); } header('Location: /exercises');