Added ticket comments

This commit is contained in:
Gregory Ballantine 2022-11-21 23:48:25 -05:00
parent 4371fd0b2f
commit 5683c0bc8b
8 changed files with 136 additions and 4 deletions

View File

@ -74,7 +74,8 @@ input[type="submit"].button-primary
#main-wrapper
margin-top: 25px
#ticket-form
#ticket-form,
#comment-form
textarea
max-width: 100%
height: 250px
@ -139,3 +140,17 @@ input[type="submit"].button-primary
i
margin-right: 5px
font-size: 2rem
#comment-form
textarea
height: 150px
.comments-list
list-style: none
margin: 0
.comment
&:not(:last-child)
border-bottom: 1px solid #666
p:last-child
margin-bottom: 5px

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class AddCommentsTable extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('comments');
$table->addColumn('body', 'text', ['null' => false])
->addColumn('ticket_id', 'integer', ['null' => false])
->addTimestamps()
->addForeignKey('ticket_id', 'tickets', 'id', ['delete' => 'CASCADE', 'update' => 'CASCADE'])
->addIndex(['body'])
->create();
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace BitGoblin\Goliath\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Routing\RouteContext;
use BitGoblin\Goliath\Models\Comment;
class CommentController extends Controller {
public function postAdd(Request $request, Response $response): Response {
$params = (array)$request->getParsedBody();
$comment = new Comment;
$comment->body = $params['comment_body'];
$comment->ticket_id = $params['ticket_id'];
$comment->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' => $comment->ticket->id]))
->withStatus(302);
}
}

View File

@ -11,11 +11,11 @@ use BitGoblin\Goliath\Models\Ticket;
class TicketController extends Controller {
public function getView(Request $request, Response $response, array $args): Response {
$ticket = Ticket::where('id', $args['ticket_id'])->get();
$ticket = Ticket::where('id', $args['ticket_id'])->get()[0];
$view = Twig::fromRequest($request);
return $view->render($response, 'ticket/view.twig', [
'ticket' => $ticket[0],
'ticket' => $ticket,
]);
}

28
src/Models/Comment.php Normal file
View File

@ -0,0 +1,28 @@
<?php
namespace BitGoblin\Goliath\Models;
use Illuminate\Database\Eloquent\Model;
use League\CommonMark\CommonMarkConverter;
class Comment extends Model {
protected $fillable = [
'body',
'ticket_id',
];
public function render(): string {
$converter = new CommonMarkConverter([
'html_input' => 'strip',
'allow_unsafe_links' => false,
]);
return $converter->convert($this->body);
}
public function ticket() {
return $this->belongsTo(Ticket::class);
}
}

View File

@ -23,4 +23,8 @@ class Ticket extends Model {
return $converter->convert($this->body);
}
public function comments() {
return $this->hasMany(Comment::class);
}
}

View File

@ -17,3 +17,6 @@ $app->post('/ticket/{ticket_id}/edit', '\\BitGoblin\\Goliath\\Controllers\\Ticke
// /ticket/id route - displays ticket info to user
$app->get('/ticket/{ticket_id}', '\\BitGoblin\\Goliath\\Controllers\\TicketController:getView')->setName('ticket.view');
// add a comment to a ticket
$app->post('/comment/add', '\\BitGoblin\\Goliath\\Controllers\\CommentController:postAdd')->setName('comment.add');

View File

@ -48,7 +48,31 @@
<div class="row">
<div class="twelve columns">
<h3>Ticket comments/Updates</h3>
{% if ticket.comments | length > 0 %}
<ul class="comments-list">
{% for comment in ticket.comments %}
<li class="comment">{{ comment.render() | raw }}</li>
{% endfor %}
</ul>
{% else %}
<p>There are no comments to display at this time.</p>
{% endif %}
</div>
</div>
<hr>
<!-- add a comment form -->
<div class="row">
<div class="twelve columns">
<form id="comment-form" action="{{ url_for('comment.add') }}" method="POST" class="u-full-width">
<label for="comment_body">Add a comment:</label>
<textarea id="comment_body" class="u-full-width" placeholder="Add a comment..." name="comment_body"></textarea>
<input type="hidden" name="ticket_id" value="{{ ticket.id }}">
<input class="button-primary u-full-width" type="submit" value="Submit">
</form>
</div>
</div>
{% endblock %}