initial commit
This commit is contained in:
commit
ae93c7e1a6
233 changed files with 20282 additions and 0 deletions
1
backend/database/.gitignore
vendored
Normal file
1
backend/database/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
*.sqlite*
|
||||
30
backend/database/factories/BrandFactory.php
Normal file
30
backend/database/factories/BrandFactory.php
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Brand>
|
||||
*/
|
||||
class BrandFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$name = fake()->unique()->company();
|
||||
|
||||
return [
|
||||
'name' => $name,
|
||||
'slug' => Str::slug($name).'-'.fake()->unique()->numberBetween(10, 99),
|
||||
'description' => fake()->optional()->sentence(),
|
||||
'website_url' => fake()->optional()->url(),
|
||||
'active' => fake()->boolean(95),
|
||||
];
|
||||
}
|
||||
}
|
||||
39
backend/database/factories/CategoryFactory.php
Normal file
39
backend/database/factories/CategoryFactory.php
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\Category;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Category>
|
||||
*/
|
||||
class CategoryFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$name = fake()->category;
|
||||
|
||||
return [
|
||||
'parent_id' => null,
|
||||
'name' => Str::title($name),
|
||||
'slug' => Str::slug($name).'-'.fake()->unique()->numberBetween(1, 999),
|
||||
'description' => fake()->optional()->sentence(),
|
||||
'active' => fake()->boolean(90),
|
||||
'sort_order' => fake()->numberBetween(0, 50),
|
||||
];
|
||||
}
|
||||
|
||||
public function childOf(Category $category): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'parent_id' => $category->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
38
backend/database/factories/ProductFactory.php
Normal file
38
backend/database/factories/ProductFactory.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\Brand;
|
||||
use App\Models\Category;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Product>
|
||||
*/
|
||||
class ProductFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$name = Str::title(fake()->words(fake()->numberBetween(2, 5), true));
|
||||
|
||||
return [
|
||||
'category_id' => Category::factory(),
|
||||
'brand_id' => Brand::factory(),
|
||||
'name' => $name,
|
||||
'slug' => Str::slug($name).'-'.fake()->unique()->numberBetween(100, 999),
|
||||
'sku' => strtoupper(fake()->bothify('SKU-#####??')),
|
||||
'short_description' => fake()->optional()->sentence(),
|
||||
'description' => fake()->paragraphs(2, true),
|
||||
'price' => fake()->randomFloat(2, 10, 10000),
|
||||
'stock_quantity' => fake()->numberBetween(0, 5000),
|
||||
'active' => fake()->boolean(90),
|
||||
'published_at' => fake()->optional(0.8)->dateTimeBetween('-1 year', 'now'),
|
||||
];
|
||||
}
|
||||
}
|
||||
44
backend/database/factories/UserFactory.php
Normal file
44
backend/database/factories/UserFactory.php
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
|
||||
*/
|
||||
class UserFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* The current password being used by the factory.
|
||||
*/
|
||||
protected static ?string $password;
|
||||
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'name' => fake()->name(),
|
||||
'email' => fake()->unique()->safeEmail(),
|
||||
'email_verified_at' => now(),
|
||||
'password' => static::$password ??= Hash::make('password'),
|
||||
'remember_token' => Str::random(10),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the model's email address should be unverified.
|
||||
*/
|
||||
public function unverified(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'email_verified_at' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
108
backend/database/migrations/0001_01_01_000000_base.php
Normal file
108
backend/database/migrations/0001_01_01_000000_base.php
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
<?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('users', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->string('email')->unique();
|
||||
$table->timestamp('email_verified_at')->nullable();
|
||||
$table->string('password');
|
||||
$table->rememberToken();
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
Schema::create('password_reset_tokens', function (Blueprint $table) {
|
||||
$table->string('email')->primary();
|
||||
$table->string('token');
|
||||
$table->timestamp('created_at')->nullable();
|
||||
});
|
||||
|
||||
Schema::create('jobs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('queue')->index();
|
||||
$table->longText('payload');
|
||||
$table->unsignedTinyInteger('attempts');
|
||||
$table->unsignedInteger('reserved_at')->nullable();
|
||||
$table->unsignedInteger('available_at');
|
||||
$table->unsignedInteger('created_at');
|
||||
});
|
||||
|
||||
Schema::create('job_batches', function (Blueprint $table) {
|
||||
$table->string('id')->primary();
|
||||
$table->string('name');
|
||||
$table->integer('total_jobs');
|
||||
$table->integer('pending_jobs');
|
||||
$table->integer('failed_jobs');
|
||||
$table->longText('failed_job_ids');
|
||||
$table->mediumText('options')->nullable();
|
||||
$table->integer('cancelled_at')->nullable();
|
||||
$table->integer('created_at');
|
||||
$table->integer('finished_at')->nullable();
|
||||
});
|
||||
|
||||
Schema::create('failed_jobs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('uuid')->unique();
|
||||
$table->text('connection');
|
||||
$table->text('queue');
|
||||
$table->longText('payload');
|
||||
$table->longText('exception');
|
||||
$table->timestamp('failed_at')->useCurrent();
|
||||
});
|
||||
|
||||
Schema::create('categories', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('parent_id')->nullable()->constrained('categories')->nullOnDelete();
|
||||
$table->string('name');
|
||||
$table->string('slug')->unique();
|
||||
$table->text('description')->nullable();
|
||||
$table->boolean('active')->default(true);
|
||||
$table->unsignedInteger('sort_order')->default(0);
|
||||
$table->timestamps();
|
||||
|
||||
$table->index(['parent_id', 'active']);
|
||||
});
|
||||
|
||||
Schema::create('brands', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->string('slug')->unique();
|
||||
$table->text('description')->nullable();
|
||||
$table->string('website_url')->nullable();
|
||||
$table->boolean('active')->default(true);
|
||||
$table->timestamps();
|
||||
|
||||
$table->index('active');
|
||||
});
|
||||
|
||||
Schema::create('products', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('category_id')->constrained()->cascadeOnDelete();
|
||||
$table->foreignId('brand_id')->constrained()->cascadeOnDelete();
|
||||
$table->string('name');
|
||||
$table->string('slug')->unique();
|
||||
$table->string('sku')->unique();
|
||||
$table->text('short_description')->nullable();
|
||||
$table->longText('description')->nullable();
|
||||
$table->decimal('price', 12, 2);
|
||||
$table->unsignedInteger('stock_quantity')->default(0);
|
||||
$table->boolean('active')->default(true);
|
||||
$table->timestamp('published_at')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->index(['category_id', 'brand_id']);
|
||||
$table->index(['active', 'published_at']);
|
||||
$table->index('price');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
@ -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::create('personal_access_tokens', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->morphs('tokenable');
|
||||
$table->text('name');
|
||||
$table->string('token', 64)->unique();
|
||||
$table->text('abilities')->nullable();
|
||||
$table->timestamp('last_used_at')->nullable();
|
||||
$table->timestamp('expires_at')->nullable()->index();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('personal_access_tokens');
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
<?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('users', function (Blueprint $table) {
|
||||
$table->text('two_factor_secret')
|
||||
->after('password')
|
||||
->nullable();
|
||||
|
||||
$table->text('two_factor_recovery_codes')
|
||||
->after('two_factor_secret')
|
||||
->nullable();
|
||||
|
||||
$table->timestamp('two_factor_confirmed_at')
|
||||
->after('two_factor_recovery_codes')
|
||||
->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->dropColumn([
|
||||
'two_factor_secret',
|
||||
'two_factor_recovery_codes',
|
||||
'two_factor_confirmed_at',
|
||||
]);
|
||||
});
|
||||
}
|
||||
};
|
||||
81
backend/database/seeders/CategorySeeder.php
Normal file
81
backend/database/seeders/CategorySeeder.php
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
<?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
|
||||
{
|
||||
use WithoutModelEvents;
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$categories = [
|
||||
'Computers' => [
|
||||
'Laptop',
|
||||
'Stationary',
|
||||
'Server'
|
||||
],
|
||||
'Components' => [
|
||||
'Memory',
|
||||
'Harddrives' => [
|
||||
'SSD',
|
||||
'Mechanical',
|
||||
],
|
||||
'Motherboard',
|
||||
'Graphicscard',
|
||||
'Soundcard',
|
||||
'Tools'
|
||||
],
|
||||
'Accessories' => [
|
||||
'Keyboard',
|
||||
'Mouse',
|
||||
'Bags',
|
||||
'Streaming'
|
||||
],
|
||||
'Monitor' => [
|
||||
'Gaming monitors',
|
||||
'Mounting',
|
||||
'Accessories'
|
||||
],
|
||||
];
|
||||
|
||||
$this->seedCategoryTree($categories);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int|string, mixed> $nodes
|
||||
*/
|
||||
private function seedCategoryTree(array $nodes, ?int $parentId = null, string $pathPrefix = ''): void
|
||||
{
|
||||
$sortOrder = 0;
|
||||
|
||||
foreach ($nodes as $key => $value) {
|
||||
$name = is_int($key) ? (string) $value : (string) $key;
|
||||
$children = is_int($key) ? [] : (is_array($value) ? $value : []);
|
||||
|
||||
$path = trim($pathPrefix.' '.$name);
|
||||
$slug = Str::slug($path);
|
||||
|
||||
$category = Category::query()->updateOrCreate(
|
||||
['slug' => $slug],
|
||||
[
|
||||
'parent_id' => $parentId,
|
||||
'name' => $name,
|
||||
'description' => null,
|
||||
'active' => true,
|
||||
'sort_order' => $sortOrder,
|
||||
],
|
||||
);
|
||||
|
||||
if ($children !== []) {
|
||||
$this->seedCategoryTree($children, (int) $category->id, $path);
|
||||
}
|
||||
|
||||
$sortOrder++;
|
||||
}
|
||||
}
|
||||
}
|
||||
56
backend/database/seeders/DatabaseSeeder.php
Normal file
56
backend/database/seeders/DatabaseSeeder.php
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\Brand;
|
||||
use App\Models\Category;
|
||||
use App\Models\Product;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class DatabaseSeeder extends Seeder
|
||||
{
|
||||
use WithoutModelEvents;
|
||||
|
||||
/**
|
||||
* Seed the application's database.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
// User::factory(10)->create();
|
||||
|
||||
User::factory()->create([
|
||||
'name' => 'Test User',
|
||||
'email' => 'test@example.com',
|
||||
]);
|
||||
|
||||
$brands = Brand::factory(12)->create();
|
||||
|
||||
$this->call(CategorySeeder::class);
|
||||
|
||||
$allCategories = Category::query()->get();
|
||||
$parentCategories = $allCategories->whereNull('parent_id')->values();
|
||||
$childCategories = $allCategories->whereNotNull('parent_id')->values();
|
||||
|
||||
if ($allCategories->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Product::factory(100)
|
||||
->state(fn (): array => [
|
||||
'category_id' => $childCategories->isNotEmpty()
|
||||
? $childCategories->random()->id
|
||||
: $allCategories->random()->id,
|
||||
'brand_id' => $brands->random()->id,
|
||||
])
|
||||
->create();
|
||||
|
||||
Product::factory(30)
|
||||
->state(fn (): array => [
|
||||
'category_id' => $parentCategories->random()->id,
|
||||
'brand_id' => $brands->random()->id,
|
||||
])
|
||||
->create();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue