From 72828e9f3adfe4c1588034524f372ee307019be0 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Mon, 25 Mar 2019 21:04:33 +0800 Subject: [PATCH 01/11] Add job has has many comments relation --- app/Entities/Projects/Issue.php | 11 +++++++++++ app/Providers/AppServiceProvider.php | 1 + tests/Unit/Models/IssueTest.php | 15 +++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/app/Entities/Projects/Issue.php b/app/Entities/Projects/Issue.php index 17a31af..2ec06fc 100644 --- a/app/Entities/Projects/Issue.php +++ b/app/Entities/Projects/Issue.php @@ -3,6 +3,7 @@ namespace App\Entities\Projects; use App\Entities\Users\User; +use App\Entities\Projects\Comment; use App\Entities\Projects\Project; use Illuminate\Database\Eloquent\Model; @@ -48,4 +49,14 @@ class Issue extends Model { return ''.$this->status.''; } + + /** + * Issue has many comments relation. + * + * @return \Illuminate\Database\Eloquent\Relations\MorphMany + */ + public function comments() + { + return $this->morphMany(Comment::class, 'commentable'); + } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 818ae3c..d8823c2 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -22,6 +22,7 @@ class AppServiceProvider extends ServiceProvider Relation::morphMap([ 'projects' => 'App\Entities\Projects\Project', + 'issues' => 'App\Entities\Projects\Issue', 'jobs' => 'App\Entities\Projects\Job', ]); } diff --git a/tests/Unit/Models/IssueTest.php b/tests/Unit/Models/IssueTest.php index 7877fdd..a43ed72 100644 --- a/tests/Unit/Models/IssueTest.php +++ b/tests/Unit/Models/IssueTest.php @@ -5,7 +5,9 @@ namespace Tests\Unit\Models; use Tests\TestCase; use App\Entities\Users\User; use App\Entities\Projects\Issue; +use App\Entities\Projects\Comment; use App\Entities\Projects\Project; +use Illuminate\Support\Collection; use App\Entities\Projects\Priority; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -81,4 +83,17 @@ class IssueTest extends TestCase $this->assertEquals(''.$issue->priority.'', $issue->priority_label); } + + /** @test */ + public function an_issue_has_many_comments_relation() + { + $issue = factory(Issue::class)->create(); + $comment = factory(Comment::class)->create([ + 'commentable_type' => 'issues', + 'commentable_id' => $issue->id, + ]); + + $this->assertInstanceOf(Collection::class, $issue->comments); + $this->assertInstanceOf(Comment::class, $issue->comments->first()); + } } From bc9f380c853f9d9de2161529370239f96751a568 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Mon, 25 Mar 2019 21:14:06 +0800 Subject: [PATCH 02/11] User can see issue comments --- app/Http/Controllers/Projects/IssueController.php | 3 ++- .../issues/partials/comment-section.blade.php | 9 +++++++ resources/views/projects/issues/show.blade.php | 2 ++ tests/Feature/Projects/IssueCommentsTest.php | 29 ++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 resources/views/projects/issues/partials/comment-section.blade.php create mode 100644 tests/Feature/Projects/IssueCommentsTest.php diff --git a/app/Http/Controllers/Projects/IssueController.php b/app/Http/Controllers/Projects/IssueController.php index 1a0c603..a18938a 100644 --- a/app/Http/Controllers/Projects/IssueController.php +++ b/app/Http/Controllers/Projects/IssueController.php @@ -50,12 +50,13 @@ class IssueController extends Controller public function show(Project $project, Issue $issue) { + $comments = $issue->comments; $priorities = Priority::toArray(); $statuses = IssueStatus::toArray(); $users = User::pluck('name', 'id'); return view('projects.issues.show', compact( - 'project', 'issue', 'users', 'statuses', 'priorities' + 'project', 'issue', 'users', 'statuses', 'priorities', 'comments' )); } diff --git a/resources/views/projects/issues/partials/comment-section.blade.php b/resources/views/projects/issues/partials/comment-section.blade.php new file mode 100644 index 0000000..d19d66a --- /dev/null +++ b/resources/views/projects/issues/partials/comment-section.blade.php @@ -0,0 +1,9 @@ +@foreach($comments as $comment) +
+ + {{ $comment->time_display }} + {{ $comment->creator->name }} + + {!! nl2br($comment->body) !!} +
+@endforeach diff --git a/resources/views/projects/issues/show.blade.php b/resources/views/projects/issues/show.blade.php index 42ccb12..b069268 100755 --- a/resources/views/projects/issues/show.blade.php +++ b/resources/views/projects/issues/show.blade.php @@ -32,6 +32,8 @@ {{ link_to_route('projects.issues.index', __('issue.back_to_index'), [$project], ['class' => 'btn btn-default pull-right']) }} + + @include('projects.issues.partials.comment-section')
{{ Form::model($issue, ['route' => ['issues.options.update', $issue], 'method' => 'patch']) }} diff --git a/tests/Feature/Projects/IssueCommentsTest.php b/tests/Feature/Projects/IssueCommentsTest.php new file mode 100644 index 0000000..22b0890 --- /dev/null +++ b/tests/Feature/Projects/IssueCommentsTest.php @@ -0,0 +1,29 @@ +adminUserSigningIn(); + $issue = factory(Issue::class)->create(); + $comment = factory(Comment::class)->create([ + 'commentable_type' => 'issues', + 'commentable_id' => $issue->id, + 'body' => 'This is issue comment.', + ]); + + $this->visitRoute('projects.issues.show', [$issue->project, $issue]); + + $this->seeText('This is issue comment.'); + } +} From eaf5619c691177a3e7a53030fab9c8630d1f84ba Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Mon, 25 Mar 2019 21:14:52 +0800 Subject: [PATCH 03/11] Eagerloads issue comment creator --- app/Http/Controllers/Projects/IssueController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/Projects/IssueController.php b/app/Http/Controllers/Projects/IssueController.php index a18938a..2164c7b 100644 --- a/app/Http/Controllers/Projects/IssueController.php +++ b/app/Http/Controllers/Projects/IssueController.php @@ -50,10 +50,10 @@ class IssueController extends Controller public function show(Project $project, Issue $issue) { - $comments = $issue->comments; $priorities = Priority::toArray(); $statuses = IssueStatus::toArray(); $users = User::pluck('name', 'id'); + $comments = $issue->comments()->with('creator')->get(); return view('projects.issues.show', compact( 'project', 'issue', 'users', 'statuses', 'priorities', 'comments' From a0b7c66e40044af28f6dd2c113d191432d96004c Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Mon, 25 Mar 2019 21:24:21 +0800 Subject: [PATCH 04/11] 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, + ]); + } } From 3168ae91ac4281e8427476072c36f72fe0c8dbd1 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Mon, 25 Mar 2019 21:36:13 +0800 Subject: [PATCH 05/11] Add authoriation for issue commentings --- app/Http/Controllers/Issues/CommentController.php | 2 ++ app/Policies/Projects/IssuePolicy.php | 12 ++++++++++++ .../views/projects/issues/partials/comment-section.blade.php | 2 ++ tests/Unit/Policies/IssuePolicyTest.php | 9 +++++++++ 4 files changed, 25 insertions(+) diff --git a/app/Http/Controllers/Issues/CommentController.php b/app/Http/Controllers/Issues/CommentController.php index d7f66e4..8288f3b 100644 --- a/app/Http/Controllers/Issues/CommentController.php +++ b/app/Http/Controllers/Issues/CommentController.php @@ -18,6 +18,8 @@ class CommentController extends Controller */ public function store(Request $request, Issue $issue) { + $this->authorize('comment-on', $issue); + $newComment = $request->validate([ 'body' => 'required|string|max:255', ]); diff --git a/app/Policies/Projects/IssuePolicy.php b/app/Policies/Projects/IssuePolicy.php index 9bd8c8d..7fe975a 100644 --- a/app/Policies/Projects/IssuePolicy.php +++ b/app/Policies/Projects/IssuePolicy.php @@ -14,4 +14,16 @@ class IssuePolicy { return true; } + + /** + * Determine whether the user can add comment to an issue. + * + * @param \App\Entities\Users\User $user + * @param \App\Entities\Projects\Issue $issue + * @return bool + */ + public function commentOn(User $user, Issue $issue) + { + return true; + } } diff --git a/resources/views/projects/issues/partials/comment-section.blade.php b/resources/views/projects/issues/partials/comment-section.blade.php index 949ec7a..dff082a 100644 --- a/resources/views/projects/issues/partials/comment-section.blade.php +++ b/resources/views/projects/issues/partials/comment-section.blade.php @@ -8,8 +8,10 @@
@endforeach +@can('comment-on', $issue) {{ 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() }}

+@endcan diff --git a/tests/Unit/Policies/IssuePolicyTest.php b/tests/Unit/Policies/IssuePolicyTest.php index 051e927..a5b1016 100644 --- a/tests/Unit/Policies/IssuePolicyTest.php +++ b/tests/Unit/Policies/IssuePolicyTest.php @@ -22,4 +22,13 @@ class IssuePolicyTest extends TestCase $this->assertTrue($admin->can('create', new Issue())); } + + /** @test */ + public function admin_can_add_comment_to_an_issue() + { + $admin = $this->createUser('admin'); + $issue = factory(Issue::class)->create(); + + $this->assertTrue($admin->can('comment-on', $issue)); + } } From f07912eb81f926d96f524867d4a97ce57e2cfd09 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 26 Mar 2019 16:20:43 +0800 Subject: [PATCH 06/11] User can edit issue comment --- app/Http/Controllers/Issues/CommentController.php | 21 +++++++++++++- app/Http/Controllers/Projects/IssueController.php | 9 +++++- resources/lang/id/comment.php | 2 +- .../issues/partials/comment-section.blade.php | 27 ++++++++++++++++++ resources/views/projects/issues/show.blade.php | 11 ++++++++ routes/web/projects.php | 1 + tests/Feature/Projects/IssueCommentsTest.php | 32 ++++++++++++++++++++++ 7 files changed, 100 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Issues/CommentController.php b/app/Http/Controllers/Issues/CommentController.php index 8288f3b..31f9888 100644 --- a/app/Http/Controllers/Issues/CommentController.php +++ b/app/Http/Controllers/Issues/CommentController.php @@ -4,11 +4,11 @@ namespace App\Http\Controllers\Issues; use Illuminate\Http\Request; use App\Entities\Projects\Issue; +use App\Entities\Projects\Comment; use App\Http\Controllers\Controller; class CommentController extends Controller { - /** * Store a new comment in storage. * @@ -33,4 +33,23 @@ class CommentController extends Controller return back(); } + + /** + * Update the specified comment. + * + * @param \Illuminate\Http\Request $request + * @param \App\Entities\Projects\Issue $issue + * @param \App\Entities\Projects\Comment $comment + * @return \Illuminate\Http\Response + */ + public function update(Request $request, Issue $issue, Comment $comment) + { + $commentData = $request->validate([ + 'body' => 'required|string|max:255', + ]); + $comment->update($commentData); + flash(__('comment.updated'), 'success'); + + return redirect()->route('projects.issues.show', [$issue->project, $issue]); + } } diff --git a/app/Http/Controllers/Projects/IssueController.php b/app/Http/Controllers/Projects/IssueController.php index 2164c7b..830df9c 100644 --- a/app/Http/Controllers/Projects/IssueController.php +++ b/app/Http/Controllers/Projects/IssueController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Projects; use App\Entities\Users\User; use Illuminate\Http\Request; use App\Entities\Projects\Issue; +use App\Entities\Projects\Comment; use App\Entities\Projects\Project; use App\Entities\Projects\Priority; use App\Http\Controllers\Controller; @@ -50,13 +51,19 @@ class IssueController extends Controller public function show(Project $project, Issue $issue) { + $editableComment = null; $priorities = Priority::toArray(); $statuses = IssueStatus::toArray(); $users = User::pluck('name', 'id'); $comments = $issue->comments()->with('creator')->get(); + if (request('action') == 'comment-edit' && request('comment_id') != null) { + $editableComment = Comment::find(request('comment_id')); + } + return view('projects.issues.show', compact( - 'project', 'issue', 'users', 'statuses', 'priorities', 'comments' + 'project', 'issue', 'users', 'statuses', 'priorities', 'comments', + 'editableComment' )); } diff --git a/resources/lang/id/comment.php b/resources/lang/id/comment.php index 95a1586..b79cd50 100644 --- a/resources/lang/id/comment.php +++ b/resources/lang/id/comment.php @@ -12,7 +12,7 @@ return [ 'created' => 'Input Komentar berhasil.', 'edit' => 'Edit Komentar', 'update' => 'Update Komentar', - 'updated' => 'Update data Komentar telah berhasil.', + 'updated' => 'Update Komentar berhasil.', 'delete' => 'Hapus Komentar', 'delete_confirm' => 'Anda yakin akan menghapus Komentar ini?', 'deleted' => 'Komentar berhasil dihapus.', diff --git a/resources/views/projects/issues/partials/comment-section.blade.php b/resources/views/projects/issues/partials/comment-section.blade.php index dff082a..094df69 100644 --- a/resources/views/projects/issues/partials/comment-section.blade.php +++ b/resources/views/projects/issues/partials/comment-section.blade.php @@ -4,6 +4,9 @@ {{ $comment->time_display }} {{ $comment->creator->name }} +
+ {{ link_to_route('projects.issues.show', __('app.edit'), [$project, $issue, 'action' => 'comment-edit', 'comment_id' => $comment->id], ['id' => 'edit-comment-'.$comment->id, 'class' => 'small', 'title' => __('comment.edit')]) }} +
{!! nl2br($comment->body) !!} @endforeach @@ -15,3 +18,27 @@ {{ Form::close() }}

@endcan + +@if (Request::get('action') == 'comment-edit' && $editableComment) + +@endif diff --git a/resources/views/projects/issues/show.blade.php b/resources/views/projects/issues/show.blade.php index 906d5f3..beb3017 100755 --- a/resources/views/projects/issues/show.blade.php +++ b/resources/views/projects/issues/show.blade.php @@ -52,3 +52,14 @@ @endsection + +@section('script') + +@endsection diff --git a/routes/web/projects.php b/routes/web/projects.php index 1b585a1..c162b54 100644 --- a/routes/web/projects.php +++ b/routes/web/projects.php @@ -110,3 +110,4 @@ Route::patch('issues/{issue}/options', 'Issues\OptionController@update')->name(' * Issue Comments Routes */ Route::post('issues/{issue}/comments', 'Issues\CommentController@store')->name('issues.comments.store'); +Route::patch('issues/{issue}/comments/{comment}', 'Issues\CommentController@update')->name('issues.comments.update'); diff --git a/tests/Feature/Projects/IssueCommentsTest.php b/tests/Feature/Projects/IssueCommentsTest.php index fe8c2db..a57cc29 100644 --- a/tests/Feature/Projects/IssueCommentsTest.php +++ b/tests/Feature/Projects/IssueCommentsTest.php @@ -49,4 +49,36 @@ class IssueCommentsTest extends TestCase 'creator_id' => $admin->id, ]); } + + /** @test */ + public function user_can_edit_an_issue_comment() + { + $this->adminUserSigningIn(); + $issue = factory(Issue::class)->create(); + $comment = factory(Comment::class)->create([ + 'commentable_type' => 'issues', + 'commentable_id' => $issue->id, + 'body' => 'This is issue comment.', + ]); + + $this->visitRoute('projects.issues.show', [$issue->project, $issue]); + $this->seeElement('a', ['id' => 'edit-comment-'.$comment->id]); + $this->click('edit-comment-'.$comment->id); + $this->seeRouteIs('projects.issues.show', [$issue->project, $issue, 'action' => 'comment-edit', 'comment_id' => $comment->id]); + + $this->submitForm(__('comment.update'), [ + 'body' => 'Edited comment.', + ]); + + $this->seePageIs(route('projects.issues.show', [$issue->project, $issue])); + $this->see(__('comment.updated')); + + $this->seeInDatabase('comments', [ + 'id' => $comment->id, + 'commentable_type' => 'issues', + 'commentable_id' => $issue->id, + 'body' => 'Edited comment.', + ]); + } + } From e9c725e263a3f34f6058ee0226cdeff4dc768a6f Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 26 Mar 2019 16:24:56 +0800 Subject: [PATCH 07/11] Add authorization to edit comment --- app/Http/Controllers/Issues/CommentController.php | 2 ++ resources/views/projects/issues/index.blade.php | 2 +- resources/views/projects/issues/partials/comment-section.blade.php | 4 +++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Issues/CommentController.php b/app/Http/Controllers/Issues/CommentController.php index 31f9888..1894e32 100644 --- a/app/Http/Controllers/Issues/CommentController.php +++ b/app/Http/Controllers/Issues/CommentController.php @@ -44,6 +44,8 @@ class CommentController extends Controller */ public function update(Request $request, Issue $issue, Comment $comment) { + $this->authorize('update', $comment); + $commentData = $request->validate([ 'body' => 'required|string|max:255', ]); diff --git a/resources/views/projects/issues/index.blade.php b/resources/views/projects/issues/index.blade.php index a582c34..e90ff77 100755 --- a/resources/views/projects/issues/index.blade.php +++ b/resources/views/projects/issues/index.blade.php @@ -45,7 +45,7 @@ @empty - {{ __('issue.empty') }} + {{ __('issue.empty') }} @endforelse diff --git a/resources/views/projects/issues/partials/comment-section.blade.php b/resources/views/projects/issues/partials/comment-section.blade.php index 094df69..3cc9d69 100644 --- a/resources/views/projects/issues/partials/comment-section.blade.php +++ b/resources/views/projects/issues/partials/comment-section.blade.php @@ -5,7 +5,9 @@ {{ $comment->creator->name }}
- {{ link_to_route('projects.issues.show', __('app.edit'), [$project, $issue, 'action' => 'comment-edit', 'comment_id' => $comment->id], ['id' => 'edit-comment-'.$comment->id, 'class' => 'small', 'title' => __('comment.edit')]) }} + @can('update', $comment) + {{ link_to_route('projects.issues.show', __('app.edit'), [$project, $issue, 'action' => 'comment-edit', 'comment_id' => $comment->id], ['id' => 'edit-comment-'.$comment->id, 'class' => 'small', 'title' => __('comment.edit')]) }} + @endcan
{!! nl2br($comment->body) !!} From 27f77dcb2e3944dcbea5871dc7126346631bc0b7 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 26 Mar 2019 16:37:04 +0800 Subject: [PATCH 08/11] User can delete issue comments --- app/Http/Controllers/Issues/CommentController.php | 25 ++++++++++++++++++++++ .../issues/partials/comment-section.blade.php | 8 +++++++ routes/web/projects.php | 1 + tests/Feature/Projects/IssueCommentsTest.php | 22 +++++++++++++++++++ 4 files changed, 56 insertions(+) diff --git a/app/Http/Controllers/Issues/CommentController.php b/app/Http/Controllers/Issues/CommentController.php index 1894e32..95dcdbc 100644 --- a/app/Http/Controllers/Issues/CommentController.php +++ b/app/Http/Controllers/Issues/CommentController.php @@ -54,4 +54,29 @@ class CommentController extends Controller return redirect()->route('projects.issues.show', [$issue->project, $issue]); } + + /** + * Remove the specified comment. + * + * @param \App\Entities\Projects\Issue $issue + * @param \\App\Entities\Projects\Comment $comment + * @return \Illuminate\Routing\Redirector + */ + public function destroy(Issue $issue, Comment $comment) + { + $this->authorize('delete', $comment); + + request()->validate([ + 'comment_id' => 'required|exists:comments,id', + ]); + + if (request('comment_id') == $comment->id && $comment->delete()) { + flash(__('comment.deleted'), 'warning'); + + return redirect()->route('projects.issues.show', [$issue->project, $issue]); + } + flash(__('comment.undeleted'), 'error'); + + 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 3cc9d69..73eec00 100644 --- a/resources/views/projects/issues/partials/comment-section.blade.php +++ b/resources/views/projects/issues/partials/comment-section.blade.php @@ -8,6 +8,14 @@ @can('update', $comment) {{ link_to_route('projects.issues.show', __('app.edit'), [$project, $issue, 'action' => 'comment-edit', 'comment_id' => $comment->id], ['id' => 'edit-comment-'.$comment->id, 'class' => 'small', 'title' => __('comment.edit')]) }} @endcan + @can('delete', $comment) + {!! FormField::delete( + ['route' => ['issues.comments.destroy', $issue, $comment], 'class' => ''], + '×', + ['class' => 'btn-link', 'id' => 'delete-comment-'.$comment->id], + ['comment_id' => $comment->id] + ) !!} + @endcan {!! nl2br($comment->body) !!} diff --git a/routes/web/projects.php b/routes/web/projects.php index c162b54..2f4ae02 100644 --- a/routes/web/projects.php +++ b/routes/web/projects.php @@ -111,3 +111,4 @@ Route::patch('issues/{issue}/options', 'Issues\OptionController@update')->name(' */ Route::post('issues/{issue}/comments', 'Issues\CommentController@store')->name('issues.comments.store'); Route::patch('issues/{issue}/comments/{comment}', 'Issues\CommentController@update')->name('issues.comments.update'); +Route::delete('issues/{issue}/comments/{comment}', 'Issues\CommentController@destroy')->name('issues.comments.destroy'); diff --git a/tests/Feature/Projects/IssueCommentsTest.php b/tests/Feature/Projects/IssueCommentsTest.php index a57cc29..a85c290 100644 --- a/tests/Feature/Projects/IssueCommentsTest.php +++ b/tests/Feature/Projects/IssueCommentsTest.php @@ -81,4 +81,26 @@ class IssueCommentsTest extends TestCase ]); } + /** @test */ + public function user_can_delete_an_issue_comment() + { + $this->adminUserSigningIn(); + $issue = factory(Issue::class)->create(); + $comment = factory(Comment::class)->create([ + 'commentable_type' => 'issues', + 'commentable_id' => $issue->id, + 'body' => 'This is issue comment.', + ]); + + $this->visitRoute('projects.issues.show', [$issue->project, $issue]); + $this->seeElement('button', ['id' => 'delete-comment-'.$comment->id]); + $this->press('delete-comment-'.$comment->id); + + $this->seePageIs(route('projects.issues.show', [$issue->project, $issue])); + $this->see(__('comment.deleted')); + + $this->dontSeeInDatabase('comments', [ + 'id' => $comment->id, + ]); + } } From 803e1edaefcb4b44eb537d71cf8a9b467384dab2 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 26 Mar 2019 16:49:29 +0800 Subject: [PATCH 09/11] Add issue comments count on issue index page --- app/Http/Controllers/Projects/IssueController.php | 5 ++++- resources/views/projects/issues/index.blade.php | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Projects/IssueController.php b/app/Http/Controllers/Projects/IssueController.php index 830df9c..942c579 100644 --- a/app/Http/Controllers/Projects/IssueController.php +++ b/app/Http/Controllers/Projects/IssueController.php @@ -15,7 +15,10 @@ class IssueController extends Controller { public function index(Project $project) { - $issues = $project->issues()->with(['pic', 'creator'])->get(); + $issues = $project->issues() + ->with(['pic', 'creator']) + ->withCount(['comments']) + ->get(); return view('projects.issues.index', compact('project', 'issues')); } diff --git a/resources/views/projects/issues/index.blade.php b/resources/views/projects/issues/index.blade.php index e90ff77..8d0c4e1 100755 --- a/resources/views/projects/issues/index.blade.php +++ b/resources/views/projects/issues/index.blade.php @@ -19,6 +19,7 @@ {{ __('issue.title') }} {{ __('issue.priority') }} {{ __('app.status') }} + {{ __('comment.comment') }} {{ __('issue.pic') }} {{ __('issue.creator') }} {{ __('app.action') }} @@ -33,6 +34,7 @@ {{ $issue->title }} {!! $issue->priority_label !!} {!! $issue->status_label !!} + {{ $issue->comments_count }} {{ $issue->pic->name }} {{ $issue->creator->name }} From 2339a5f5172d74c9ba61ada6e04d5121421e5be7 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 26 Mar 2019 17:14:38 +0800 Subject: [PATCH 10/11] Sort project issue by update time --- app/Http/Controllers/Projects/IssueController.php | 1 + resources/lang/de/app.php | 1 + resources/lang/en/app.php | 1 + resources/lang/id/app.php | 1 + resources/views/projects/issues/index.blade.php | 2 ++ 5 files changed, 6 insertions(+) diff --git a/app/Http/Controllers/Projects/IssueController.php b/app/Http/Controllers/Projects/IssueController.php index 942c579..99e4463 100644 --- a/app/Http/Controllers/Projects/IssueController.php +++ b/app/Http/Controllers/Projects/IssueController.php @@ -16,6 +16,7 @@ class IssueController extends Controller public function index(Project $project) { $issues = $project->issues() + ->orderBy('updated_at', 'desc') ->with(['pic', 'creator']) ->withCount(['comments']) ->get(); diff --git a/resources/lang/de/app.php b/resources/lang/de/app.php index 2547e4e..6b6d461 100644 --- a/resources/lang/de/app.php +++ b/resources/lang/de/app.php @@ -17,6 +17,7 @@ return [ 'total' => 'gesamt', 'count' => 'Summe', 'remark' => 'Remark', + 'last_update' => 'Last Update', // Action 'add' => 'Hinzufügen', diff --git a/resources/lang/en/app.php b/resources/lang/en/app.php index 2f55d05..2fb3794 100644 --- a/resources/lang/en/app.php +++ b/resources/lang/en/app.php @@ -17,6 +17,7 @@ return [ 'total' => 'Total', 'count' => 'Count', 'remark' => 'Remark', + 'last_update' => 'Last Update', // Action 'add' => 'Add', diff --git a/resources/lang/id/app.php b/resources/lang/id/app.php index 3906fd0..99fffa1 100644 --- a/resources/lang/id/app.php +++ b/resources/lang/id/app.php @@ -17,6 +17,7 @@ return [ 'total' => 'Total', 'count' => 'Jumlah', 'remark' => 'Keterangan', + 'last_update' => 'Update', // Action 'add' => 'Tambah', diff --git a/resources/views/projects/issues/index.blade.php b/resources/views/projects/issues/index.blade.php index 8d0c4e1..cfcfbde 100755 --- a/resources/views/projects/issues/index.blade.php +++ b/resources/views/projects/issues/index.blade.php @@ -22,6 +22,7 @@ {{ __('comment.comment') }} {{ __('issue.pic') }} {{ __('issue.creator') }} + {{ __('app.last_update') }} {{ __('app.action') }} @@ -37,6 +38,7 @@ {{ $issue->comments_count }} {{ $issue->pic->name }} {{ $issue->creator->name }} + {{ $issue->updated_at->diffForHumans() }} {{ link_to_route( 'projects.issues.show', From c532c5bad92d1f08d80fe99fabe615283debe731 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 26 Mar 2019 17:15:04 +0800 Subject: [PATCH 11/11] Touch issue timestamp on comment creation --- app/Http/Controllers/Issues/CommentController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Http/Controllers/Issues/CommentController.php b/app/Http/Controllers/Issues/CommentController.php index 95dcdbc..1b3d312 100644 --- a/app/Http/Controllers/Issues/CommentController.php +++ b/app/Http/Controllers/Issues/CommentController.php @@ -28,6 +28,7 @@ class CommentController extends Controller 'body' => $newComment['body'], 'creator_id' => auth()->id(), ]); + $issue->touch(); flash(__('comment.created'), 'success');