Added a test edit page; fixed some styles

This commit is contained in:
2025-08-01 00:19:21 -04:00
parent 9dedba6364
commit 078edf5ab0
6 changed files with 126 additions and 1 deletions

View File

@ -64,4 +64,49 @@ class TestController extends Controller {
->withStatus(302);
}
public function getEdit(Request $request, Response $response, array $args): Response {
$test = Test::where('id', $args['test_id'])->first();
$benchmarks = Benchmark::all();
$components = Component::all();
$view = Twig::fromRequest($request);
return $view->render($response, 'test/edit.twig', [
'test' => $test,
'benchmarks' => $benchmarks,
'components' => $components,
]);
}
public function postEdit(Request $request, Response $response, array $args): Response {
$params = (array)$request->getParsedBody();
$test = Test::where('id', $args['test_id'])->first();
$test->title = $params['test_title'];
$test->description = $params['test_description'];
$test->component_id = $params['test_component'];
$test->save();
// attach benchmarks to test that aren't already attached
foreach ($params['test_benchmarks'] as $b) {
if (!$test->isBenchmarkSelected($b)) {
$test->benchmarks()->attach($b);
}
}
// remove benchmarks are not in the list
foreach ($test->benchmarks as $b) {
if (!in_array($b->id, $params['test_benchmarks'])) {
$test->benchmarks()->detach($b);
}
}
// redirect the user back to the home page
$routeContext = RouteContext::fromRequest($request);
$routeParser = $routeContext->getRouteParser();
return $response
->withHeader('Location', $routeParser->urlFor('test.view', [ 'test_id' => $test->id ]))
->withStatus(302);
}
}