diff --git a/public/index.php b/public/index.php index 71b2d2b..89117fa 100644 --- a/public/index.php +++ b/public/index.php @@ -32,6 +32,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..96f6434 100644 --- a/src/controllers/exercise_controller.php +++ b/src/controllers/exercise_controller.php @@ -12,3 +12,18 @@ function createExercise() $exercise = Exercises::create($_POST['exercise_title']); header('Location: /exercises/' . $exercise->getId() . '/fields'); } + +function deleteExercise($id) +{ + try { + $exercise = new Exercises($id); + } catch (Exception $e) { + lost(); + return; + } + + 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.php b/src/models/databases_connectors/postgresql/postgresql.php index 52fb02c..24ab270 100644 --- a/src/models/databases_connectors/postgresql/postgresql.php +++ b/src/models/databases_connectors/postgresql/postgresql.php @@ -26,13 +26,8 @@ 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 { 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..9b35c96 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();