mirror of
https://github.com/m1ngsama/php.git
synced 2025-12-24 16:01:19 +00:00
Implement comment, voting, and user profile features
Added controllers for comments, voting system, user profiles, and home page. Includes nested comment support and karma calculation.
This commit is contained in:
parent
21ace0f38e
commit
a158e64985
4 changed files with 173 additions and 0 deletions
37
app/Http/Controllers/CommentController.php
Normal file
37
app/Http/Controllers/CommentController.php
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Comment;
|
||||
use App\Models\Post;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CommentController extends Controller
|
||||
{
|
||||
public function store(Request $request, Post $post)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'content' => 'required|string',
|
||||
'parent_id' => 'nullable|exists:comments,id',
|
||||
]);
|
||||
|
||||
$comment = Comment::create([
|
||||
'post_id' => $post->id,
|
||||
'user_id' => auth()->id(),
|
||||
'parent_id' => $validated['parent_id'] ?? null,
|
||||
'content' => $validated['content'],
|
||||
]);
|
||||
|
||||
return back()->with('success', 'Comment posted successfully!');
|
||||
}
|
||||
|
||||
public function destroy(Comment $comment)
|
||||
{
|
||||
if ($comment->user_id !== auth()->id()) {
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$comment->delete();
|
||||
return back()->with('success', 'Comment deleted successfully!');
|
||||
}
|
||||
}
|
||||
19
app/Http/Controllers/HomeController.php
Normal file
19
app/Http/Controllers/HomeController.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Post;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$posts = Post::with(['user', 'community', 'comments'])
|
||||
->orderBy('votes', 'desc')
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate(20);
|
||||
|
||||
return view('home', compact('posts'));
|
||||
}
|
||||
}
|
||||
24
app/Http/Controllers/UserController.php
Normal file
24
app/Http/Controllers/UserController.php
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function show(User $user)
|
||||
{
|
||||
$posts = $user->posts()
|
||||
->with(['community', 'comments'])
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate(20);
|
||||
|
||||
$comments = $user->comments()
|
||||
->with(['post', 'post.community'])
|
||||
->orderBy('created_at', 'desc')
|
||||
->paginate(20);
|
||||
|
||||
return view('users.show', compact('user', 'posts', 'comments'));
|
||||
}
|
||||
}
|
||||
93
app/Http/Controllers/VoteController.php
Normal file
93
app/Http/Controllers/VoteController.php
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Comment;
|
||||
use App\Models\Post;
|
||||
use App\Models\Vote;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class VoteController extends Controller
|
||||
{
|
||||
public function votePost(Request $request, Post $post)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'vote' => 'required|in:1,-1',
|
||||
]);
|
||||
|
||||
$existingVote = Vote::where([
|
||||
'user_id' => auth()->id(),
|
||||
'voteable_id' => $post->id,
|
||||
'voteable_type' => Post::class,
|
||||
])->first();
|
||||
|
||||
if ($existingVote) {
|
||||
if ($existingVote->vote == $validated['vote']) {
|
||||
$existingVote->delete();
|
||||
$this->updateVoteCount($post);
|
||||
return back();
|
||||
}
|
||||
$existingVote->update(['vote' => $validated['vote']]);
|
||||
} else {
|
||||
Vote::create([
|
||||
'user_id' => auth()->id(),
|
||||
'voteable_id' => $post->id,
|
||||
'voteable_type' => Post::class,
|
||||
'vote' => $validated['vote'],
|
||||
]);
|
||||
}
|
||||
|
||||
$this->updateVoteCount($post);
|
||||
$this->updateUserKarma($post->user);
|
||||
|
||||
return back();
|
||||
}
|
||||
|
||||
public function voteComment(Request $request, Comment $comment)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'vote' => 'required|in:1,-1',
|
||||
]);
|
||||
|
||||
$existingVote = Vote::where([
|
||||
'user_id' => auth()->id(),
|
||||
'voteable_id' => $comment->id,
|
||||
'voteable_type' => Comment::class,
|
||||
])->first();
|
||||
|
||||
if ($existingVote) {
|
||||
if ($existingVote->vote == $validated['vote']) {
|
||||
$existingVote->delete();
|
||||
$this->updateVoteCount($comment);
|
||||
return back();
|
||||
}
|
||||
$existingVote->update(['vote' => $validated['vote']]);
|
||||
} else {
|
||||
Vote::create([
|
||||
'user_id' => auth()->id(),
|
||||
'voteable_id' => $comment->id,
|
||||
'voteable_type' => Comment::class,
|
||||
'vote' => $validated['vote'],
|
||||
]);
|
||||
}
|
||||
|
||||
$this->updateVoteCount($comment);
|
||||
$this->updateUserKarma($comment->user);
|
||||
|
||||
return back();
|
||||
}
|
||||
|
||||
private function updateVoteCount($model)
|
||||
{
|
||||
$model->votes = $model->votes()->sum('vote');
|
||||
$model->save();
|
||||
}
|
||||
|
||||
private function updateUserKarma($user)
|
||||
{
|
||||
$postKarma = $user->posts()->sum('votes');
|
||||
$commentKarma = $user->comments()->sum('votes');
|
||||
$user->karma = $postKarma + $commentKarma;
|
||||
$user->save();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue