Skip to content

Commit

Permalink
Merge pull request #21 from CPNV-ES/feature/delete-an-exercise
Browse files Browse the repository at this point in the history
Feature/delete an exercise
  • Loading branch information
Guillaume1868 authored Oct 9, 2024
2 parents 5f61443 + fd81801 commit 0d50f30
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
4 changes: 4 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 15 additions & 0 deletions src/controllers/exercise_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
4 changes: 4 additions & 0 deletions src/models/databases_connectors/databases_access.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 1 addition & 6 deletions src/models/databases_connectors/postgresql/postgresql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions src/models/databases_connectors/postgresql/postgresql_access.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions src/models/exercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 0d50f30

Please sign in to comment.