diff --git a/src/Controllers/TicketController.php b/src/Controllers/TicketController.php index 3dfa05e..fd47f53 100644 --- a/src/Controllers/TicketController.php +++ b/src/Controllers/TicketController.php @@ -4,6 +4,7 @@ namespace BitGoblin\Goliath\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\Goliath\Models\Ticket; @@ -40,4 +41,32 @@ class TicketController extends Controller { ->withStatus(302); } + public function getEdit(Request $request, Response $response, array $args): Response { + $ticket = Ticket::where('id', $args['ticket_id'])->get(); + + $view = Twig::fromRequest($request); + return $view->render($response, 'ticket/edit.twig', [ + 'ticket' => $ticket[0], + ]); + } + + public function postEdit(Request $request, Response $response, array $args): Response { + $ticket = Ticket::where('id', $args['ticket_id'])->first(); + + $params = (array)$request->getParsedBody(); + $ticket->title = $params['ticket_title']; + $ticket->body = $params['ticket_body']; + $ticket->severity = $params['ticket_severity']; + $ticket->due_at = $params['ticket_due']; + + $ticket->save(); + + // redirect the user back to the home page + $routeContext = RouteContext::fromRequest($request); + $routeParser = $routeContext->getRouteParser(); + return $response + ->withHeader('Location', $routeParser->urlFor('ticket.view', ['ticket_id' => $ticket->id])) + ->withStatus(302); + } + } diff --git a/src/routes.php b/src/routes.php index 2f1de9f..6daddd2 100644 --- a/src/routes.php +++ b/src/routes.php @@ -7,9 +7,13 @@ use Slim\Views\Twig; // index GET route - this page should welcome the user and direct them to the available actions $app->get('/', '\\BitGoblin\\Goliath\\Controllers\\HomeController:getIndex')->setName('index'); -// /ticket/create GET route - allows a user to fill out a form to create a ticket +// /ticket/create routes - allows a user to fill out a form to create a ticket $app->get('/ticket/create', '\\BitGoblin\\Goliath\\Controllers\\TicketController:getCreate')->setName('ticket.create'); $app->post('/ticket/create', '\\BitGoblin\\Goliath\\Controllers\\TicketController:postCreate'); +// /ticket/edit routes - update a ticket! +$app->get('/ticket/{ticket_id}/edit', '\\BitGoblin\\Goliath\\Controllers\\TicketController:getEdit')->setName('ticket.edit'); +$app->post('/ticket/{ticket_id}/edit', '\\BitGoblin\\Goliath\\Controllers\\TicketController:postEdit')->setName('ticket.edit'); + // /ticket/id route - displays ticket info to user $app->get('/ticket/{ticket_id}', '\\BitGoblin\\Goliath\\Controllers\\TicketController:getView')->setName('ticket.view'); diff --git a/views/index.twig b/views/index.twig index 85196ba..6ee6bdd 100644 --- a/views/index.twig +++ b/views/index.twig @@ -16,12 +16,16 @@ Title Severity Due date + Actions {% for ticket in tickets %} {{ ticket.title }} {{ ticket.severity }} {{ ticket.due_at }} + + + {% endfor %} diff --git a/views/ticket/edit.twig b/views/ticket/edit.twig new file mode 100644 index 0000000..6aa09f9 --- /dev/null +++ b/views/ticket/edit.twig @@ -0,0 +1,41 @@ +{% extends 'layout.twig' %} + +{% block title %}Create new ticket{% endblock %} + +{% block content %} +
+
+

Editing "{{ ticket.title }}"

+
+
+ +
+
+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + + + + +
+
+
+{% endblock %}