68 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			68 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?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;
 | |
| use BitGoblin\Colossus\Models\Component;
 | |
| use BitGoblin\Colossus\Models\Test;
 | |
| 
 | |
| class TestController extends Controller {
 | |
| 
 | |
|   public function getList(Request $request, Response $response): Response {
 | |
|     $tests = Test::orderByDesc('updated_at')->get();
 | |
| 
 | |
|     $view = Twig::fromRequest($request);
 | |
|     return $view->render($response, 'test/list.twig', [
 | |
|       'tests' => $tests,
 | |
|     ]);
 | |
|   }
 | |
| 
 | |
|   public function getView(Request $request, Response $response, array $args): Response {
 | |
|     $test = Test::where('id', $args['test_id'])->first();
 | |
| 
 | |
|     $view = Twig::fromRequest($request);
 | |
|     return $view->render($response, 'test/view.twig', [
 | |
|       'test' => $test,
 | |
|     ]);
 | |
|   }
 | |
| 
 | |
|   public function getAdd(Request $request, Response $response): Response {
 | |
|     $benchmarks = Benchmark::all();
 | |
|     $components = Component::all();
 | |
| 
 | |
|     $view = Twig::fromRequest($request);
 | |
|     return $view->render($response, 'test/add.twig', [
 | |
|       'benchmarks' => $benchmarks,
 | |
|       'components' => $components,
 | |
|     ]);
 | |
|   }
 | |
| 
 | |
|   public function postAdd(Request $request, Response $response): Response {
 | |
|     $params = (array)$request->getParsedBody();
 | |
| 
 | |
|     $test = new Test;
 | |
|     $test->title = $params['test_title'];
 | |
|     $test->description = $params['test_description'];
 | |
|     $test->component_id = $params['test_component'];
 | |
| 
 | |
|     // attach benchmarks to test
 | |
|     foreach ($params['test_benchmarks'] as $b) {
 | |
|       $test->benchmarks()->attach($b);
 | |
|     }
 | |
| 
 | |
|     $test->save();
 | |
| 
 | |
|     // redirect the user back to the home page
 | |
|     $routeContext = RouteContext::fromRequest($request);
 | |
|     $routeParser = $routeContext->getRouteParser();
 | |
|     return $response
 | |
|       ->withHeader('Location', $routeParser->urlFor('test.list'))
 | |
|       ->withStatus(302);
 | |
|   }
 | |
| 
 | |
| }
 |