From a0b7c66e40044af28f6dd2c113d191432d96004c Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Mon, 25 Mar 2019 21:24:21 +0800 Subject: [PATCH] User can add comment to a project issue --- app/Http/Controllers/Issues/CommentController.php | 34 ++++++++++++++++++++++ .../issues/partials/comment-section.blade.php | 6 ++++ resources/views/projects/issues/show.blade.php | 2 +- routes/web/projects.php | 5 ++++ tests/Feature/Projects/IssueCommentsTest.php | 23 +++++++++++++++ 5 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/Issues/CommentController.php diff --git a/app/Http/Controllers/Issues/CommentController.php b/app/Http/Controllers/Issues/CommentController.php new file mode 100644 index 0000000..d7f66e4 --- /dev/null +++ b/app/Http/Controllers/Issues/CommentController.php @@ -0,0 +1,34 @@ +validate([ + 'body' => 'required|string|max:255', + ]); + + $issue->comments()->create([ + 'body' => $newComment['body'], + 'creator_id' => auth()->id(), + ]); + + flash(__('comment.created'), 'success'); + + return back(); + } +} diff --git a/resources/views/projects/issues/partials/comment-section.blade.php b/resources/views/projects/issues/partials/comment-section.blade.php index d19d66a..949ec7a 100644 --- a/resources/views/projects/issues/partials/comment-section.blade.php +++ b/resources/views/projects/issues/partials/comment-section.blade.php @@ -7,3 +7,9 @@ {!! nl2br($comment->body) !!} @endforeach + +{{ Form::open(['route' => ['issues.comments.store', $issue]]) }} +{!! FormField::textarea('body', ['required' => true, 'label' => false, 'placeholder' => __('comment.create_text')]) !!} +{{ Form::submit(__('comment.create'), ['class' => 'btn btn-success pull-right']) }} +{{ Form::close() }} +

diff --git a/resources/views/projects/issues/show.blade.php b/resources/views/projects/issues/show.blade.php index b069268..906d5f3 100755 --- a/resources/views/projects/issues/show.blade.php +++ b/resources/views/projects/issues/show.blade.php @@ -32,7 +32,7 @@ {{ link_to_route('projects.issues.index', __('issue.back_to_index'), [$project], ['class' => 'btn btn-default pull-right']) }} - +
@include('projects.issues.partials.comment-section')
diff --git a/routes/web/projects.php b/routes/web/projects.php index 45d403f..1b585a1 100644 --- a/routes/web/projects.php +++ b/routes/web/projects.php @@ -105,3 +105,8 @@ Route::group(['middleware' => ['auth']], function () { * Issue Options Routes */ Route::patch('issues/{issue}/options', 'Issues\OptionController@update')->name('issues.options.update'); + +/** + * Issue Comments Routes + */ +Route::post('issues/{issue}/comments', 'Issues\CommentController@store')->name('issues.comments.store'); diff --git a/tests/Feature/Projects/IssueCommentsTest.php b/tests/Feature/Projects/IssueCommentsTest.php index 22b0890..fe8c2db 100644 --- a/tests/Feature/Projects/IssueCommentsTest.php +++ b/tests/Feature/Projects/IssueCommentsTest.php @@ -26,4 +26,27 @@ class IssueCommentsTest extends TestCase $this->seeText('This is issue comment.'); } + + /** @test */ + public function admin_can_add_comment_to_an_issue() + { + $admin = $this->adminUserSigningIn(); + $issue = factory(Issue::class)->create(); + + $this->visitRoute('projects.issues.show', [$issue->project, $issue]); + + $this->submitForm(__('comment.create'), [ + 'body' => 'First comment.', + ]); + + $this->seePageIs(route('projects.issues.show', [$issue->project, $issue])); + $this->see(__('comment.created')); + + $this->seeInDatabase('comments', [ + 'commentable_type' => 'issues', + 'commentable_id' => $issue->id, + 'body' => 'First comment.', + 'creator_id' => $admin->id, + ]); + } }