diff --git a/app/Livewire/Product/Create.php b/app/Livewire/Product/Create.php index 1638439..3eb6962 100644 --- a/app/Livewire/Product/Create.php +++ b/app/Livewire/Product/Create.php @@ -34,7 +34,7 @@ public function create() $product = Product::create([ 'name' => $this->name, 'description' => $this->description, - 'price' => $this->price, + 'price' => $this->price * 100, 'image' => $image, ]); diff --git a/app/Livewire/Product/Show.php b/app/Livewire/Product/Show.php index 637def8..b29b4eb 100644 --- a/app/Livewire/Product/Show.php +++ b/app/Livewire/Product/Show.php @@ -3,12 +3,65 @@ namespace App\Livewire\Product; use App\Models\Product; +use Illuminate\Support\Number; use Illuminate\View\View; +use Livewire\Attributes\Validate; use Livewire\Component; +use Livewire\WithFileUploads; class Show extends Component { public Product $product; + use WithFileUploads; + + #[Validate('required|string|max:255')] + public string $name = ''; + + #[Validate('required')] + public string $description = ''; + + #[Validate('required')] + public string $price = ''; + + #[Validate('nullable|image|max:1024')] + public $image = ''; + + public function mount() + { + $this->name = $this->product->name; + $this->description = $this->product->description; + $this->price = $this->product->price / 100; + } + + public function update() + { + $this->validate(); + + try { + $image = $this->image ? $this->image->store('products', 'public') : $this->product->image; + + $this->product->update([ + 'name' => $this->name, + 'description' => $this->description, + 'price' => $this->price * 100, + 'image' => $image, + ]); + + session()->flash('flash.type', 'success'); + session()->flash('flash.title', $this->name.' Updated'); + session()->flash('flash.message', 'Product has been successfully updated.'); + + return redirect()->route('products.show', $this->product); + } catch (\Exception $e) { + $this->addError('name', $e->getMessage()); + + session()->flash('flash.type', 'error'); + session()->flash('flash.title', 'Product Update Failed'); + session()->flash('flash.message', 'There was an error updating the product.'); + + return redirect()->back(); + } + } public function render(): View { diff --git a/app/Models/Product.php b/app/Models/Product.php index 53ad6fe..af29eb8 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -2,6 +2,7 @@ namespace App\Models; +use Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -24,7 +25,7 @@ class Product extends Model ]; protected $casts = [ - 'price' => 'float', + 'price' => 'integer', ]; public function user(): BelongsTo @@ -45,8 +46,8 @@ protected static function boot() }); } - public function getPriceAttribute(): string + public function getFormattedPriceAttribute(): string { - return Number::currency($this->attributes['price']); + return number_format($this->price / 100, 2); } } diff --git a/database/migrations/2024_04_24_013252_create_products_table.php b/database/migrations/2024_04_24_013252_create_products_table.php index fda5149..8c8358a 100644 --- a/database/migrations/2024_04_24_013252_create_products_table.php +++ b/database/migrations/2024_04_24_013252_create_products_table.php @@ -16,7 +16,7 @@ public function up(): void $table->string('name'); $table->string('slug'); $table->string('description'); - $table->string('price'); + $table->integer('price'); $table->string('image')->nullable(); $table->softDeletes(); $table->timestamps(); diff --git a/resources/views/livewire/product/create.blade.php b/resources/views/livewire/product/create.blade.php index 6da26e5..8ac8383 100644 --- a/resources/views/livewire/product/create.blade.php +++ b/resources/views/livewire/product/create.blade.php @@ -18,7 +18,12 @@
- +
+
+ $ +
+ +
@@ -48,3 +53,7 @@
+ +@assets + +@endassets diff --git a/resources/views/livewire/product/index-item.blade.php b/resources/views/livewire/product/index-item.blade.php index 121b927..84edf7a 100644 --- a/resources/views/livewire/product/index-item.blade.php +++ b/resources/views/livewire/product/index-item.blade.php @@ -1,6 +1,6 @@ {{ $product->name }} - {{ $product->price }} + ${{ $product->formattedPrice }} @if($product->image) diff --git a/resources/views/livewire/product/show.blade.php b/resources/views/livewire/product/show.blade.php index 411f998..109ad65 100644 --- a/resources/views/livewire/product/show.blade.php +++ b/resources/views/livewire/product/show.blade.php @@ -5,4 +5,53 @@ +
+
+
+
+ + + +
+
+ +{{-- --}} +
+
+ $ +
+ +
+ +
+
+ + + +
+
+
+ @if($image) + Product Image + @else + + @endif + +
+ +
+ Update +
+
+
+ + +@assets + +@endassets diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php deleted file mode 100644 index 8b5843f..0000000 --- a/tests/Feature/ExampleTest.php +++ /dev/null @@ -1,7 +0,0 @@ -get('/'); - - $response->assertStatus(200); -}); diff --git a/tests/Feature/Product/EditTest.php b/tests/Feature/Product/EditTest.php new file mode 100644 index 0000000..42842cc --- /dev/null +++ b/tests/Feature/Product/EditTest.php @@ -0,0 +1,24 @@ +create(); + + $response = $this->get(route('products.show', $product)); + + $response->assertRedirect(route('login')); +}); + +test('auth', function () { +$product = Product::factory()->create(); + + $this->actingAs($product->user); + + $response = $this->get(route('products.show', $product)); + + $response->assertOk()->assertSee($product->name); + + $response->assertSeeLivewire('product.show'); +}); diff --git a/tests/Unit/Livewire/ProductCreateTest.php b/tests/Unit/Livewire/Product/CreateTest.php similarity index 100% rename from tests/Unit/Livewire/ProductCreateTest.php rename to tests/Unit/Livewire/Product/CreateTest.php diff --git a/tests/Unit/ExampleTest.php b/tests/Unit/Livewire/Product/EditTest.php similarity index 50% rename from tests/Unit/ExampleTest.php rename to tests/Unit/Livewire/Product/EditTest.php index 44a4f33..fb64feb 100644 --- a/tests/Unit/ExampleTest.php +++ b/tests/Unit/Livewire/Product/EditTest.php @@ -1,5 +1,5 @@ toBeTrue(); });