Started work to re-work database; add new models for benchmarks and components
This commit is contained in:
54
src/Controllers/BenchmarkController.php
Normal file
54
src/Controllers/BenchmarkController.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace BitGoblin\Colossus\Controllers;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Slim\Routing\RouteContext;
|
||||
use Slim\Views\Twig;
|
||||
use BitGoblin\Colossus\Models\Benchmark;
|
||||
|
||||
class BenchmarkController extends Controller {
|
||||
|
||||
public function getList(Request $request, Response $response): Response {
|
||||
$benchmarks = Benchmark::orderByDesc('updated_at')->get();
|
||||
|
||||
$view = Twig::fromRequest($request);
|
||||
return $view->render($response, 'benchmark/list.twig', [
|
||||
'benchmarks' => $benchmarks,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getView(Request $request, Response $response, array $args): Response {
|
||||
$benchmark = Benchmark::where('id', $args['benchmark_id'])->first();
|
||||
|
||||
$view = Twig::fromRequest($request);
|
||||
return $view->render($response, 'benchmark/view.twig', [
|
||||
'benchmark' => $benchmark,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getAdd(Request $request, Response $response): Response {
|
||||
$view = Twig::fromRequest($request);
|
||||
return $view->render($response, 'benchmark/add.twig');
|
||||
}
|
||||
|
||||
public function postAdd(Request $request, Response $response): Response {
|
||||
$params = (array)$request->getParsedBody();
|
||||
|
||||
$benchmark = new Benchmark;
|
||||
$benchmark->name = $params['benchmark_name'];
|
||||
$benchmark->description = $params['benchmark_description'];
|
||||
$benchmark->scoring = $params['benchmark_scoring'];
|
||||
|
||||
$benchmark->save();
|
||||
|
||||
// redirect the user back to the home page
|
||||
$routeContext = RouteContext::fromRequest($request);
|
||||
$routeParser = $routeContext->getRouteParser();
|
||||
return $response
|
||||
->withHeader('Location', $routeParser->urlFor('benchmark.list'))
|
||||
->withStatus(302);
|
||||
}
|
||||
|
||||
}
|
53
src/Controllers/ComponentController.php
Normal file
53
src/Controllers/ComponentController.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace BitGoblin\Colossus\Controllers;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Slim\Routing\RouteContext;
|
||||
use Slim\Views\Twig;
|
||||
use BitGoblin\Colossus\Models\Component;
|
||||
|
||||
class ComponentController extends Controller {
|
||||
|
||||
public function getList(Request $request, Response $response): Response {
|
||||
$components = Component::orderByDesc('updated_at')->get();
|
||||
|
||||
$view = Twig::fromRequest($request);
|
||||
return $view->render($response, 'component/list.twig', [
|
||||
'components' => $components,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getView(Request $request, Response $response, array $args): Response {
|
||||
$component = Component::where('id', $args['component_id'])->first();
|
||||
|
||||
$view = Twig::fromRequest($request);
|
||||
return $view->render($response, 'components/view.twig', [
|
||||
'component' => $component,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getAdd(Request $request, Response $response): Response {
|
||||
$view = Twig::fromRequest($request);
|
||||
return $view->render($response, 'component/add.twig');
|
||||
}
|
||||
|
||||
public function postAdd(Request $request, Response $response): Response {
|
||||
$params = (array)$request->getParsedBody();
|
||||
|
||||
$component = new Component;
|
||||
$component->name = $params['component_name'];
|
||||
$component->type = $params['component_description'];
|
||||
|
||||
$component->save();
|
||||
|
||||
// redirect the user back to the home page
|
||||
$routeContext = RouteContext::fromRequest($request);
|
||||
$routeParser = $routeContext->getRouteParser();
|
||||
return $response
|
||||
->withHeader('Location', $routeParser->urlFor('component.list'))
|
||||
->withStatus(302);
|
||||
}
|
||||
|
||||
}
|
19
src/Models/Benchmark.php
Normal file
19
src/Models/Benchmark.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace BitGoblin\Colossus\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Benchmark extends Model {
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'description',
|
||||
'scoring',
|
||||
];
|
||||
|
||||
public function tests() {
|
||||
return $this->hasMany(Test::class);
|
||||
}
|
||||
|
||||
}
|
18
src/Models/Component.php
Normal file
18
src/Models/Component.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace BitGoblin\Colossus\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Component extends Model {
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'type',
|
||||
];
|
||||
|
||||
public function tests() {
|
||||
return $this->hasMany(Test::class);
|
||||
}
|
||||
|
||||
}
|
@ -8,9 +8,6 @@ class Result extends Model {
|
||||
|
||||
protected $fillable = [
|
||||
'test_id',
|
||||
'component',
|
||||
'benchmark',
|
||||
'type',
|
||||
'average',
|
||||
'minimum',
|
||||
'maximum',
|
||||
|
@ -7,12 +7,21 @@ use Illuminate\Database\Eloquent\Model;
|
||||
class Test extends Model {
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'description',
|
||||
'date_tag',
|
||||
'benchmark_id',
|
||||
'component_id',
|
||||
];
|
||||
|
||||
public function results() {
|
||||
return $this->hasMany(Result::class);
|
||||
}
|
||||
|
||||
public function benchmark() {
|
||||
return $this->belongsTo(Benchmark::class);
|
||||
}
|
||||
|
||||
public function component() {
|
||||
return $this->belongsTo(Component::class);
|
||||
}
|
||||
|
||||
}
|
@ -1,12 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Slim\Routing\RouteCollectorProxy;
|
||||
|
||||
$app->get('/', '\\BitGoblin\\Colossus\\Controllers\\HomeController:getIndex')->setName('dashboard');
|
||||
|
||||
$app->get('/test', '\\BitGoblin\\Colossus\\Controllers\\TestController:getList')->setName('test.list');
|
||||
$app->get('/test/add', '\\BitGoblin\\Colossus\\Controllers\\TestController:getAdd')->setName('test.add');
|
||||
$app->post('/test/add', '\\BitGoblin\\Colossus\\Controllers\\TestController:postAdd');
|
||||
$app->get('/test/{test_id}', '\\BitGoblin\\Colossus\\Controllers\\TestController:getView')->setName('test.view');
|
||||
$app->group('/benchmark', function(RouteCollectorProxy $group) {
|
||||
$group->get('', '\\BitGoblin\\Colossus\\Controllers\\BenchmarkController:getList')->setName('benchmark.list');
|
||||
$group->get('/add', '\\BitGoblin\\Colossus\\Controllers\\BenchmarkController:getAdd')->setName('benchmark.add');
|
||||
$group->post('/add', '\\BitGoblin\\Colossus\\Controllers\\BenchmarkController:postAdd');
|
||||
$group->get('/{test_id}', '\\BitGoblin\\Colossus\\Controllers\\BenchmarkController:getView')->setName('benchmark.view');
|
||||
});
|
||||
|
||||
$app->get('/result', '\\BitGoblin\\Colossus\\Controllers\\ResultController:getList')->setName('result.list');
|
||||
$app->get('/result/add', '\\BitGoblin\\Colossus\\Controllers\\ResultController:getAdd')->setName('result.add');
|
||||
$app->post('/result/add', '\\BitGoblin\\Colossus\\Controllers\\ResultController:postAdd');
|
||||
$app->group('/component', function(RouteCollectorProxy $group) {
|
||||
$group->get('', '\\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('/{test_id}', '\\BitGoblin\\Colossus\\Controllers\\ComponentController:getView')->setName('component.view');
|
||||
});
|
||||
|
||||
$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');
|
||||
});
|
||||
|
||||
$app->group('/result', function(RouteCollectorProxy $group) {
|
||||
$group->get('', '\\BitGoblin\\Colossus\\Controllers\\ResultController:getList')->setName('result.list');
|
||||
$group->get('/add', '\\BitGoblin\\Colossus\\Controllers\\ResultController:getAdd')->setName('result.add');
|
||||
$group->post('/add', '\\BitGoblin\\Colossus\\Controllers\\ResultController:postAdd');
|
||||
});
|
||||
|
Reference in New Issue
Block a user