Просмотр исходного кода

feat(products): remove campos obrigatorios, adiciona request e resource produto

ebagabee 1 месяц назад
Родитель
Сommit
ee54318d3a

+ 7 - 7
app/Http/Requests/ProductRequest.php

@@ -9,15 +9,15 @@ class ProductRequest extends FormRequest
     public function rules(): array
     {
         $rules = [
-            // Add your validation rules here
-            //'field' => 'sometimes|string|max:255',
+            'name'        => 'sometimes|string|max:255',
+            'description' => 'sometimes|nullable|string|max:2000',
+            'price_sale'  => 'sometimes|numeric|min:0',
         ];
 
-        // Different rules for creation
-        //if ($this->isMethod('POST')) {
-            // Make fields required if needed
-            // $rules['field'] = 'required|string|max:255';
-        //}
+        if ($this->isMethod('POST')) {
+            $rules['name']       = 'required|string|max:255';
+            $rules['price_sale'] = 'required|numeric|min:0';
+        }
 
         return $rules;
     }

+ 6 - 13
app/Http/Resources/ProductResource.php

@@ -18,19 +18,12 @@ class ProductResource extends JsonResource
     public function toArray(Request $request): array
     {
         return [
-            'id' => $this->id,
-            'name' => $this->name,
-            'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
-            'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
-            // Add your fields here
-
-            // Conditional fields
-            // $this->mergeWhen($request->user()?->isAdmin(), [
-            //     'internal_notes' => $this->internal_notes,
-            // ]),
-
-            // Relationships
-            // 'user' => new UserResource($this->whenLoaded('user')),
+            'id'          => $this->id,
+            'name'        => $this->name,
+            'description' => $this->description,
+            'price_sale'  => $this->price_sale,
+            'created_at'  => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
+            'updated_at'  => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
         ];
     }
 

+ 2 - 1
app/Models/Product.php

@@ -4,6 +4,7 @@
 
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
 
 /**
  * @property int $id
@@ -43,7 +44,7 @@
  */
 class Product extends Model
 {
-    use HasFactory;
+    use HasFactory, SoftDeletes;
 
     protected $table = 'products';
 

+ 36 - 0
database/migrations/2026_05_14_000001_alter_products_table_nullable_and_description.php

@@ -0,0 +1,36 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    public function up(): void
+    {
+        Schema::table('products', function (Blueprint $table) {
+            $table->text('description')->nullable()->after('name');
+            $table->string('sku')->nullable()->change();
+            $table->string('barcode')->nullable()->change();
+            $table->string('measure_unit')->nullable()->change();
+            $table->decimal('price_cost', 10, 2)->nullable()->change();
+            $table->decimal('weight', 10, 3)->nullable()->change();
+            $table->decimal('length', 10, 3)->nullable()->change();
+            $table->decimal('height', 10, 3)->nullable()->change();
+        });
+    }
+
+    public function down(): void
+    {
+        Schema::table('products', function (Blueprint $table) {
+            $table->dropColumn('description');
+            $table->string('sku')->nullable(false)->change();
+            $table->string('barcode')->nullable(false)->change();
+            $table->string('measure_unit')->nullable(false)->change();
+            $table->decimal('price_cost', 10, 2)->nullable(false)->change();
+            $table->decimal('weight', 10, 3)->nullable(false)->change();
+            $table->decimal('length', 10, 3)->nullable(false)->change();
+            $table->decimal('height', 10, 3)->nullable(false)->change();
+        });
+    }
+};