Added ability to edit hardware components

This commit is contained in:
Gregory Ballantine 2025-06-26 12:17:05 -04:00
parent 1a7e41edb2
commit e9e43187e4
4 changed files with 75 additions and 1 deletions

View File

@ -59,4 +59,31 @@ class ComponentController extends Controller {
->withStatus(302);
}
public function getEdit(Request $request, Response $response, array $args): Response {
$component = Component::where('id', $args['component_id'])->first();
$view = Twig::fromRequest($request);
return $view->render($response, 'component/edit.twig', [
'component' => $component,
]);
}
public function postEdit(Request $request, Response $response, array $args): Response {
$component = Component::where('id', $args['component_id'])->first();
$params = (array)$request->getParsedBody();
$component->name = $params['component_name'];
$component->type = $params['component_type'];
$component->save();
// redirect the user back to the home page
$routeContext = RouteContext::fromRequest($request);
$routeParser = $routeContext->getRouteParser();
return $response
->withHeader('Location', $routeParser->urlFor('component.view', ['component_id' => $component->id]))
->withStatus(302);
}
}

View File

@ -17,7 +17,13 @@ $app->group('/component', function(RouteCollectorProxy $group) {
$group->get('/list', '\\BitGoblin\\Colossus\\Controllers\\ComponentController:getList')->setName('component.list');
$group->get('/add', '\\BitGoblin\\Colossus\\Controllers\\ComponentController:getAdd')->setName('component.add');
$group->post('/add', '\\BitGoblin\\Colossus\\Controllers\\ComponentController:postAdd');
$group->get('/{component_id}', '\\BitGoblin\\Colossus\\Controllers\\ComponentController:getView')->setName('component.view');
$group->group('/{component_id}', function(RouteCollectorProxy $component) {
$component->get('', '\\BitGoblin\\Colossus\\Controllers\\ComponentController:getView')->setName('component.view');
$component->get('/edit', '\\BitGoblin\\Colossus\\Controllers\\ComponentController:getEdit')->setName('component.edit');
$component->post('/edit', '\\BitGoblin\\Colossus\\Controllers\\ComponentController:postEdit');
});
});
$app->group('/test', function(RouteCollectorProxy $group) {

40
views/component/edit.twig Normal file
View File

@ -0,0 +1,40 @@
{% extends 'layout.twig' %}
{% block title %}Editing Component: {{ component.name }}{% endblock %}
{% block content %}
<div class="row">
<div class="twelve columns">
<h1>Editing Component: {{ component.name }}</h1>
</div>
</div>
<div class="row">
<div class="twelve columns">
<form action="{{ url_for('component.edit', { component_id: component.id }) }}" method="POST" class="u-full-width">
<div class="row">
<div class="nine columns">
<label>
Component name:
<input type="text" id="component_name" class="u-full-width" name="component_name" value="{{ component.name }}">
</label>
</div>
<div class="three columns">
<label>
Component type:
<select class="u-full-width" name="component_type">
<option value="gpu" {% if component.type == 'gpu' %}selected{% endif %}>Graphics card</option>
<option value="cpu" {% if component.type == 'cpu' %}selected{% endif %}>Processor</option>
</select>
</label>
</div>
</div>
<input class="button button-primary u-full-width" type="submit" value="Submit">
</form>
</div>
</div>
{% endblock %}

View File

@ -6,6 +6,7 @@
<div class="row">
<div class="twelve columns">
<h1>{{ component.name }}</h1>
<p><a href="{{ url_for('component.edit', { component_id: component.id }) }}">Edit</a></p>
<p>{{ component.type }}</p>
</div>
</div>