Added a test edit page; fixed some styles
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user