Преглед на файлове

🐛 fix(city): corrigir resource com whenLoaded, fillable e cascadeOnDelete na migration

Fase: dev | Origin: bug-interno
Gustavo Zanatta преди 1 седмица
родител
ревизия
504785f4a9

+ 12 - 0
app/Http/Controllers/CityController.php

@@ -52,4 +52,16 @@ class CityController extends Controller
             code: 204,
         );
     }
+
+    public function allByStateId(int $id): JsonResponse
+    {
+        $items = $this->service->getAllByStateId($id);
+        return $this->successResponse(payload: CityResource::collection($items));
+    }
+
+    public function allByCountryId(int $id): JsonResponse
+    {
+        $items = $this->service->getAllByCountryId($id);
+        return $this->successResponse(payload: CityResource::collection($items));
+    }
 }

+ 4 - 2
app/Http/Resources/CityResource.php

@@ -7,6 +7,8 @@ use Illuminate\Http\Request;
 use Illuminate\Http\Resources\Json\JsonResource;
 use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
 use App\Models\City;
+use App\Http\Resources\StateResource;
+use App\Http\Resources\CountryResource;
 
 class CityResource extends JsonResource
 {
@@ -17,8 +19,8 @@ class CityResource extends JsonResource
             'name' => $this->name,
             'state_id' => $this->state_id,
             'country_id' => $this->country_id,
-            'state' => $this->state,
-            'country' => $this->country,
+            'state' => $this->whenLoaded('state', fn() => new StateResource($this->state)),
+            'country' => $this->whenLoaded('country', fn() => new CountryResource($this->country)),
             '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'),

+ 1 - 1
app/Models/City.php

@@ -41,7 +41,7 @@ class City extends Model
 
     protected $table = "cities";
 
-    protected $guarded = ["id"];
+    protected $fillable = ['name', 'country_id', 'state_id', 'status'];
 
     /**
      * Get the attributes that should be cast.

+ 15 - 1
app/Services/CityService.php

@@ -47,5 +47,19 @@ class CityService
         return $model->delete();
     }
 
-    // Add custom business logic methods here
+    public function getAllByStateId(int $stateId): Collection
+    {
+        return City::with(["state:id,name", "country:id,name"])
+            ->where('state_id', $stateId)
+            ->orderBy('name', 'asc')
+            ->get();
+    }
+
+    public function getAllByCountryId(int $countryId): Collection
+    {
+        return City::with(["state:id,name", "country:id,name"])
+            ->where('country_id', $countryId)
+            ->orderBy('name', 'asc')
+            ->get();
+    }
 }

+ 2 - 2
database/migrations/2024_12_18_184016_create_cities_table.php

@@ -11,8 +11,8 @@ return new class extends Migration
         Schema::create('cities', function (Blueprint $table) {
             $table->id();
             $table->string('name');
-            $table->foreignId('country_id')->constrained();
-            $table->foreignId('state_id')->constrained();
+            $table->foreignId('country_id')->constrained()->cascadeOnDelete();
+            $table->foreignId('state_id')->constrained()->cascadeOnDelete();
             $table->index('country_id');
             $table->index('state_id');
             $table->string('status')->default('ACTIVE');