Преглед изворни кода

feat: adiciona save de unit

ebagabee пре 2 недеља
родитељ
комит
390a049e6c

+ 38 - 17
app/Http/Requests/UnitRequest.php

@@ -8,27 +8,48 @@ class UnitRequest extends FormRequest
 {
     public function rules(): array
     {
+        $isCreate = $this->isMethod('POST');
+
         $rules = [
-            // Add your validation rules here
-            //'field' => 'sometimes|string|max:255',
+            'fantasy_name'      => 'sometimes|required|string|max:255',
+            'social_reason'     => 'sometimes|required|string|max:255',
+            'cnpj'              => 'sometimes|required|string|max:20',
+            'state_registration' => 'sometimes|nullable|string|max:50',
+            'name_responsible'  => 'sometimes|required|string|max:255',
+            'street'            => 'sometimes|required|string|max:255',
+            'address_number'    => 'sometimes|nullable|string|max:20',
+            'postal_code'       => 'sometimes|required|string|max:9',
+            'neighborhood'      => 'sometimes|required|string|max:255',
+            'complement'        => 'sometimes|nullable|string|max:255',
+            'city_id'           => 'sometimes|required|integer|exists:cities,id',
+            'state_id'          => 'sometimes|required|integer|exists:states,id',
+            'email'             => 'sometimes|required|email|max:255',
+            'secondary_email'   => 'sometimes|nullable|email|max:255',
+            'phone_number'      => 'sometimes|required|string|max:20',
+            'cell_number'       => 'sometimes|nullable|string|max:20',
+            'avatar'            => 'sometimes|nullable|image|max:2048',
         ];
 
-        // Different rules for creation
-        //if ($this->isMethod('POST')) {
-            // Make fields required if needed
-            // $rules['field'] = 'required|string|max:255';
-        //}
+        if ($isCreate) {
+            $required = [
+                'fantasy_name',
+                'social_reason',
+                'cnpj',
+                'name_responsible',
+                'street',
+                'postal_code',
+                'neighborhood',
+                'city_id',
+                'state_id',
+                'email',
+                'phone_number',
+            ];
+
+            foreach ($required as $field) {
+                $rules[$field] = str_replace('sometimes|required', 'required', $rules[$field]);
+            }
+        }
 
         return $rules;
     }
-
-    /**
-    * Add custom messages when needed
-    * public function messages(): array
-    * {
-    *   return [
-    *        'field.required' => __('message.algo'),
-    *    ];
-    * }
-    */
 }

+ 32 - 11
app/Http/Resources/UnitResource.php

@@ -6,6 +6,7 @@
 use Illuminate\Http\Request;
 use Illuminate\Http\Resources\Json\JsonResource;
 use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
+use Illuminate\Support\Facades\Storage;
 use App\Models\Unit;
 
 class UnitResource extends JsonResource
@@ -18,19 +19,39 @@ class UnitResource 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
+            'id'                 => $this->id,
+            'fantasy_name'       => $this->fantasy_name,
+            'social_reason'      => $this->social_reason,
+            'cnpj'               => $this->cnpj,
+            'state_registration' => $this->state_registration,
+            'name_responsible'   => $this->name_responsible,
+            'street'             => $this->street,
+            'address_number'     => $this->address_number,
+            'postal_code'        => $this->postal_code,
+            'neighborhood'       => $this->neighborhood,
+            'complement'         => $this->complement,
+            'city_id'            => $this->city_id,
+            'state_id'           => $this->state_id,
+            'email'              => $this->email,
+            'secondary_email'    => $this->secondary_email,
+            'phone_number'       => $this->phone_number,
+            'cell_number'        => $this->cell_number,
+            'avatar_url'         => $this->avatar_url
+                ? Storage::disk('public')->url($this->avatar_url)
+                : null,
+            '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'),
 
-            // Conditional fields
-            // $this->mergeWhen($request->user()?->isAdmin(), [
-            //     'internal_notes' => $this->internal_notes,
-            // ]),
+            'city'  => $this->whenLoaded('city', fn() => [
+                'id'   => $this->city->id,
+                'name' => $this->city->name,
+            ]),
 
-            // Relationships
-            // 'user' => new UserResource($this->whenLoaded('user')),
+            'state' => $this->whenLoaded('state', fn() => [
+                'id'   => $this->state->id,
+                'name' => $this->state->name,
+                'code' => $this->state->code,
+            ]),
         ];
     }
 

+ 33 - 12
app/Models/Unit.php

@@ -4,34 +4,55 @@
 
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use Illuminate\Database\Eloquent\SoftDeletes;
 
 /**
  * @property int $id
+ * @property string $fantasy_name
+ * @property string $social_reason
+ * @property string $cnpj
+ * @property string|null $state_registration
+ * @property string $name_responsible
+ * @property string $street
+ * @property string|null $address_number
+ * @property string $postal_code
+ * @property string $neighborhood
+ * @property string|null $complement
+ * @property int $city_id
+ * @property int $state_id
+ * @property string $email
+ * @property string|null $secondary_email
+ * @property string $phone_number
+ * @property string|null $cell_number
+ * @property string|null $avatar_url
  * @property \Carbon\Carbon $created_at
  * @property \Carbon\Carbon $updated_at
+ * @property \Carbon\Carbon|null $deleted_at
+ * @property-read \App\Models\City $city
+ * @property-read \App\Models\State $state
  */
 class Unit extends Model
 {
-    use HasFactory;
+    use HasFactory, SoftDeletes;
 
     protected $table = 'units';
 
-    protected $guarded = [
-        'id', // Add more fields that shouldn't be edited here
-    ];
+    protected $guarded = ['id'];
 
     protected $casts = [
         'created_at' => 'datetime',
         'updated_at' => 'datetime',
-        // Add your casts here (e.g., 'is_active' => 'boolean')
+        'deleted_at' => 'datetime',
     ];
 
-    // Relationships
-
-    // Business Logic Methods
-
-    // Custom Finders
-
-    // Query Scopes
+    public function city(): BelongsTo
+    {
+        return $this->belongsTo(City::class, 'city_id');
+    }
 
+    public function state(): BelongsTo
+    {
+        return $this->belongsTo(State::class, 'state_id');
+    }
 }

+ 33 - 4
app/Services/UnitService.php

@@ -4,22 +4,26 @@
 
 use App\Models\Unit;
 use Illuminate\Database\Eloquent\Collection;
+use Illuminate\Http\UploadedFile;
+use Illuminate\Support\Facades\Storage;
 
 class UnitService
 {
     public function getAll(): Collection
     {
-        return Unit::orderBy('created_at', 'desc')
+        return Unit::with(['city', 'state'])
+            ->orderBy('fantasy_name')
             ->get();
     }
 
     public function findById(int $id): ?Unit
     {
-        return Unit::find($id);
+        return Unit::with(['city', 'state'])->find($id);
     }
 
     public function create(array $data): Unit
     {
+        $data = $this->handleAvatar($data);
         return Unit::create($data);
     }
 
@@ -31,8 +35,9 @@ public function update(int $id, array $data): ?Unit
             return null;
         }
 
+        $data = $this->handleAvatar($data, $model->avatar_url);
         $model->update($data);
-        return $model->fresh();
+        return $model->fresh(['city', 'state']);
     }
 
     public function delete(int $id): bool
@@ -43,8 +48,32 @@ public function delete(int $id): bool
             return false;
         }
 
+        if ($model->avatar_url) {
+            Storage::disk('public')->delete($model->avatar_url);
+        }
+
         return $model->delete();
     }
 
-    // Add custom business logic methods here
+    private function handleAvatar(array $data, ?string $oldAvatarPath = null): array
+    {
+        if (!isset($data['avatar'])) {
+            return $data;
+        }
+
+        if ($data['avatar'] instanceof UploadedFile) {
+            if ($oldAvatarPath) {
+                Storage::disk('public')->delete($oldAvatarPath);
+            }
+            $data['avatar_url'] = $data['avatar']->store('units/avatars', 'public');
+        } elseif (is_null($data['avatar'])) {
+            if ($oldAvatarPath) {
+                Storage::disk('public')->delete($oldAvatarPath);
+            }
+            $data['avatar_url'] = null;
+        }
+
+        unset($data['avatar']);
+        return $data;
+    }
 }

+ 22 - 0
database/migrations/2026_04_13_000001_add_complement_to_units_table.php

@@ -0,0 +1,22 @@
+<?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('units', function (Blueprint $table) {
+            $table->string('complement')->nullable()->after('neighborhood');
+        });
+    }
+
+    public function down(): void
+    {
+        Schema::table('units', function (Blueprint $table) {
+            $table->dropColumn('complement');
+        });
+    }
+};