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,
]);
}
}
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Bookmark extends Model
{
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'user_id',
'product_id',
];
/**
* Get the user that owns the bookmark.
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* Get the product that owns the bookmark.
*/
public function product(): BelongsTo
{
return $this->belongsTo(Product::class);
}
}
+36
View File
@@ -0,0 +1,36 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Comment extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'user_id',
'product_id',
'body',
];
/**
* Get the user that owns the comment.
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* Get the product that owns the comment.
*/
public function product(): BelongsTo
{
return $this->belongsTo(Product::class);
}
}
+64
View File
@@ -197,4 +197,68 @@ class Product extends Model
'approved_at' => null,
]);
}
/**
* Get the upvotes for the product.
*/
public function upvotes()
{
return $this->hasMany(Upvote::class);
}
/**
* Get the comments for the product.
*/
public function comments()
{
return $this->hasMany(Comment::class)->latest();
}
/**
* Get the bookmarks for the product.
*/
public function bookmarks()
{
return $this->hasMany(Bookmark::class);
}
/**
* Check if a user has upvoted the product.
*/
public function isUpvotedBy(?User $user): bool
{
if (!$user) {
return false;
}
return $this->upvotes()->where('user_id', $user->id)->exists();
}
/**
* Check if a user has bookmarked the product.
*/
public function isBookmarkedBy(?User $user): bool
{
if (!$user) {
return false;
}
return $this->bookmarks()->where('user_id', $user->id)->exists();
}
/**
* Get the upvotes count for the product.
*/
public function getUpvotesCountAttribute(): int
{
return $this->upvotes()->count();
}
/**
* Get the comments count for the product.
*/
public function getCommentsCountAttribute(): int
{
return $this->comments()->count();
}
}
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Upvote extends Model
{
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'user_id',
'product_id',
];
/**
* Get the user that owns the upvote.
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* Get the product that owns the upvote.
*/
public function product(): BelongsTo
{
return $this->belongsTo(Product::class);
}
}
+24
View File
@@ -120,4 +120,28 @@ class User extends Authenticatable
{
return $this->hasMany(Product::class);
}
/**
* Get the upvotes for the user.
*/
public function upvotes(): HasMany
{
return $this->hasMany(Upvote::class);
}
/**
* Get the comments for the user.
*/
public function comments(): HasMany
{
return $this->hasMany(Comment::class);
}
/**
* Get the bookmarks for the user.
*/
public function bookmarks(): HasMany
{
return $this->hasMany(Bookmark::class);
}
}