product submission
This commit is contained in:
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Product;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the products.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = Product::with(['user', 'category'])
|
||||
->approved()
|
||||
->latest();
|
||||
|
||||
if ($request->has('category')) {
|
||||
$query->where('category_id', $request->category);
|
||||
}
|
||||
|
||||
$products = $query->paginate(12);
|
||||
$categories = Category::all();
|
||||
|
||||
return view('products.index', compact('products', 'categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new product.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$categories = Category::all();
|
||||
return view('products.create', compact('categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created product in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
Gate::authorize('create-product');
|
||||
|
||||
$validated = $request->validate([
|
||||
'title' => 'required|string|max:255',
|
||||
'description' => 'required|string',
|
||||
'website_url' => 'required|url',
|
||||
'thumbnail' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
|
||||
'category_id' => 'required|exists:categories,id',
|
||||
]);
|
||||
|
||||
$validated['user_id'] = Auth::id();
|
||||
$validated['status'] = 'pending';
|
||||
|
||||
// Handle thumbnail upload
|
||||
if ($request->hasFile('thumbnail')) {
|
||||
$path = $request->file('thumbnail')->store('thumbnails', 'public');
|
||||
$validated['thumbnail'] = $path;
|
||||
}
|
||||
|
||||
$product = Product::create($validated);
|
||||
|
||||
return redirect()->route('products.show', $product)
|
||||
->with('success', 'Product submitted successfully! It will be reviewed by our moderators.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified product.
|
||||
*/
|
||||
public function show(Product $product)
|
||||
{
|
||||
// Only show approved products to non-owners
|
||||
if (!$product->isApproved() && $product->user_id !== Auth::id() && !Auth::user()?->hasAnyRole(['admin', 'moderator'])) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$product->load(['user', 'category']);
|
||||
|
||||
return view('products.show', compact('product'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified product.
|
||||
*/
|
||||
public function edit(Product $product)
|
||||
{
|
||||
Gate::authorize('edit-product', $product);
|
||||
|
||||
$categories = Category::all();
|
||||
return view('products.edit', compact('product', 'categories'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified product in storage.
|
||||
*/
|
||||
public function update(Request $request, Product $product)
|
||||
{
|
||||
Gate::authorize('edit-product', $product);
|
||||
|
||||
$validated = $request->validate([
|
||||
'title' => 'required|string|max:255',
|
||||
'description' => 'required|string',
|
||||
'website_url' => 'required|url',
|
||||
'thumbnail' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
|
||||
'category_id' => 'required|exists:categories,id',
|
||||
]);
|
||||
|
||||
// Handle thumbnail upload
|
||||
if ($request->hasFile('thumbnail')) {
|
||||
// Delete old thumbnail if exists
|
||||
if ($product->thumbnail) {
|
||||
Storage::disk('public')->delete($product->thumbnail);
|
||||
}
|
||||
|
||||
$path = $request->file('thumbnail')->store('thumbnails', 'public');
|
||||
$validated['thumbnail'] = $path;
|
||||
}
|
||||
|
||||
$product->update($validated);
|
||||
|
||||
return redirect()->route('products.show', $product)
|
||||
->with('success', 'Product updated successfully!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified product from storage.
|
||||
*/
|
||||
public function destroy(Product $product)
|
||||
{
|
||||
Gate::authorize('delete-product', $product);
|
||||
|
||||
// Delete thumbnail if exists
|
||||
if ($product->thumbnail) {
|
||||
Storage::disk('public')->delete($product->thumbnail);
|
||||
}
|
||||
|
||||
$product->delete();
|
||||
|
||||
return redirect()->route('products.index')
|
||||
->with('success', 'Product deleted successfully!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the product management dashboard for admin/moderator.
|
||||
*/
|
||||
public function manage(Request $request)
|
||||
{
|
||||
Gate::authorize('approve-product');
|
||||
|
||||
$query = Product::with(['user', 'category']);
|
||||
|
||||
// Filter by status
|
||||
if ($request->has('status') && in_array($request->status, ['pending', 'approved', 'rejected'])) {
|
||||
$query->byStatus($request->status);
|
||||
} else {
|
||||
$query->pending(); // Default to pending
|
||||
}
|
||||
|
||||
$products = $query->latest()->paginate(20);
|
||||
|
||||
$statusCounts = [
|
||||
'pending' => Product::pending()->count(),
|
||||
'approved' => Product::approved()->count(),
|
||||
'rejected' => Product::rejected()->count(),
|
||||
];
|
||||
|
||||
return view('products.manage', compact('products', 'statusCounts'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Approve a product.
|
||||
*/
|
||||
public function approve(Product $product)
|
||||
{
|
||||
Gate::authorize('approve-product');
|
||||
|
||||
$product->approve();
|
||||
|
||||
return back()->with('success', 'Product approved successfully!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reject a product.
|
||||
*/
|
||||
public function reject(Product $product)
|
||||
{
|
||||
Gate::authorize('reject-product');
|
||||
|
||||
$product->reject();
|
||||
|
||||
return back()->with('success', 'Product rejected.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Category extends Model
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'description',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the products for the category.
|
||||
*/
|
||||
public function products(): HasMany
|
||||
{
|
||||
return $this->hasMany(Product::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Product extends Model
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'slug',
|
||||
'description',
|
||||
'website_url',
|
||||
'thumbnail',
|
||||
'user_id',
|
||||
'category_id',
|
||||
'status',
|
||||
'approved_at',
|
||||
'rejected_at',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'approved_at' => 'datetime',
|
||||
'rejected_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Boot the model.
|
||||
*/
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function ($product) {
|
||||
if (empty($product->slug)) {
|
||||
$product->slug = static::generateUniqueSlug($product->title);
|
||||
}
|
||||
});
|
||||
|
||||
static::updating(function ($product) {
|
||||
if ($product->isDirty('title')) {
|
||||
$product->slug = static::generateUniqueSlug($product->title, $product->id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique slug from the title.
|
||||
*/
|
||||
protected static function generateUniqueSlug(string $title, ?int $ignoreId = null): string
|
||||
{
|
||||
$slug = Str::slug($title);
|
||||
$originalSlug = $slug;
|
||||
$counter = 1;
|
||||
|
||||
while (static::slugExists($slug, $ignoreId)) {
|
||||
$slug = $originalSlug . '-' . $counter;
|
||||
$counter++;
|
||||
}
|
||||
|
||||
return $slug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a slug exists.
|
||||
*/
|
||||
protected static function slugExists(string $slug, ?int $ignoreId = null): bool
|
||||
{
|
||||
$query = static::where('slug', $slug);
|
||||
|
||||
if ($ignoreId) {
|
||||
$query->where('id', '!=', $ignoreId);
|
||||
}
|
||||
|
||||
return $query->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user that owns the product.
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the category that owns the product.
|
||||
*/
|
||||
public function category(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Category::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope a query to only include approved products.
|
||||
*/
|
||||
public function scopeApproved(Builder $query): Builder
|
||||
{
|
||||
return $query->where('status', 'approved');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope a query to only include pending products.
|
||||
*/
|
||||
public function scopePending(Builder $query): Builder
|
||||
{
|
||||
return $query->where('status', 'pending');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope a query to only include rejected products.
|
||||
*/
|
||||
public function scopeRejected(Builder $query): Builder
|
||||
{
|
||||
return $query->where('status', 'rejected');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope a query to filter by status.
|
||||
*/
|
||||
public function scopeByStatus(Builder $query, string $status): Builder
|
||||
{
|
||||
return $query->where('status', $status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the product's thumbnail URL.
|
||||
*/
|
||||
public function getThumbnailUrlAttribute(): ?string
|
||||
{
|
||||
if ($this->thumbnail) {
|
||||
return asset('storage/' . $this->thumbnail);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the product is pending.
|
||||
*/
|
||||
public function isPending(): bool
|
||||
{
|
||||
return $this->status === 'pending';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the product is approved.
|
||||
*/
|
||||
public function isApproved(): bool
|
||||
{
|
||||
return $this->status === 'approved';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the product is rejected.
|
||||
*/
|
||||
public function isRejected(): bool
|
||||
{
|
||||
return $this->status === 'rejected';
|
||||
}
|
||||
|
||||
/**
|
||||
* Approve the product.
|
||||
*/
|
||||
public function approve(): void
|
||||
{
|
||||
$this->update([
|
||||
'status' => 'approved',
|
||||
'approved_at' => now(),
|
||||
'rejected_at' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reject the product.
|
||||
*/
|
||||
public function reject(): void
|
||||
{
|
||||
$this->update([
|
||||
'status' => 'rejected',
|
||||
'rejected_at' => now(),
|
||||
'approved_at' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace App\Models;
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
@@ -111,4 +112,12 @@ class User extends Authenticatable
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the products for the user.
|
||||
*/
|
||||
public function products(): HasMany
|
||||
{
|
||||
return $this->hasMany(Product::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
<?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::create('categories', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->string('slug')->unique();
|
||||
$table->text('description')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('categories');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
<?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::create('products', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('title');
|
||||
$table->string('slug')->unique();
|
||||
$table->text('description');
|
||||
$table->string('website_url');
|
||||
$table->string('thumbnail')->nullable();
|
||||
$table->foreignId('user_id')->constrained()->onDelete('cascade');
|
||||
$table->foreignId('category_id')->constrained()->onDelete('cascade');
|
||||
$table->string('status')->default('pending');
|
||||
$table->timestamp('approved_at')->nullable();
|
||||
$table->timestamp('rejected_at')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->index('status');
|
||||
$table->index('user_id');
|
||||
$table->index('category_id');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('products');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\Category;
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class CategorySeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$categories = [
|
||||
[
|
||||
'name' => 'Developer Tools',
|
||||
'description' => 'Tools and services for developers',
|
||||
],
|
||||
[
|
||||
'name' => 'Productivity',
|
||||
'description' => 'Apps and tools to boost productivity',
|
||||
],
|
||||
[
|
||||
'name' => 'Design',
|
||||
'description' => 'Design tools and resources',
|
||||
],
|
||||
[
|
||||
'name' => 'Marketing',
|
||||
'description' => 'Marketing tools and platforms',
|
||||
],
|
||||
[
|
||||
'name' => 'AI & Machine Learning',
|
||||
'description' => 'AI-powered tools and services',
|
||||
],
|
||||
[
|
||||
'name' => 'Analytics',
|
||||
'description' => 'Data analytics and tracking tools',
|
||||
],
|
||||
[
|
||||
'name' => 'Communication',
|
||||
'description' => 'Communication and collaboration tools',
|
||||
],
|
||||
[
|
||||
'name' => 'E-commerce',
|
||||
'description' => 'E-commerce platforms and tools',
|
||||
],
|
||||
[
|
||||
'name' => 'Education',
|
||||
'description' => 'Educational tools and platforms',
|
||||
],
|
||||
[
|
||||
'name' => 'Finance',
|
||||
'description' => 'Financial services and tools',
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($categories as $category) {
|
||||
Category::create([
|
||||
'name' => $category['name'],
|
||||
'slug' => Str::slug($category['name']),
|
||||
'description' => $category['description'],
|
||||
]);
|
||||
}
|
||||
|
||||
$this->command->info('Categories created successfully!');
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ class DatabaseSeeder extends Seeder
|
||||
{
|
||||
$this->call([
|
||||
RoleSeeder::class,
|
||||
CategorySeeder::class,
|
||||
]);
|
||||
|
||||
// Uncomment to create additional users with factory
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="container">
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-md-10">
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h3 class="mb-0">Submit a Product</h3>
|
||||
</div>
|
||||
|
||||
<div class="card-body">
|
||||
<form method="POST" action="{{ route('products.store') }}" enctype="multipart/form-data">
|
||||
@csrf
|
||||
|
||||
<!-- Title -->
|
||||
<div class="mb-3">
|
||||
<label for="title" class="form-label">Product Title <span class="text-danger">*</span></label>
|
||||
<input type="text"
|
||||
class="form-control @error('title') is-invalid @enderror"
|
||||
id="title"
|
||||
name="title"
|
||||
value="{{ old('title') }}"
|
||||
required
|
||||
maxlength="255"
|
||||
placeholder="e.g., My Awesome Product">
|
||||
@error('title')
|
||||
<span class="invalid-feedback" role="alert">
|
||||
<strong>{{ $message }}</strong>
|
||||
</span>
|
||||
@enderror
|
||||
<small class="form-text text-muted">A catchy, descriptive title for your product.</small>
|
||||
</div>
|
||||
|
||||
<!-- Description -->
|
||||
<div class="mb-3">
|
||||
<label for="description" class="form-label">Description <span class="text-danger">*</span></label>
|
||||
<textarea class="form-control @error('description') is-invalid @enderror"
|
||||
id="description"
|
||||
name="description"
|
||||
rows="5"
|
||||
required
|
||||
placeholder="Tell us about your product...">{{ old('description') }}</textarea>
|
||||
@error('description')
|
||||
<span class="invalid-feedback" role="alert">
|
||||
<strong>{{ $message }}</strong>
|
||||
</span>
|
||||
@enderror
|
||||
<small class="form-text text-muted">Provide a detailed description of your product (what it does, key features, etc.).</small>
|
||||
</div>
|
||||
|
||||
<!-- Website URL -->
|
||||
<div class="mb-3">
|
||||
<label for="website_url" class="form-label">Website URL <span class="text-danger">*</span></label>
|
||||
<input type="url"
|
||||
class="form-control @error('website_url') is-invalid @enderror"
|
||||
id="website_url"
|
||||
name="website_url"
|
||||
value="{{ old('website_url') }}"
|
||||
required
|
||||
placeholder="https://example.com">
|
||||
@error('website_url')
|
||||
<span class="invalid-feedback" role="alert">
|
||||
<strong>{{ $message }}</strong>
|
||||
</span>
|
||||
@enderror
|
||||
<small class="form-text text-muted">The full URL to your product's website or landing page.</small>
|
||||
</div>
|
||||
|
||||
<!-- Category -->
|
||||
<div class="mb-3">
|
||||
<label for="category_id" class="form-label">Category <span class="text-danger">*</span></label>
|
||||
<select class="form-select @error('category_id') is-invalid @enderror"
|
||||
id="category_id"
|
||||
name="category_id"
|
||||
required>
|
||||
<option value="">Select a category...</option>
|
||||
@foreach($categories as $category)
|
||||
<option value="{{ $category->id }}" {{ old('category_id') == $category->id ? 'selected' : '' }}>
|
||||
{{ $category->name }}
|
||||
</option>
|
||||
@endforeach
|
||||
</select>
|
||||
@error('category_id')
|
||||
<span class="invalid-feedback" role="alert">
|
||||
<strong>{{ $message }}</strong>
|
||||
</span>
|
||||
@enderror
|
||||
<small class="form-text text-muted">Choose the category that best fits your product.</small>
|
||||
</div>
|
||||
|
||||
<!-- Thumbnail -->
|
||||
<div class="mb-3">
|
||||
<label for="thumbnail" class="form-label">Product Thumbnail</label>
|
||||
<input type="file"
|
||||
class="form-control @error('thumbnail') is-invalid @enderror"
|
||||
id="thumbnail"
|
||||
name="thumbnail"
|
||||
accept="image/*">
|
||||
@error('thumbnail')
|
||||
<span class="invalid-feedback" role="alert">
|
||||
<strong>{{ $message }}</strong>
|
||||
</span>
|
||||
@enderror
|
||||
<small class="form-text text-muted">Upload a product screenshot or logo (JPEG, PNG, or GIF, max 2MB).</small>
|
||||
</div>
|
||||
|
||||
<!-- Image Preview -->
|
||||
<div class="mb-3" id="imagePreview" style="display: none;">
|
||||
<label class="form-label">Preview:</label>
|
||||
<div>
|
||||
<img id="preview" src="#" alt="Thumbnail preview" style="max-width: 300px; max-height: 300px;" class="img-thumbnail">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Submit Button -->
|
||||
<div class="d-flex justify-content-between">
|
||||
<a href="{{ route('products.index') }}" class="btn btn-secondary">Cancel</a>
|
||||
<button type="submit" class="btn btn-primary">Submit Product</button>
|
||||
</div>
|
||||
|
||||
<div class="alert alert-info mt-3" role="alert">
|
||||
<strong>Note:</strong> Your product will be reviewed by our moderators before it appears on the site.
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@push('scripts')
|
||||
<script>
|
||||
document.getElementById('thumbnail').addEventListener('change', function(e) {
|
||||
const file = e.target.files[0];
|
||||
if (file) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function(e) {
|
||||
document.getElementById('preview').src = e.target.result;
|
||||
document.getElementById('imagePreview').style.display = 'block';
|
||||
}
|
||||
reader.readAsDataURL(file);
|
||||
} else {
|
||||
document.getElementById('imagePreview').style.display = 'none';
|
||||
}
|
||||
});
|
||||
</script>
|
||||
@endpush
|
||||
@endsection
|
||||
@@ -0,0 +1,152 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="container">
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-md-10">
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h3 class="mb-0">Edit Product</h3>
|
||||
</div>
|
||||
|
||||
<div class="card-body">
|
||||
<form method="POST" action="{{ route('products.update', $product) }}" enctype="multipart/form-data">
|
||||
@csrf
|
||||
@method('PUT')
|
||||
|
||||
<!-- Title -->
|
||||
<div class="mb-3">
|
||||
<label for="title" class="form-label">Product Title <span class="text-danger">*</span></label>
|
||||
<input type="text"
|
||||
class="form-control @error('title') is-invalid @enderror"
|
||||
id="title"
|
||||
name="title"
|
||||
value="{{ old('title', $product->title) }}"
|
||||
required
|
||||
maxlength="255">
|
||||
@error('title')
|
||||
<span class="invalid-feedback" role="alert">
|
||||
<strong>{{ $message }}</strong>
|
||||
</span>
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
<!-- Description -->
|
||||
<div class="mb-3">
|
||||
<label for="description" class="form-label">Description <span class="text-danger">*</span></label>
|
||||
<textarea class="form-control @error('description') is-invalid @enderror"
|
||||
id="description"
|
||||
name="description"
|
||||
rows="5"
|
||||
required>{{ old('description', $product->description) }}</textarea>
|
||||
@error('description')
|
||||
<span class="invalid-feedback" role="alert">
|
||||
<strong>{{ $message }}</strong>
|
||||
</span>
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
<!-- Website URL -->
|
||||
<div class="mb-3">
|
||||
<label for="website_url" class="form-label">Website URL <span class="text-danger">*</span></label>
|
||||
<input type="url"
|
||||
class="form-control @error('website_url') is-invalid @enderror"
|
||||
id="website_url"
|
||||
name="website_url"
|
||||
value="{{ old('website_url', $product->website_url) }}"
|
||||
required>
|
||||
@error('website_url')
|
||||
<span class="invalid-feedback" role="alert">
|
||||
<strong>{{ $message }}</strong>
|
||||
</span>
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
<!-- Category -->
|
||||
<div class="mb-3">
|
||||
<label for="category_id" class="form-label">Category <span class="text-danger">*</span></label>
|
||||
<select class="form-select @error('category_id') is-invalid @enderror"
|
||||
id="category_id"
|
||||
name="category_id"
|
||||
required>
|
||||
<option value="">Select a category...</option>
|
||||
@foreach($categories as $category)
|
||||
<option value="{{ $category->id }}"
|
||||
{{ old('category_id', $product->category_id) == $category->id ? 'selected' : '' }}>
|
||||
{{ $category->name }}
|
||||
</option>
|
||||
@endforeach
|
||||
</select>
|
||||
@error('category_id')
|
||||
<span class="invalid-feedback" role="alert">
|
||||
<strong>{{ $message }}</strong>
|
||||
</span>
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
<!-- Current Thumbnail -->
|
||||
@if($product->thumbnail_url)
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Current Thumbnail:</label>
|
||||
<div>
|
||||
<img src="{{ $product->thumbnail_url }}" alt="{{ $product->title }}" style="max-width: 300px; max-height: 300px;" class="img-thumbnail">
|
||||
</div>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<!-- New Thumbnail -->
|
||||
<div class="mb-3">
|
||||
<label for="thumbnail" class="form-label">
|
||||
{{ $product->thumbnail_url ? 'Replace Thumbnail' : 'Product Thumbnail' }}
|
||||
</label>
|
||||
<input type="file"
|
||||
class="form-control @error('thumbnail') is-invalid @enderror"
|
||||
id="thumbnail"
|
||||
name="thumbnail"
|
||||
accept="image/*">
|
||||
@error('thumbnail')
|
||||
<span class="invalid-feedback" role="alert">
|
||||
<strong>{{ $message }}</strong>
|
||||
</span>
|
||||
@enderror
|
||||
<small class="form-text text-muted">JPEG, PNG, or GIF, max 2MB.</small>
|
||||
</div>
|
||||
|
||||
<!-- Image Preview -->
|
||||
<div class="mb-3" id="imagePreview" style="display: none;">
|
||||
<label class="form-label">New Preview:</label>
|
||||
<div>
|
||||
<img id="preview" src="#" alt="Thumbnail preview" style="max-width: 300px; max-height: 300px;" class="img-thumbnail">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Submit Button -->
|
||||
<div class="d-flex justify-content-between">
|
||||
<a href="{{ route('products.show', $product) }}" class="btn btn-secondary">Cancel</a>
|
||||
<button type="submit" class="btn btn-primary">Update Product</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@push('scripts')
|
||||
<script>
|
||||
document.getElementById('thumbnail').addEventListener('change', function(e) {
|
||||
const file = e.target.files[0];
|
||||
if (file) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function(e) {
|
||||
document.getElementById('preview').src = e.target.result;
|
||||
document.getElementById('imagePreview').style.display = 'block';
|
||||
}
|
||||
reader.readAsDataURL(file);
|
||||
} else {
|
||||
document.getElementById('imagePreview').style.display = 'none';
|
||||
}
|
||||
});
|
||||
</script>
|
||||
@endpush
|
||||
@endsection
|
||||
@@ -0,0 +1,78 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||
<h1>Discover Products</h1>
|
||||
@auth
|
||||
<a href="{{ route('products.create') }}" class="btn btn-primary">Submit a Product</a>
|
||||
@endauth
|
||||
</div>
|
||||
|
||||
<!-- Categories Filter -->
|
||||
<div class="mb-4">
|
||||
<a href="{{ route('products.index') }}" class="btn btn-sm {{ !request('category') ? 'btn-primary' : 'btn-outline-primary' }}">
|
||||
All Categories
|
||||
</a>
|
||||
@foreach($categories as $category)
|
||||
<a href="{{ route('products.index', ['category' => $category->id]) }}"
|
||||
class="btn btn-sm {{ request('category') == $category->id ? 'btn-primary' : 'btn-outline-primary' }}">
|
||||
{{ $category->name }}
|
||||
</a>
|
||||
@endforeach
|
||||
</div>
|
||||
|
||||
@if(session('success'))
|
||||
<div class="alert alert-success alert-dismissible fade show" role="alert">
|
||||
{{ session('success') }}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<!-- Products Grid -->
|
||||
<div class="row">
|
||||
@forelse($products as $product)
|
||||
<div class="col-md-4 mb-4">
|
||||
<div class="card h-100">
|
||||
@if($product->thumbnail_url)
|
||||
<img src="{{ $product->thumbnail_url }}" class="card-img-top" alt="{{ $product->title }}" style="height: 200px; object-fit: cover;">
|
||||
@else
|
||||
<div class="bg-light d-flex align-items-center justify-content-center" style="height: 200px;">
|
||||
<span class="text-muted">No image</span>
|
||||
</div>
|
||||
@endif
|
||||
<div class="card-body d-flex flex-column">
|
||||
<h5 class="card-title">{{ $product->title }}</h5>
|
||||
<p class="card-text text-muted small mb-2">
|
||||
<span class="badge bg-secondary">{{ $product->category->name }}</span>
|
||||
</p>
|
||||
<p class="card-text flex-grow-1">{{ Str::limit($product->description, 100) }}</p>
|
||||
<div class="mt-auto">
|
||||
<a href="{{ route('products.show', $product) }}" class="btn btn-sm btn-primary">View Details</a>
|
||||
<a href="{{ $product->website_url }}" target="_blank" rel="noopener" class="btn btn-sm btn-outline-primary">Visit Website</a>
|
||||
</div>
|
||||
<div class="mt-2 text-muted small">
|
||||
By {{ $product->user->name }} · {{ $product->created_at->diffForHumans() }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@empty
|
||||
<div class="col-md-12">
|
||||
<div class="alert alert-info">
|
||||
No products found. Be the first to submit one!
|
||||
</div>
|
||||
</div>
|
||||
@endforelse
|
||||
</div>
|
||||
|
||||
<!-- Pagination -->
|
||||
<div class="d-flex justify-content-center">
|
||||
{{ $products->links() }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
@@ -0,0 +1,168 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="container-fluid">
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<h1 class="mb-4">Product Management</h1>
|
||||
|
||||
@if(session('success'))
|
||||
<div class="alert alert-success alert-dismissible fade show" role="alert">
|
||||
{{ session('success') }}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<!-- Status Filter -->
|
||||
<div class="card mb-4">
|
||||
<div class="card-body">
|
||||
<div class="row">
|
||||
<div class="col-md-12">
|
||||
<h5 class="mb-3">Filter by Status</h5>
|
||||
<a href="{{ route('products.manage') }}"
|
||||
class="btn btn-sm {{ !request('status') || request('status') == 'pending' ? 'btn-warning' : 'btn-outline-warning' }}">
|
||||
Pending ({{ $statusCounts['pending'] }})
|
||||
</a>
|
||||
<a href="{{ route('products.manage', ['status' => 'approved']) }}"
|
||||
class="btn btn-sm {{ request('status') == 'approved' ? 'btn-success' : 'btn-outline-success' }}">
|
||||
Approved ({{ $statusCounts['approved'] }})
|
||||
</a>
|
||||
<a href="{{ route('products.manage', ['status' => 'rejected']) }}"
|
||||
class="btn btn-sm {{ request('status') == 'rejected' ? 'btn-danger' : 'btn-outline-danger' }}">
|
||||
Rejected ({{ $statusCounts['rejected'] }})
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Products Table -->
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
@if($products->count() > 0)
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Thumbnail</th>
|
||||
<th>Title</th>
|
||||
<th>Category</th>
|
||||
<th>Submitted By</th>
|
||||
<th>Status</th>
|
||||
<th>Date</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach($products as $product)
|
||||
<tr>
|
||||
<td>
|
||||
@if($product->thumbnail_url)
|
||||
<img src="{{ $product->thumbnail_url }}"
|
||||
alt="{{ $product->title }}"
|
||||
style="width: 60px; height: 60px; object-fit: cover;"
|
||||
class="rounded">
|
||||
@else
|
||||
<div class="bg-light d-flex align-items-center justify-content-center rounded"
|
||||
style="width: 60px; height: 60px;">
|
||||
<span class="text-muted small">No img</span>
|
||||
</div>
|
||||
@endif
|
||||
</td>
|
||||
<td>
|
||||
<a href="{{ route('products.show', $product) }}" class="fw-bold text-decoration-none">
|
||||
{{ $product->title }}
|
||||
</a>
|
||||
<br>
|
||||
<small class="text-muted">{{ Str::limit($product->description, 60) }}</small>
|
||||
</td>
|
||||
<td>
|
||||
<span class="badge bg-secondary">{{ $product->category->name }}</span>
|
||||
</td>
|
||||
<td>
|
||||
<a href="{{ route('profile.show', $product->user) }}">
|
||||
{{ $product->user->name }}
|
||||
</a>
|
||||
</td>
|
||||
<td>
|
||||
@if($product->isPending())
|
||||
<span class="badge bg-warning text-dark">Pending</span>
|
||||
@elseif($product->isApproved())
|
||||
<span class="badge bg-success">Approved</span>
|
||||
@elseif($product->isRejected())
|
||||
<span class="badge bg-danger">Rejected</span>
|
||||
@endif
|
||||
</td>
|
||||
<td>
|
||||
<small>{{ $product->created_at->format('M j, Y') }}</small>
|
||||
<br>
|
||||
<small class="text-muted">{{ $product->created_at->diffForHumans() }}</small>
|
||||
</td>
|
||||
<td>
|
||||
<div class="btn-group btn-group-sm" role="group">
|
||||
@if($product->isPending() || $product->isRejected())
|
||||
<form action="{{ route('products.approve', $product) }}" method="POST" class="d-inline">
|
||||
@csrf
|
||||
<button type="submit" class="btn btn-success btn-sm" title="Approve">
|
||||
✓
|
||||
</button>
|
||||
</form>
|
||||
@endif
|
||||
|
||||
@if($product->isPending() || $product->isApproved())
|
||||
<form action="{{ route('products.reject', $product) }}" method="POST" class="d-inline">
|
||||
@csrf
|
||||
<button type="submit"
|
||||
class="btn btn-danger btn-sm"
|
||||
title="Reject"
|
||||
onclick="return confirm('Are you sure you want to reject this product?');">
|
||||
✗
|
||||
</button>
|
||||
</form>
|
||||
@endif
|
||||
|
||||
<a href="{{ route('products.show', $product) }}" class="btn btn-info btn-sm" title="View">
|
||||
👁
|
||||
</a>
|
||||
|
||||
@can('edit-product', $product)
|
||||
<a href="{{ route('products.edit', $product) }}" class="btn btn-primary btn-sm" title="Edit">
|
||||
✎
|
||||
</a>
|
||||
@endcan
|
||||
|
||||
@can('delete-product', $product)
|
||||
<form action="{{ route('products.destroy', $product) }}" method="POST" class="d-inline">
|
||||
@csrf
|
||||
@method('DELETE')
|
||||
<button type="submit"
|
||||
class="btn btn-danger btn-sm"
|
||||
title="Delete"
|
||||
onclick="return confirm('Are you sure you want to delete this product permanently?');">
|
||||
🗑
|
||||
</button>
|
||||
</form>
|
||||
@endcan
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<!-- Pagination -->
|
||||
<div class="d-flex justify-content-center mt-4">
|
||||
{{ $products->appends(['status' => request('status')])->links() }}
|
||||
</div>
|
||||
@else
|
||||
<div class="alert alert-info">
|
||||
No products found with status: {{ request('status', 'pending') }}
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
@@ -0,0 +1,114 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
@section('content')
|
||||
<div class="container">
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-md-10">
|
||||
@if(session('success'))
|
||||
<div class="alert alert-success alert-dismissible fade show" role="alert">
|
||||
{{ session('success') }}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<!-- Product Header -->
|
||||
<div class="d-flex justify-content-between align-items-start mb-4">
|
||||
<div class="flex-grow-1">
|
||||
<h1 class="mb-2">{{ $product->title }}</h1>
|
||||
<p class="text-muted mb-0">
|
||||
<span class="badge bg-secondary">{{ $product->category->name }}</span>
|
||||
@if($product->isPending())
|
||||
<span class="badge bg-warning text-dark">Pending Review</span>
|
||||
@elseif($product->isApproved())
|
||||
<span class="badge bg-success">Approved</span>
|
||||
@elseif($product->isRejected())
|
||||
<span class="badge bg-danger">Rejected</span>
|
||||
@endif
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
@can('edit-product', $product)
|
||||
<a href="{{ route('products.edit', $product) }}" class="btn btn-sm btn-outline-primary">Edit</a>
|
||||
@endcan
|
||||
@can('delete-product', $product)
|
||||
<form action="{{ route('products.destroy', $product) }}" method="POST" class="d-inline" onsubmit="return confirm('Are you sure you want to delete this product?');">
|
||||
@csrf
|
||||
@method('DELETE')
|
||||
<button type="submit" class="btn btn-sm btn-outline-danger">Delete</button>
|
||||
</form>
|
||||
@endcan
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Product Thumbnail -->
|
||||
@if($product->thumbnail_url)
|
||||
<div class="mb-4">
|
||||
<img src="{{ $product->thumbnail_url }}" alt="{{ $product->title }}" class="img-fluid rounded" style="max-height: 400px;">
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<!-- Product Description -->
|
||||
<div class="mb-4">
|
||||
<h4>About</h4>
|
||||
<p class="lead">{{ $product->description }}</p>
|
||||
</div>
|
||||
|
||||
<!-- Product Links -->
|
||||
<div class="mb-4">
|
||||
<a href="{{ $product->website_url }}" target="_blank" rel="noopener" class="btn btn-primary btn-lg">
|
||||
Visit Website →
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<!-- Product Metadata -->
|
||||
<hr>
|
||||
<div class="row text-muted small">
|
||||
<div class="col-md-6">
|
||||
<strong>Submitted by:</strong>
|
||||
<a href="{{ route('profile.show', $product->user) }}">{{ $product->user->name }}</a>
|
||||
</div>
|
||||
<div class="col-md-6 text-md-end">
|
||||
<strong>Submitted:</strong> {{ $product->created_at->format('F j, Y') }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if($product->isApproved() && $product->approved_at)
|
||||
<div class="row text-muted small mt-2">
|
||||
<div class="col-md-12">
|
||||
<strong>Approved:</strong> {{ $product->approved_at->format('F j, Y') }}
|
||||
</div>
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Moderator Actions -->
|
||||
@can('approve-product')
|
||||
@if($product->isPending() || $product->isRejected())
|
||||
<div class="card mt-3">
|
||||
<div class="card-header bg-warning">
|
||||
<h5 class="mb-0">Moderator Actions</h5>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form action="{{ route('products.approve', $product) }}" method="POST" class="d-inline">
|
||||
@csrf
|
||||
<button type="submit" class="btn btn-success">Approve Product</button>
|
||||
</form>
|
||||
<form action="{{ route('products.reject', $product) }}" method="POST" class="d-inline">
|
||||
@csrf
|
||||
<button type="submit" class="btn btn-danger" onclick="return confirm('Are you sure you want to reject this product?');">Reject Product</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
@endif
|
||||
@endcan
|
||||
|
||||
<div class="mt-3">
|
||||
<a href="{{ route('products.index') }}" class="btn btn-secondary">← Back to Products</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
@@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\ProductController;
|
||||
use App\Http\Controllers\ProfileController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
@@ -20,6 +21,25 @@ Route::middleware(['auth'])->group(function () {
|
||||
|
||||
Route::get('/profile/{user}', [ProfileController::class, 'show'])->name('profile.show');
|
||||
|
||||
// Product Routes
|
||||
Route::get('/products', [ProductController::class, 'index'])->name('products.index');
|
||||
Route::get('/products/{product}', [ProductController::class, 'show'])->name('products.show');
|
||||
|
||||
Route::middleware(['auth'])->group(function () {
|
||||
Route::get('/products-submit', [ProductController::class, 'create'])->name('products.create');
|
||||
Route::post('/products', [ProductController::class, 'store'])->name('products.store');
|
||||
Route::get('/products/{product}/edit', [ProductController::class, 'edit'])->name('products.edit');
|
||||
Route::put('/products/{product}', [ProductController::class, 'update'])->name('products.update');
|
||||
Route::delete('/products/{product}', [ProductController::class, 'destroy'])->name('products.destroy');
|
||||
});
|
||||
|
||||
// Admin/Moderator Product Management Routes
|
||||
Route::middleware(['auth', 'moderator'])->group(function () {
|
||||
Route::get('/products-manage', [ProductController::class, 'manage'])->name('products.manage');
|
||||
Route::post('/products/{product}/approve', [ProductController::class, 'approve'])->name('products.approve');
|
||||
Route::post('/products/{product}/reject', [ProductController::class, 'reject'])->name('products.reject');
|
||||
});
|
||||
|
||||
// Admin Routes (example)
|
||||
Route::middleware(['auth', 'admin'])->prefix('admin')->name('admin.')->group(function () {
|
||||
Route::get('/dashboard', function () {
|
||||
|
||||
@@ -0,0 +1,415 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Product;
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
Storage::fake('public');
|
||||
|
||||
// Create roles
|
||||
$this->userRole = Role::create([
|
||||
'name' => 'User',
|
||||
'slug' => 'user',
|
||||
'description' => 'Regular user',
|
||||
]);
|
||||
|
||||
$this->moderatorRole = Role::create([
|
||||
'name' => 'Moderator',
|
||||
'slug' => 'moderator',
|
||||
'description' => 'Moderator',
|
||||
]);
|
||||
|
||||
$this->adminRole = Role::create([
|
||||
'name' => 'Admin',
|
||||
'slug' => 'admin',
|
||||
'description' => 'Admin',
|
||||
]);
|
||||
|
||||
// Create category
|
||||
$this->category = Category::create([
|
||||
'name' => 'Test Category',
|
||||
'slug' => 'test-category',
|
||||
'description' => 'Test description',
|
||||
]);
|
||||
});
|
||||
|
||||
test('guests can view approved products', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$product = Product::create([
|
||||
'title' => 'Test Product',
|
||||
'slug' => 'test-product',
|
||||
'description' => 'Test description',
|
||||
'website_url' => 'https://example.com',
|
||||
'user_id' => $user->id,
|
||||
'category_id' => $this->category->id,
|
||||
'status' => 'approved',
|
||||
]);
|
||||
|
||||
$response = $this->get(route('products.index'));
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee('Test Product');
|
||||
});
|
||||
|
||||
test('guests cannot view pending products', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$product = Product::create([
|
||||
'title' => 'Pending Product',
|
||||
'slug' => 'pending-product',
|
||||
'description' => 'Test description',
|
||||
'website_url' => 'https://example.com',
|
||||
'user_id' => $user->id,
|
||||
'category_id' => $this->category->id,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
$response = $this->get(route('products.show', $product));
|
||||
|
||||
$response->assertStatus(404);
|
||||
});
|
||||
|
||||
test('authenticated users can submit products', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$response = $this->actingAs($user)->post(route('products.store'), [
|
||||
'title' => 'New Product',
|
||||
'description' => 'This is a new product',
|
||||
'website_url' => 'https://newproduct.com',
|
||||
'category_id' => $this->category->id,
|
||||
]);
|
||||
|
||||
$response->assertRedirect();
|
||||
$this->assertDatabaseHas('products', [
|
||||
'title' => 'New Product',
|
||||
'user_id' => $user->id,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
});
|
||||
|
||||
test('guests cannot submit products', function () {
|
||||
$response = $this->get(route('products.create'));
|
||||
|
||||
$response->assertRedirect(route('login'));
|
||||
});
|
||||
|
||||
test('product submission requires title', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$response = $this->actingAs($user)->post(route('products.store'), [
|
||||
'description' => 'Test description',
|
||||
'website_url' => 'https://example.com',
|
||||
'category_id' => $this->category->id,
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors('title');
|
||||
});
|
||||
|
||||
test('product submission requires description', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$response = $this->actingAs($user)->post(route('products.store'), [
|
||||
'title' => 'Test Product',
|
||||
'website_url' => 'https://example.com',
|
||||
'category_id' => $this->category->id,
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors('description');
|
||||
});
|
||||
|
||||
test('product submission requires valid url', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$response = $this->actingAs($user)->post(route('products.store'), [
|
||||
'title' => 'Test Product',
|
||||
'description' => 'Test description',
|
||||
'website_url' => 'not-a-valid-url',
|
||||
'category_id' => $this->category->id,
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors('website_url');
|
||||
});
|
||||
|
||||
test('product submission requires valid category', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$response = $this->actingAs($user)->post(route('products.store'), [
|
||||
'title' => 'Test Product',
|
||||
'description' => 'Test description',
|
||||
'website_url' => 'https://example.com',
|
||||
'category_id' => 999,
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors('category_id');
|
||||
});
|
||||
|
||||
test('users can upload thumbnail when submitting product', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$file = UploadedFile::fake()->image('thumbnail.jpg');
|
||||
|
||||
$response = $this->actingAs($user)->post(route('products.store'), [
|
||||
'title' => 'Test Product',
|
||||
'description' => 'Test description',
|
||||
'website_url' => 'https://example.com',
|
||||
'category_id' => $this->category->id,
|
||||
'thumbnail' => $file,
|
||||
]);
|
||||
|
||||
$product = Product::where('title', 'Test Product')->first();
|
||||
|
||||
expect($product->thumbnail)->not->toBeNull();
|
||||
Storage::disk('public')->assertExists($product->thumbnail);
|
||||
});
|
||||
|
||||
test('thumbnail must be an image', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$file = UploadedFile::fake()->create('document.pdf');
|
||||
|
||||
$response = $this->actingAs($user)->post(route('products.store'), [
|
||||
'title' => 'Test Product',
|
||||
'description' => 'Test description',
|
||||
'website_url' => 'https://example.com',
|
||||
'category_id' => $this->category->id,
|
||||
'thumbnail' => $file,
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors('thumbnail');
|
||||
});
|
||||
|
||||
test('product owner can edit their product', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$product = Product::create([
|
||||
'title' => 'Test Product',
|
||||
'slug' => 'test-product',
|
||||
'description' => 'Test description',
|
||||
'website_url' => 'https://example.com',
|
||||
'user_id' => $user->id,
|
||||
'category_id' => $this->category->id,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->put(route('products.update', $product), [
|
||||
'title' => 'Updated Product',
|
||||
'description' => 'Updated description',
|
||||
'website_url' => 'https://updated.com',
|
||||
'category_id' => $this->category->id,
|
||||
]);
|
||||
|
||||
$response->assertRedirect();
|
||||
$this->assertDatabaseHas('products', [
|
||||
'id' => $product->id,
|
||||
'title' => 'Updated Product',
|
||||
]);
|
||||
});
|
||||
|
||||
test('non-owner cannot edit product', function () {
|
||||
$owner = User::factory()->create();
|
||||
$owner->roles()->attach($this->userRole);
|
||||
|
||||
$other = User::factory()->create();
|
||||
$other->roles()->attach($this->userRole);
|
||||
|
||||
$product = Product::create([
|
||||
'title' => 'Test Product',
|
||||
'slug' => 'test-product',
|
||||
'description' => 'Test description',
|
||||
'website_url' => 'https://example.com',
|
||||
'user_id' => $owner->id,
|
||||
'category_id' => $this->category->id,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($other)->put(route('products.update', $product), [
|
||||
'title' => 'Updated Product',
|
||||
'description' => 'Updated description',
|
||||
'website_url' => 'https://updated.com',
|
||||
'category_id' => $this->category->id,
|
||||
]);
|
||||
|
||||
$response->assertStatus(403);
|
||||
});
|
||||
|
||||
test('product owner can delete their product', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$product = Product::create([
|
||||
'title' => 'Test Product',
|
||||
'slug' => 'test-product',
|
||||
'description' => 'Test description',
|
||||
'website_url' => 'https://example.com',
|
||||
'user_id' => $user->id,
|
||||
'category_id' => $this->category->id,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->delete(route('products.destroy', $product));
|
||||
|
||||
$response->assertRedirect();
|
||||
$this->assertDatabaseMissing('products', ['id' => $product->id]);
|
||||
});
|
||||
|
||||
test('moderators can approve products', function () {
|
||||
$moderator = User::factory()->create();
|
||||
$moderator->roles()->attach($this->moderatorRole);
|
||||
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$product = Product::create([
|
||||
'title' => 'Test Product',
|
||||
'slug' => 'test-product',
|
||||
'description' => 'Test description',
|
||||
'website_url' => 'https://example.com',
|
||||
'user_id' => $user->id,
|
||||
'category_id' => $this->category->id,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($moderator)->post(route('products.approve', $product));
|
||||
|
||||
$response->assertRedirect();
|
||||
$this->assertDatabaseHas('products', [
|
||||
'id' => $product->id,
|
||||
'status' => 'approved',
|
||||
]);
|
||||
|
||||
expect($product->fresh()->approved_at)->not->toBeNull();
|
||||
});
|
||||
|
||||
test('moderators can reject products', function () {
|
||||
$moderator = User::factory()->create();
|
||||
$moderator->roles()->attach($this->moderatorRole);
|
||||
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$product = Product::create([
|
||||
'title' => 'Test Product',
|
||||
'slug' => 'test-product',
|
||||
'description' => 'Test description',
|
||||
'website_url' => 'https://example.com',
|
||||
'user_id' => $user->id,
|
||||
'category_id' => $this->category->id,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($moderator)->post(route('products.reject', $product));
|
||||
|
||||
$response->assertRedirect();
|
||||
$this->assertDatabaseHas('products', [
|
||||
'id' => $product->id,
|
||||
'status' => 'rejected',
|
||||
]);
|
||||
|
||||
expect($product->fresh()->rejected_at)->not->toBeNull();
|
||||
});
|
||||
|
||||
test('regular users cannot approve products', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$product = Product::create([
|
||||
'title' => 'Test Product',
|
||||
'slug' => 'test-product',
|
||||
'description' => 'Test description',
|
||||
'website_url' => 'https://example.com',
|
||||
'user_id' => $user->id,
|
||||
'category_id' => $this->category->id,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->post(route('products.approve', $product));
|
||||
|
||||
$response->assertStatus(403);
|
||||
});
|
||||
|
||||
test('regular users cannot access product management page', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$response = $this->actingAs($user)->get(route('products.manage'));
|
||||
|
||||
$response->assertStatus(403);
|
||||
});
|
||||
|
||||
test('moderators can access product management page', function () {
|
||||
$moderator = User::factory()->create();
|
||||
$moderator->roles()->attach($this->moderatorRole);
|
||||
|
||||
$response = $this->actingAs($moderator)->get(route('products.manage'));
|
||||
|
||||
$response->assertStatus(200);
|
||||
});
|
||||
|
||||
test('admins can access product management page', function () {
|
||||
$admin = User::factory()->create();
|
||||
$admin->roles()->attach($this->adminRole);
|
||||
|
||||
$response = $this->actingAs($admin)->get(route('products.manage'));
|
||||
|
||||
$response->assertStatus(200);
|
||||
});
|
||||
|
||||
test('slug is auto-generated from title', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$product = Product::create([
|
||||
'title' => 'My Awesome Product',
|
||||
'description' => 'Test description',
|
||||
'website_url' => 'https://example.com',
|
||||
'user_id' => $user->id,
|
||||
'category_id' => $this->category->id,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
expect($product->slug)->toBe('my-awesome-product');
|
||||
});
|
||||
|
||||
test('duplicate slugs are handled correctly', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->roles()->attach($this->userRole);
|
||||
|
||||
$product1 = Product::create([
|
||||
'title' => 'Test Product',
|
||||
'description' => 'Test description',
|
||||
'website_url' => 'https://example.com',
|
||||
'user_id' => $user->id,
|
||||
'category_id' => $this->category->id,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
$product2 = Product::create([
|
||||
'title' => 'Test Product',
|
||||
'description' => 'Test description',
|
||||
'website_url' => 'https://example2.com',
|
||||
'user_id' => $user->id,
|
||||
'category_id' => $this->category->id,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
expect($product1->slug)->toBe('test-product');
|
||||
expect($product2->slug)->toBe('test-product-1');
|
||||
});
|
||||
Reference in New Issue
Block a user