Skip to content

Commit

Permalink
Merge pull request #24 from HE-Arc/18-comment-section
Browse files Browse the repository at this point in the history
18 comment section
  • Loading branch information
katsulon authored Dec 6, 2024
2 parents 05a38c4 + 1b56c75 commit adddc67
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 19 deletions.
45 changes: 45 additions & 0 deletions timeliner/app/Http/Controllers/CommentController.php
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.');
}
}
7 changes: 5 additions & 2 deletions timeliner/app/Http/Controllers/TimelineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@ public function show($id)
->get();

Log::info('nodes: '.$nodes->count());
$comments = Comment::where('timeline', '=', $timeline->id);

$comments = Comment::where('timeline_id', '=', $timeline->id)
->with('user')
->get();

$isOwner = false;
if (Auth::check())
{
$isOwner = Ownership::find($timeline->id . Auth::user()->id);
}

return view('timeline.timeline', ['isOwner'=> $isOwner, 'timeline' => $timeline, 'nodes' => $nodes]);
return view('timeline.timeline', ['isOwner'=> $isOwner, 'timeline' => $timeline, 'nodes' => $nodes, 'comments' => $comments]);
}

return redirect()->route('timeline.index')
Expand Down
16 changes: 16 additions & 0 deletions timeliner/app/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,20 @@
class Comment extends Model
{
use HasFactory;

protected $fillable = [
'user_id',
'timeline_id',
'comment',
];

public function user()
{
return $this->belongsTo(User::class, 'user_id');
}

public function timeline()
{
return $this->belongsTo(Timeline::class, 'timeline_id');
}
}
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) {
//
});
}
};
32 changes: 32 additions & 0 deletions timeliner/database/seeders/CommentSeeder.php
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);
}
}
}
5 changes: 4 additions & 1 deletion timeliner/database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
DB::table('users')->truncate();
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
DB::table('users')->truncate();
DB::statement('SET FOREIGN_KEY_CHECKS=1;');

User::factory()->create([
'name' => 'Test User',
Expand All @@ -34,6 +36,7 @@ public function run(): void
]);

$this->call(TimelineSeeder::class);
$this->call(CommentSeeder::class);
$this->call(NodeSeeder::class);
$this->call(MilestoneSeeder::class);
}
Expand Down
7 changes: 4 additions & 3 deletions timeliner/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions timeliner/resources/js/formfunctions.js
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();
}
});
});
});
14 changes: 14 additions & 0 deletions timeliner/resources/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@
</h2>
</x-slot>

@if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@elseif($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif

<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
Expand Down
2 changes: 1 addition & 1 deletion timeliner/resources/views/layouts/navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="flex">
<!-- Logo -->
<div class="shrink-0 flex items-center">
<a href="{{ route('dashboard') }}">
<a href="{{ route('home') }}">
<!--<x-application-logo class="block h-9 w-auto fill-current text-gray-800 dark:text-gray-200" />-->
<x-application-logo class="block h-2 sm:h-2 md:h-2 w-auto max-h-4 fill-current text-gray-800 dark:text-gray-200" />

Expand Down
32 changes: 32 additions & 0 deletions timeliner/resources/views/timeline/partials/comment.blade.php
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>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<h5 class="card-title bi bi-lock-fill"> {{ $timeline->name }}</h5>
@endif
<p class="card-text">{{$timeline->description}}</p>
<a href="timeline/{{ $timeline->id }}" class="btn btn-primary stretched-link">View</a>
<a href="timeline/{{ $timeline->id }}" class="btn btn-primary stretched-link bi bi-eye"> View</a>
</div>
</div>
</div>
Expand Down
44 changes: 34 additions & 10 deletions timeliner/resources/views/timeline/timeline.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,29 @@
</h2>
</x-slot>

@vite(['resources/js/timelinelistener.js', 'resources/css/timelinestyle.css'])
@vite(['resources/js/timelinelistener.js', 'resources/css/timelinestyle.css', 'resources/js/formfunctions.js'])

@if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@elseif($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif

<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 text-gray-900 dark:text-gray-100">

@auth
@if ($isOwner) <a href="timeline/{{ $timeline->id }}/edit" class="btn btn-primary">Edit</a> @endif
@if ($isOwner) <a href="timeline/{{ $timeline->id }}/edit" class="btn btn-primary bi bi-pencil"> Edit</a> @endif
@endauth

<!-- Timeline show section -->
Expand All @@ -34,18 +48,28 @@


<!-- Comments to this timeline section -->

<div>
<h2>
Comments
</h2>
<h2>Comments</h2>

<!-- Add comment to this timeline section -->
@auth
Add comment
@endauth
</div>
<button class="btn btn-primary bi bi-plus-lg toggle-button" data-target="addComment"> Add comment</button>

<form id="addComment" class ="hidden" action="{{ route("comment.store") }}" method="POST">
@csrf
<input type="hidden" name="timeline_id" value="{{ $timeline->id }}">
<input type="hidden" name="user_id" value="{{ Auth::id() }}">
<div class="form-group">
<label for="inputComment">New comment</label>
<textarea name="comment" rows="5" class="form-control" id="inputComment">{{ old('comment') }}</textarea>
</div>
<button type="submit" class="btn btn-primary bi bi-plus-lg"> Add</button>
</form>

<!-- Show comments to this timeline section -->
@foreach ($comments as $comment)
@include("timeline.partials.comment", ["comment"=>$comment])
@endforeach
</div>
</div>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion timeliner/routes/web.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\Http\Controllers\TimelineController;
use App\Http\Controllers\CommentController;
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;

Expand All @@ -18,7 +19,6 @@
return view('timeline.createtimeline');
})->middleware(['auth', 'verified'])->name('createtimeline');


Route::middleware('auth')->group(function () {
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
Expand All @@ -28,3 +28,4 @@
require __DIR__.'/auth.php';

Route::resource('timeline', TimelineController::class);
Route::resource('comment', CommentController::class)->only(['store', 'destroy', 'update']);

0 comments on commit adddc67

Please sign in to comment.