-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from HE-Arc/18-comment-section
18 comment section
- Loading branch information
Showing
14 changed files
with
247 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
use App\Models\Comment; | ||
|
||
class CommentController extends Controller | ||
{ | ||
public function store(Request $request) | ||
{ | ||
$request->validate([ | ||
'comment' => 'required|max:1000', | ||
'user_id' => 'required', | ||
'timeline_id' => 'required', | ||
]); | ||
|
||
$comment = Comment::create($request->all()); | ||
|
||
return redirect()->route('timeline.show', $request->timeline_id) | ||
->with('success', 'Comment added successfully.'); | ||
} | ||
|
||
public function destroy(Comment $comment) | ||
{ | ||
$comment->delete(); | ||
|
||
return redirect()->route('timeline.show', $comment->timeline_id) | ||
->with('success', 'Comment deleted successfully.'); | ||
} | ||
|
||
public function update(Request $request, Comment $comment) | ||
{ | ||
$request->validate([ | ||
'comment' => 'required|max:1000', | ||
'user_id' => 'required', | ||
'timeline_id' => 'required', | ||
]); | ||
|
||
$comment->update($request->all()); | ||
|
||
return redirect()->route('timeline.show', $comment->timeline_id) | ||
->with('success', 'Comment updated successfully.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
timeliner/database/migrations/2024_12_05_112043_add_fields_to_comments_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::table('comments', function (Blueprint $table) { | ||
$table->unsignedBigInteger('user_id'); | ||
$table->unsignedBigInteger('timeline_id'); | ||
$table->text('comment'); | ||
|
||
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); | ||
$table->foreign('timeline_id')->references('id')->on('timelines')->onDelete('cascade'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('comments', function (Blueprint $table) { | ||
// | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Database\Seeders; | ||
|
||
use Illuminate\Database\Console\Seeds\WithoutModelEvents; | ||
use Illuminate\Database\Seeder; | ||
use App\Models\Comment; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class CommentSeeder extends Seeder | ||
{ | ||
/** | ||
* Run the database seeds. | ||
*/ | ||
public function run(): void | ||
{ | ||
DB::statement('SET FOREIGN_KEY_CHECKS=0;'); | ||
Comment::truncate(); | ||
DB::statement('SET FOREIGN_KEY_CHECKS=1;'); | ||
|
||
$comments = [ | ||
['user_id' => "1", 'timeline_id' => "2", 'comment' => "This is a comment"], | ||
['user_id' => "1", 'timeline_id' => "1", 'comment' => "This is a second comment"], | ||
['user_id' => "2", 'timeline_id' => "2", 'comment' => "This is a third comment"], | ||
['user_id' => "2", 'timeline_id' => "1", 'comment' => "This is a fourth comment"], | ||
]; | ||
|
||
foreach ($comments as $comment) { | ||
Comment::create($comment); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function toggleElementById(id) { | ||
const element = document.getElementById(id); | ||
element.classList.toggle('hidden'); | ||
} | ||
|
||
function confirmDelete() { | ||
return confirm('Are you sure you want to delete this?'); | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
document.querySelectorAll('.toggle-button').forEach((button) => { | ||
button.addEventListener('click', (event) => { | ||
toggleElementById(event.target.dataset.target); | ||
}); | ||
}); | ||
|
||
document.querySelectorAll('.delete-button').forEach((button) => { | ||
button.addEventListener('click', (event) => { | ||
if (!confirmDelete()) { | ||
event.preventDefault(); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
timeliner/resources/views/timeline/partials/comment.blade.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
@vite(['resources/js/formfunctions.js']) | ||
|
||
<div class="card mb-3"> | ||
<h5 class="card-header">{{ $comment->user->name }}</h5> | ||
<div class="card-body"> | ||
<h5 class="card-title">{{ $comment->created_at }} (UTC)</h5> | ||
<p class="card-text">{{ $comment->comment }}</p> | ||
|
||
@auth | ||
@if ($comment->user_id == Auth::id()) | ||
<button class="btn btn-primary bi bi-pencil toggle-button" data-target="editComment{{ $comment->id }}"> Edit Comment</button> | ||
<form id="editComment{{ $comment->id }}" class="hidden" action="{{ route('comment.update', $comment->id) }}" method="POST"> | ||
@csrf | ||
@method('PUT') | ||
<input type="hidden" name="timeline_id" value="{{ $comment->timeline_id }}"> | ||
<input type="hidden" name="user_id" value="{{ $comment->user_id }}"> | ||
<div class="form-group"> | ||
<label for="inputComment">Modify comment</label> | ||
<textarea name="comment" rows="5" class="form-control" id="inputComment">{{ $comment->comment }}</textarea> | ||
</div> | ||
<button type="submit" class="btn btn-success bi bi-check2"> Confirm Edit</button> | ||
</form> | ||
|
||
<form action="{{ route('comment.destroy', $comment->id) }}" method="POST"> | ||
@csrf | ||
@method('DELETE') | ||
<button type="submit" class="btn btn-danger bi bi-trash delete-button"> Delete</button> | ||
</form> | ||
@endif | ||
@endauth | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters