Skip to content

Commit

Permalink
feat: Improve and update exercise state management
Browse files Browse the repository at this point in the history
  • Loading branch information
EthannSchneider committed Oct 9, 2024
1 parent 0c778c8 commit dc2cef9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
8 changes: 2 additions & 6 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,8 @@ function getRedirection($redirect_uri)
case '/exercises/new':
createAnExercises();
break;
case (preg_match('/^\/exercises\/([0-9]+)\?exercise%5Bstatus%5D=(answering|closed)$/A', $_SERVER['REQUEST_URI'], $output_array) ? true : false):
if ($output_array[2] == 'answering') {
setExerciseAsAnswering($output_array[1]);
} elseif ($output_array[2] == 'closed') {
setExerciseAsClosed($output_array[1]);
}
case (preg_match('/^\/exercises\/([0-9]+)$/A', $redirect_uri, $output_array) ? true : false):
changeStateOfExercise($output_array[1]);
break;
default:
lost();
Expand Down
40 changes: 29 additions & 11 deletions src/controllers/exercise_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,37 @@ function createExercise()
header('Location: /exercises/' . $exercise->getId() . '/fields');
}

function setExerciseAsAnswering(int $id)
function changeStateOfExercise(int $id)
{
$exercise = new Exercises($id);
if ($exercise->getStatus() === Status::Building && $exercise->getFieldsCount() > 0) {
$exercise->setExerciseAs(Status::Answering);
if (!isset($_GET['exercise']['status'])) {
badRequest();
return;
}
header('Location: /exercises');
}
function setExerciseAsClosed(int $id)
{
$exercise = new Exercises($id);
if ($exercise->getStatus() === Status::Answering) {
$exercise->setExerciseAs(Status::Closed);

$exercise = null;
try {
$exercise = new Exercises($id);
} catch (Exception) {
lost();
return;
}

if ($exercise->getFieldsCount() < 1) {
badRequest();
return;
}

switch ($_GET['exercise']['status']) {
case 'answering' && $exercise->getStatus() == Status::Building:
$exercise->setExerciseAs(Status::Answering);
break;
case 'closed' && $exercise->getStatus() == Status::Answering:
$exercise->setExerciseAs(Status::Closed);
break;
default:
badRequest();
return;
}

header('Location: /exercises');
}

0 comments on commit dc2cef9

Please sign in to comment.