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

@ -14,5 +14,17 @@ $shadow-heavy: rgba(0, 0, 0, 0.25) 0px 54px 55px, rgba(0, 0, 0, 0.12) 0px -12px
} }
body{ body{
background: rgb(240, 235, 248);
font-size: 18px; font-size: 18px;
} }
table{
border: 1px solid #eee;
}
#main-wrapper{
padding: 1.5rem 2rem;
background: white;
border: 1px solid #bbb;
border-radius: 8px;
}

View File

@ -64,4 +64,49 @@ class TestController extends Controller {
->withStatus(302); ->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);
}
} }

View File

@ -24,6 +24,12 @@ class Test extends Model {
return $this->belongsTo(Component::class); return $this->belongsTo(Component::class);
} }
public function isBenchmarkSelected($benchmarkId) {
return $this->benchmarks()
->where('benchmarks.id', $benchmarkId)
->exists();
}
public function benchmarkResults() { public function benchmarkResults() {
$data = []; $data = [];

View File

@ -36,7 +36,13 @@ $app->group('/test', function(RouteCollectorProxy $group) {
$group->get('', '\\BitGoblin\\Colossus\\Controllers\\TestController:getList')->setName('test.list'); $group->get('', '\\BitGoblin\\Colossus\\Controllers\\TestController:getList')->setName('test.list');
$group->get('/add', '\\BitGoblin\\Colossus\\Controllers\\TestController:getAdd')->setName('test.add'); $group->get('/add', '\\BitGoblin\\Colossus\\Controllers\\TestController:getAdd')->setName('test.add');
$group->post('/add', '\\BitGoblin\\Colossus\\Controllers\\TestController:postAdd'); $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) { $app->group('/reports', function(RouteCollectorProxy $group) {

55
views/test/edit.twig Normal file
View 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 %}

View File

@ -6,6 +6,7 @@
<div class="row mb-3"> <div class="row mb-3">
<div class="col-12"> <div class="col-12">
<h1>{{ test.title }}</h1> <h1>{{ test.title }}</h1>
<p><a href="{{ url_for('test.edit', { test_id: test.id }) }}">Edit</a></p>
<p>{{ test.description }}</p> <p>{{ test.description }}</p>
</div> </div>
</div> </div>