Added a test edit page; fixed some styles
This commit is contained in:
@ -14,5 +14,17 @@ $shadow-heavy: rgba(0, 0, 0, 0.25) 0px 54px 55px, rgba(0, 0, 0, 0.12) 0px -12px
|
||||
}
|
||||
|
||||
body{
|
||||
background: rgb(240, 235, 248);
|
||||
font-size: 18px;
|
||||
}
|
||||
|
||||
table{
|
||||
border: 1px solid #eee;
|
||||
}
|
||||
|
||||
#main-wrapper{
|
||||
padding: 1.5rem 2rem;
|
||||
background: white;
|
||||
border: 1px solid #bbb;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -24,6 +24,12 @@ class Test extends Model {
|
||||
return $this->belongsTo(Component::class);
|
||||
}
|
||||
|
||||
public function isBenchmarkSelected($benchmarkId) {
|
||||
return $this->benchmarks()
|
||||
->where('benchmarks.id', $benchmarkId)
|
||||
->exists();
|
||||
}
|
||||
|
||||
public function benchmarkResults() {
|
||||
$data = [];
|
||||
|
||||
|
@ -36,7 +36,13 @@ $app->group('/test', function(RouteCollectorProxy $group) {
|
||||
$group->get('', '\\BitGoblin\\Colossus\\Controllers\\TestController:getList')->setName('test.list');
|
||||
$group->get('/add', '\\BitGoblin\\Colossus\\Controllers\\TestController:getAdd')->setName('test.add');
|
||||
$group->post('/add', '\\BitGoblin\\Colossus\\Controllers\\TestController:postAdd');
|
||||
$group->get('/{test_id}', '\\BitGoblin\\Colossus\\Controllers\\TestController:getView')->setName('test.view');
|
||||
|
||||
$group->group('/{test_id}', function(RouteCollectorProxy $test) {
|
||||
$test->get('', '\\BitGoblin\\Colossus\\Controllers\\TestController:getView')->setName('test.view');
|
||||
|
||||
$test->get('/edit', '\\BitGoblin\\Colossus\\Controllers\\TestController:getEdit')->setName('test.edit');
|
||||
$test->post('/edit', '\\BitGoblin\\Colossus\\Controllers\\TestController:postEdit');
|
||||
});
|
||||
});
|
||||
|
||||
$app->group('/reports', function(RouteCollectorProxy $group) {
|
||||
|
55
views/test/edit.twig
Normal file
55
views/test/edit.twig
Normal file
@ -0,0 +1,55 @@
|
||||
{% extends 'layout.twig' %}
|
||||
|
||||
{% block title %}Editing: {{ test.title }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<div class="row mb-4">
|
||||
<div class="col-12">
|
||||
<h1>Editing: {{ test.title }}</h1>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<form action="{{ url_for('test.edit', { test_id: test.id }) }}" method="POST">
|
||||
<div class="row mb-3">
|
||||
<div class="col-6">
|
||||
<label class="form-label" for="test_title">Test title:</label>
|
||||
<input id="test_title" class="form-control" type="text" name="test_title" placeholder="My new test" value="{{ test.title }}">
|
||||
</div>
|
||||
|
||||
<div class="col-6">
|
||||
<label class="form-label" for="test_component">Hardware component:</label>
|
||||
<select id="test_component" class="form-select" name="test_component">
|
||||
{% for c in components %}
|
||||
<option value="{{ c.id }}" {% if test.component.id == c.id %}selected{% endif %}>{{ c.name }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-12 mb-3">
|
||||
<label class="form-label" for="test_benchmarks">Benchmarks:</label>
|
||||
<select id="test_benchmarks" class="form-select" name="test_benchmarks[]" multiple>
|
||||
{% for b in benchmarks %}
|
||||
<option value="{{ b.id }}" {% if test.isBenchmarkSelected(b.id) %}selected{% endif %}>{{ b.name }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row mb-3">
|
||||
<div class="col-12">
|
||||
<label class="form-label" for="test_description">Test description:</label>
|
||||
<textarea id="test_description" class="form-control" name="test_description" rows="8">{{ test.description }}</textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<input class="btn btn-primary" type="submit" value="Submit Changes">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
@ -6,6 +6,7 @@
|
||||
<div class="row mb-3">
|
||||
<div class="col-12">
|
||||
<h1>{{ test.title }}</h1>
|
||||
<p><a href="{{ url_for('test.edit', { test_id: test.id }) }}">Edit</a></p>
|
||||
<p>{{ test.description }}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
Reference in New Issue
Block a user