Ver Fonte

feat: adiciona campos para usuario

ebagabee há 2 semanas atrás
pai
commit
e53444db23

+ 1 - 0
app/Http/Requests/StudentRequest.php

@@ -9,6 +9,7 @@ class StudentRequest extends FormRequest
     public function rules(): array
     {
         $rules = [
+            'avatar'              => 'sometimes|nullable|image|max:2048',
             'birth_date'          => 'sometimes|nullable|date',
             'document_number'     => 'sometimes|nullable|string|max:20',
             'gender'              => 'sometimes|nullable|string|in:no_preference,male,female,other',

+ 2 - 0
app/Http/Resources/StudentResource.php

@@ -6,6 +6,7 @@
 use Illuminate\Http\Request;
 use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
 use Illuminate\Http\Resources\Json\JsonResource;
+use Illuminate\Support\Facades\Storage;
 
 class StudentResource extends JsonResource
 {
@@ -30,6 +31,7 @@ public function toArray(Request $request): array
             'payer_name'          => $this->payer_name,
             'how_did_you_know_us' => $this->how_did_you_know_us,
             'notes'               => $this->notes,
+            'photo_url'           => $this->photo_url ? Storage::url($this->photo_url) : null,
             'status'              => $this->status,
             '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'),

+ 30 - 0
app/Services/StudentService.php

@@ -5,6 +5,8 @@
 use App\Models\Student;
 use App\Models\User;
 use Illuminate\Database\Eloquent\Collection;
+use Illuminate\Http\UploadedFile;
+use Illuminate\Support\Facades\Storage;
 
 class StudentService
 {
@@ -25,6 +27,7 @@ public function findById(int $id): ?Student
     public function create(User $user, array $data): Student
     {
         $unitId = $this->resolveUnitId($user);
+        $data = $this->handlePhoto($data);
 
         return Student::create(array_merge($data, ['unit_id' => $unitId]));
     }
@@ -37,6 +40,7 @@ public function update(int $id, array $data): ?Student
             return null;
         }
 
+        $data = $this->handlePhoto($data, $model->photo_url);
         $model->update($data);
         return $model->fresh();
     }
@@ -49,9 +53,35 @@ public function delete(int $id): bool
             return false;
         }
 
+        if ($model->photo_url) {
+            Storage::delete($model->photo_url);
+        }
+
         return $model->delete();
     }
 
+    private function handlePhoto(array $data, ?string $oldPhotoPath = null): array
+    {
+        if (!isset($data['avatar'])) {
+            return $data;
+        }
+
+        if ($data['avatar'] instanceof UploadedFile) {
+            if ($oldPhotoPath) {
+                Storage::delete($oldPhotoPath);
+            }
+            $data['photo_url'] = $data['avatar']->store('students/photos');
+        } elseif (is_null($data['avatar'])) {
+            if ($oldPhotoPath) {
+                Storage::delete($oldPhotoPath);
+            }
+            $data['photo_url'] = null;
+        }
+
+        unset($data['avatar']);
+        return $data;
+    }
+
     private function resolveUnitId(User $user): int
     {
         $unit = $user->units()->first();

+ 22 - 0
database/migrations/2026_04_16_000001_add_photo_url_to_students_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('students', function (Blueprint $table) {
+            $table->string('photo_url')->nullable()->after('notes');
+        });
+    }
+
+    public function down(): void
+    {
+        Schema::table('students', function (Blueprint $table) {
+            $table->dropColumn('photo_url');
+        });
+    }
+};