Skip to content

Commit

Permalink
refactor: making a way to retrieve all 3 status of an excercice witho…
Browse files Browse the repository at this point in the history
…ut making 4 method (for all, for building, ect...)
  • Loading branch information
EthannSchneider committed Sep 26, 2024
1 parent b546124 commit 727fe60
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 27 deletions.
3 changes: 3 additions & 0 deletions src/controllers/navigation.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

include_once MODEL_DIR . '/exercise.php';

function home()
{
include VIEW_DIR . '/home.php';
Expand All @@ -12,6 +14,7 @@ function create_an_exercises()

function exercises_root()
{
$exercises = exercises::getExercises(Status::Answering);
include VIEW_DIR . '/exercises_root.php';
}

Expand Down
4 changes: 1 addition & 3 deletions src/models/databases_connectors/databases_access.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ public function createExercise(string $title): int;

public function getExerciseTitle(int $id): string;

public function getExercises(): array;

public function getExercisesAnswering(): array;
public function getExercises(int $status = -1): array;
}
12 changes: 5 additions & 7 deletions src/models/databases_connectors/postgresql/postgresql_access.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ public function getExerciseTitle(int $id): string
return $result[0]['title'];
}

public function getExercises(): array
public function getExercises(int $status = -1): array
{
return $this->postgresql->select('SELECT id FROM exercises;');
}

public function getExercisesAnswering(): array
{
return $this->postgresql->select('SELECT id FROM exercises WHERE status = 1;');
if ($status < 0) {
return $this->postgresql->select('SELECT id FROM exercises');
}
return $this->postgresql->select('SELECT id FROM exercises WHERE status = :status', [':status' => $status]);
}

private function create_db_if_not_exist()
Expand Down
28 changes: 13 additions & 15 deletions src/models/exercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

require_once MODEL_DIR . '/databases_connectors/databases_choose.php';

enum Status: int
{
case Building = 0;
case Answering = 1;
case Closed = 2;
}

class Exercises
{
private DatabasesAccess $database_access;
Expand Down Expand Up @@ -33,25 +40,16 @@ public function getTitle()
return $this->database_access->getexerciseTitle($this->id);
}

public static function getExercises()
public static function getExercises(Status $status = null)
{
$database_access = (new DatabasesChoose())->getDatabase();
$exercises_data = $database_access->getExercises();

$exercises = [];
foreach ($exercises_data as $exercise_data) {
$exercise = new self($exercise_data['id']);
$exercises[] = $exercise;
$exercises_data = [];
if ($status == null) {
$exercises_data = $database_access->getExercises();
} else {
$exercises_data = $database_access->getExercises($status->value);
}

return $exercises;
}

public static function getExercisesAnswering()
{
$database_access = (new DatabasesChoose())->getDatabase();
$exercises_data = $database_access->getExercisesAnswering();

$exercises = [];
foreach ($exercises_data as $exercise_data) {
$exercise = new self($exercise_data['id']);
Expand Down
2 changes: 0 additions & 2 deletions src/views/exercises_root.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
$title = 'ExerciseLooper';
include_once MODEL_DIR . '/exercise.php';
$exercises = exercises::getExercisesAnswering();

ob_start();
?>
Expand Down

0 comments on commit 727fe60

Please sign in to comment.