engagement system

This commit is contained in:
2026-02-06 15:13:20 +02:00
parent 2e1ce0d93b
commit 7a99c3eb59
15 changed files with 1079 additions and 2 deletions
@@ -0,0 +1,43 @@
<?php
namespace App\Http\Controllers;
use App\Models\Bookmark;
use App\Models\Product;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class BookmarkController extends Controller
{
/**
* Toggle bookmark for a product.
*/
public function store(Product $product): JsonResponse
{
$user = Auth::user();
// Check if user already bookmarked
$existingBookmark = Bookmark::where('user_id', $user->id)
->where('product_id', $product->id)
->first();
if ($existingBookmark) {
// Remove bookmark
$existingBookmark->delete();
$bookmarked = false;
} else {
// Add bookmark
Bookmark::create([
'user_id' => $user->id,
'product_id' => $product->id,
]);
$bookmarked = true;
}
return response()->json([
'success' => true,
'bookmarked' => $bookmarked,
]);
}
}
@@ -0,0 +1,43 @@
<?php
namespace App\Http\Controllers;
use App\Models\Comment;
use App\Models\Product;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
class CommentController extends Controller
{
/**
* Store a newly created comment.
*/
public function store(Request $request, Product $product): RedirectResponse
{
$validated = $request->validate([
'body' => 'required|string|max:1000',
]);
Comment::create([
'user_id' => Auth::id(),
'product_id' => $product->id,
'body' => $validated['body'],
]);
return back()->with('success', 'Comment posted successfully!');
}
/**
* Remove the specified comment.
*/
public function destroy(Comment $comment): RedirectResponse
{
Gate::authorize('delete-comment', $comment);
$comment->delete();
return back()->with('success', 'Comment deleted successfully!');
}
}
+1 -1
View File
@@ -79,7 +79,7 @@ class ProductController extends Controller
abort(404);
}
$product->load(['user', 'category']);
$product->load(['user', 'category', 'comments.user', 'upvotes', 'bookmarks']);
return view('products.show', compact('product'));
}
+47
View File
@@ -0,0 +1,47 @@
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use App\Models\Upvote;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class UpvoteController extends Controller
{
/**
* Toggle upvote for a product.
*/
public function store(Product $product): JsonResponse
{
$user = Auth::user();
// Check if user already upvoted
$existingUpvote = Upvote::where('user_id', $user->id)
->where('product_id', $product->id)
->first();
if ($existingUpvote) {
// Remove upvote
$existingUpvote->delete();
$upvoted = false;
} else {
// Add upvote
Upvote::create([
'user_id' => $user->id,
'product_id' => $product->id,
]);
$upvoted = true;
}
// Get fresh upvotes count
$upvotesCount = $product->upvotes()->count();
return response()->json([
'success' => true,
'upvoted' => $upvoted,
'upvotesCount' => $upvotesCount,
]);
}
}