diff --git a/app/Http/Controllers/Projects/CommentsController.php b/app/Http/Controllers/Projects/CommentsController.php index c1250b0..7fac242 100644 --- a/app/Http/Controllers/Projects/CommentsController.php +++ b/app/Http/Controllers/Projects/CommentsController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Projects; use Illuminate\Http\Request; +use App\Entities\Projects\Comment; use App\Entities\Projects\Project; use App\Http\Controllers\Controller; @@ -16,9 +17,14 @@ class CommentsController extends Controller */ public function index(Project $project) { + $editableComment = null; $comments = $project->comments()->latest()->paginate(); - return view('projects.comments', compact('project', 'comments')); + if (request('action') == 'comment-edit' && request('comment_id') != null) { + $editableComment = Comment::find(request('comment_id')); + } + + return view('projects.comments', compact('project', 'comments', 'editableComment')); } /** @@ -33,9 +39,7 @@ class CommentsController extends Controller $this->authorize('view', $project); $newComment = $request->validate([ - 'body' => 'required|string|max:255', - 'fu_type_id' => 'nullable|numeric', - 'objection_id' => 'nullable|numeric', + 'body' => 'required|string|max:255', ]); $project->comments()->create([ @@ -47,4 +51,23 @@ class CommentsController extends Controller return back(); } + + /** + * Update the specified comment. + * + * @param \Illuminate\Http\Request $request + * @param \App\Entities\Projects\Project $project + * @param \App\Entities\Projects\Comment $comment + * @return \Illuminate\Http\Response + */ + public function update(Request $request, Project $project, Comment $comment) + { + $commentData = $request->validate([ + 'body' => 'required|string|max:255', + ]); + $comment->update($commentData); + flash(__('comment.updated'), 'success'); + + return redirect()->route('projects.comments.index', [$project] + request(['page'])); + } } diff --git a/resources/views/projects/comments.blade.php b/resources/views/projects/comments.blade.php index 75bc337..d2ebabe 100755 --- a/resources/views/projects/comments.blade.php +++ b/resources/views/projects/comments.blade.php @@ -11,4 +11,38 @@ +@if (Request::get('action') == 'comment-edit' && $editableComment) + +@endif +@endsection + +@section('script') + @endsection diff --git a/resources/views/projects/partials/comment-section.blade.php b/resources/views/projects/partials/comment-section.blade.php index 08ef021..a054336 100644 --- a/resources/views/projects/partials/comment-section.blade.php +++ b/resources/views/projects/partials/comment-section.blade.php @@ -12,7 +12,7 @@ {{ $comment->created_at }} {{ $comment->creator->name }} - + {{ link_to_route('projects.comments.index', __('app.edit'), [$project, 'action' => 'comment-edit', 'comment_id' => $comment->id], ['id' => 'edit-comment-'.$comment->id, 'class' => 'small pull-right', 'title' => __('comment.edit')]) }} {!! nl2br($comment->body) !!} @endforeach diff --git a/routes/web/projects.php b/routes/web/projects.php index b8af5c4..8a6b120 100644 --- a/routes/web/projects.php +++ b/routes/web/projects.php @@ -42,6 +42,7 @@ Route::group(['middleware' => ['auth'], 'namespace' => 'Projects'], function () */ Route::get('projects/{project}/comments', 'CommentsController@index')->name('projects.comments.index'); Route::post('projects/{project}/comments', 'CommentsController@store')->name('projects.comments.store'); + Route::patch('projects/{project}/comments/{comment}', 'CommentsController@update')->name('projects.comments.update'); /* * Tasks Routes diff --git a/tests/Feature/Projects/ProjectCommentsTest.php b/tests/Feature/Projects/ProjectCommentsTest.php index b123298..24f85a5 100644 --- a/tests/Feature/Projects/ProjectCommentsTest.php +++ b/tests/Feature/Projects/ProjectCommentsTest.php @@ -50,4 +50,35 @@ class ProjectCommentsTest extends TestCase 'creator_id' => $admin->id, ]); } + + /** @test */ + public function user_can_edit_comment() + { + $this->adminUserSigningIn(); + $project = factory(Project::class)->create(); + $comment = factory(Comment::class)->create([ + 'commentable_type' => 'projects', + 'commentable_id' => $project->id, + 'body' => 'This is project comment.', + ]); + + $this->visitRoute('projects.comments.index', $project); + $this->seeElement('a', ['id' => 'edit-comment-'.$comment->id]); + $this->click('edit-comment-'.$comment->id); + $this->seeRouteIs('projects.comments.index', [$project, 'action' => 'comment-edit', 'comment_id' => $comment->id]); + + $this->submitForm(__('comment.update'), [ + 'body' => 'Komentar pertama.', + ]); + + $this->seePageIs(route('projects.comments.index', $project)); + $this->see(__('comment.updated')); + + $this->seeInDatabase('comments', [ + 'id' => $comment->id, + 'commentable_type' => 'projects', + 'commentable_id' => $project->id, + 'body' => 'Komentar pertama.', + ]); + } }