Skip to content

Commit

Permalink
Merge pull request #8 from SethSharp/general-seeding-and-some-product…
Browse files Browse the repository at this point in the history
…-related

General seeding and some product related
  • Loading branch information
SethSharp authored Dec 6, 2023
2 parents 9fab537 + 81f2dff commit 4c75f89
Show file tree
Hide file tree
Showing 24 changed files with 399 additions and 22 deletions.
3 changes: 3 additions & 0 deletions app/Domain/Files/Models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use App\Domain\Products\Models\Product;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class File extends Model
{
use HasFactory;

protected $fillable = ['path'];

public function products(): BelongsToMany
Expand Down
18 changes: 17 additions & 1 deletion app/Domain/Products/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,26 @@
namespace App\Domain\Products\Models;

use App\Domain\Files\Models\File;
use Illuminate\Database\Eloquent\Model;
use App\Domain\Categories\Models\Category;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Product extends \Illuminate\Database\Eloquent\Model
class Product extends Model
{
use HasFactory;

public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
}

public function file(): BelongsTo
{
return $this->belongsTo(File::class);
}

public function files(): BelongsToMany
{
return $this->belongsToMany(File::class, 'file_products')->withTimestamps();
Expand Down
17 changes: 17 additions & 0 deletions app/Http/Controllers/Products/IndexProductsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Http\Controllers\Products;

use Inertia\Inertia;
use App\Http\Controllers\Controller;
use App\Domain\Products\Models\Product;

class IndexProductsController extends Controller
{
public function __invoke()
{
return Inertia::render('Products/Index', [
'products' => Product::with(['file', 'category'])->get()
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Database\Factories\Domain\Categories\Models;

use App\Domain\Files\Models\File;
use App\Domain\Categories\Models\Category;
use Illuminate\Database\Eloquent\Factories\Factory;

Expand All @@ -12,6 +13,7 @@ class CategoryFactory extends Factory
public function definition(): array
{
return [
'file_id' => File::factory()->create(),
'name' => fake()->name(),
'description' => fake()->words(6, true),
];
Expand Down
27 changes: 27 additions & 0 deletions database/factories/Domain/Files/Models/FileFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Database\Factories\Domain\Files\Models;

use App\Domain\Files\Models\File;
use Illuminate\Database\Eloquent\Factories\Factory;

class FileFactory extends Factory
{
protected $model = File::class;

public function definition(): array
{
return [
'path' => $this->image(),
];
}

protected function image(): string
{
$images = [
'https://tailwindui.com/img/ecommerce-images/product-page-03-related-product-01.jpg'
];

return $images[array_rand($images)];
}
}
24 changes: 24 additions & 0 deletions database/factories/Domain/Products/Models/ProductFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Database\Factories\Domain\Products\Models;

use App\Domain\Files\Models\File;
use App\Domain\Products\Models\Product;
use App\Domain\Categories\Models\Category;
use Illuminate\Database\Eloquent\Factories\Factory;

class ProductFactory extends Factory
{
protected $model = Product::class;

public function definition(): array
{
return [
'file_id' => File::factory()->create(),
'category_id' => Category::factory()->create(),
'title' => $this->faker->title,
'description' => $this->faker->words(12, true),
'price' => $this->faker->numberBetween(10, 100)
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->foreignId('file_id');
$table->foreignId('category_id');
$table->string('title');
$table->text('description');
$table->integer('price');
$table->timestamps();
});
}
Expand Down
19 changes: 19 additions & 0 deletions database/migrations/2023_12_03_014914_create_features_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

return new class() extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('features', function (Blueprint $table) {
$table->id();
$table->string('description');
$table->timestamps();
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

return new class() extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('product_feature', function (Blueprint $table) {
$table->id();
$table->foreignId('feature_id');
$table->foreignId('product_id');
$table->timestamps();
});
}
};
27 changes: 27 additions & 0 deletions database/seeders/CategorySeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Domain\Files\Models\File;
use App\Domain\Categories\Models\Category;

class CategorySeeder extends Seeder
{
public function run(): void
{
$numOfCategories = 4;

$categories = Category::factory()->count($numOfCategories)->create([
'file_id' => null,
]);

foreach ($categories as $index => $category) {
$category->update([
'file_id' => File::factory()->create([
'path' => '/images/seeding/categories/' . $index + 1 . '.webp'
])->id
]);
}
}
}
2 changes: 2 additions & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public function run(): void
Artisan::call('bootstrap');

$this->call(UserTableSeeder::class);
$this->call(CategorySeeder::class);
$this->call(ProductTableSeeder::class);
}
}
26 changes: 26 additions & 0 deletions database/seeders/ProductTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Domain\Files\Models\File;
use App\Domain\Products\Models\Product;

class ProductTableSeeder extends Seeder
{
public function run(): void
{
$productCount = 4;
// loop this over how many files there are and adjust the factory to pass to file factory
$products = Product::factory()->count($productCount)->create();

foreach ($products as $index => $product) {
$product->update([
'file_id' => File::factory()->create([
'path' => '/images/seeding/categories/' . $index + 1 . '.webp'
])->id,
'category_id' => $index + 1
]);
}
}
}
Binary file added public/images/seeding/categories/1.webp
Binary file not shown.
Binary file added public/images/seeding/categories/2.webp
Binary file not shown.
Binary file added public/images/seeding/categories/3.webp
Binary file not shown.
Binary file added public/images/seeding/categories/4.webp
Binary file not shown.
2 changes: 1 addition & 1 deletion resources/js/Components/Card/CategoryCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const toggleShow = () => {
</script>

<template>
<div class="relative overflow-hidden rounded-xl shadow-sm">
<div class="relative overflow-hidden rounded-xl shadow-sm h-52">
<div>
<div class="self-stretch flex-grow overflow-hidden">
<div class="aspect-1">
Expand Down
58 changes: 58 additions & 0 deletions resources/js/Components/Card/Products/ProductAdminCard.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<script setup>
import { Link } from '@inertiajs/vue3'
import CreateEditProductForm from '@/Components/Products/CreateEditProductForm.vue'
import CreateEditProductModal from '@/Components/Modals/Products/CreateEditProductModal.vue'
import { ref } from 'vue'
defineProps({
product: Object,
})
const createEditState = ref(false)
const showState = ref(false)
const toggleCreateEdit = () => {
createEditState.value = !createEditState.value
}
const toggleShow = () => {
showState.value = !showState.value
}
</script>

<template>
<div>
<div class="relative">
<div class="relative h-44 w-full overflow-hidden rounded-lg">
<img
:src="product.file.path"
alt="Product Image"
class="h-full w-full object-cover object-center"
/>
</div>
<div class="relative mt-4">
<h3 class="text-lg font-medium text-gray-900">{{ product.title }}</h3>
<h3 class="text-sm font-medium text-gray-600">{{ product.category.name }}</h3>
</div>
<div
class="absolute inset-x-0 top-0 flex h-44 items-end justify-end overflow-hidden rounded-lg p-4"
>
<div
aria-hidden="true"
class="absolute inset-x-0 bottom-0 h-36 bg-gradient-to-t from-black opacity-50"
/>
<p class="relative text-lg font-semibold text-white">${{ product.price }}</p>
</div>
</div>
<div class="mt-6">
<div
@click="toggleCreateEdit"
class="relative flex items-center justify-center rounded-md border border-transparent bg-gray-100 px-8 py-2 text-sm font-medium text-gray-900 hover:bg-gray-200"
>
Edit
</div>
</div>
</div>

<CreateEditProductModal :state="createEditState" @close="toggleCreateEdit" :product="product" />
</template>
29 changes: 29 additions & 0 deletions resources/js/Components/Modals/Products/CreateEditProductModal.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<script setup>
import CreateEditCategoryForm from '@/Components/Categories/CreateEditCategoryForm.vue'
import BaseModal from '@/Components/Modals/BaseModal.vue'
import CreateEditProductForm from '@/Components/Products/CreateEditProductForm.vue'
const props = defineProps({
state: Boolean,
product: Object,
})
const emits = defineEmits(['close'])
const title = props.product ? 'Edit ' + props.product.name : 'Create'
const closeModal = () => {
emits('close')
}
</script>

<template>
<BaseModal :state="state" @close="closeModal">
<template #title>
{{ title }}
</template>
<template #content>
<CreateEditProductForm :product="product" @close="closeModal" />
</template>
</BaseModal>
</template>
Loading

0 comments on commit 4c75f89

Please sign in to comment.