瀏覽代碼

feat: :sparkles: crud tipos servicos dentro de providers

crud tipos servicos dentro de providers
Gustavo Zanatta 1 月之前
父節點
當前提交
7f6cd021d6

+ 47 - 0
app/Http/Controllers/ProviderServicesTypeController.php

@@ -0,0 +1,47 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Http\Requests\ProviderServicesTypeRequest;
+use App\Http\Resources\ProviderServicesTypeResource;
+use App\Services\ProviderServicesTypeService;
+use Illuminate\Http\JsonResponse;
+
+class ProviderServicesTypeController extends Controller
+{
+    protected ProviderServicesTypeService $providerServicesTypeService;
+
+    public function __construct(ProviderServicesTypeService $providerServicesTypeService)
+    {
+        $this->providerServicesTypeService = $providerServicesTypeService;
+    }
+
+    public function index(int $providerId): JsonResponse
+    {
+        $providerServicesTypes = $this->providerServicesTypeService->getByProvider($providerId);
+
+        return $this->successResponse(
+            payload: ProviderServicesTypeResource::collection($providerServicesTypes)
+        );
+    }
+
+    public function store(ProviderServicesTypeRequest $request, int $providerId): JsonResponse
+    {
+        $data = $request->validated();
+        $data['provider_id'] = $providerId;
+
+        $providerServicesType = $this->providerServicesTypeService->create($data);
+
+        return $this->successResponse(
+            payload: new ProviderServicesTypeResource($providerServicesType->load('serviceType')),
+            code: 201
+        );
+    }
+
+    public function destroy(int $providerId, int $id): JsonResponse
+    {
+        $this->providerServicesTypeService->delete($id);
+
+        return $this->successResponse();
+    }
+}

+ 22 - 0
app/Http/Requests/ProviderServicesTypeRequest.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace App\Http\Requests;
+
+use Illuminate\Contracts\Validation\ValidationRule;
+use Illuminate\Foundation\Http\FormRequest;
+
+class ProviderServicesTypeRequest extends FormRequest
+{
+
+    /**
+     * Get the validation rules that apply to the request.
+     *
+     * @return array<string, ValidationRule|array|string>
+     */
+    public function rules(): array
+    {
+        return [
+            'service_type_id' => ['required', 'integer', 'exists:service_types,id'],
+        ];
+    }
+}

+ 26 - 0
app/Http/Resources/ProviderServicesTypeResource.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Illuminate\Http\Request;
+use Illuminate\Http\Resources\Json\JsonResource;
+
+class ProviderServicesTypeResource extends JsonResource
+{
+    /**
+     * Transform the resource into an array.
+     *
+     * @return array<string, mixed>
+     */
+    public function toArray(Request $request): array
+    {
+        return [
+            'id' => $this->id,
+            'provider_id' => $this->provider_id,
+            'service_type_id' => $this->service_type_id,
+            'service_type' => new ServiceTypeResource($this->whenLoaded('serviceType')),
+            'created_at' => $this->created_at,
+            'updated_at' => $this->updated_at,
+        ];
+    }
+}

+ 33 - 0
app/Models/ProviderServicesType.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class ProviderServicesType extends Model
+{
+    use HasFactory, SoftDeletes;
+
+    protected $fillable = [
+        'provider_id',
+        'service_type_id',
+    ];
+
+    protected $casts = [
+        'provider_id' => 'integer',
+        'service_type_id' => 'integer',
+    ];
+
+    public function provider(): BelongsTo
+    {
+        return $this->belongsTo(Provider::class);
+    }
+
+    public function serviceType(): BelongsTo
+    {
+        return $this->belongsTo(ServiceType::class);
+    }
+}

+ 28 - 0
app/Services/ProviderServicesTypeService.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace App\Services;
+
+use App\Models\ProviderServicesType;
+use Illuminate\Database\Eloquent\Collection;
+
+class ProviderServicesTypeService
+{
+    public function getByProvider(int $providerId): Collection
+    {
+        return ProviderServicesType::with('serviceType')
+            ->where('provider_id', $providerId)
+            ->orderBy('created_at', 'desc')
+            ->get();
+    }
+
+    public function create(array $data): ProviderServicesType
+    {
+        return ProviderServicesType::create($data);
+    }
+
+    public function delete(int $id): bool
+    {
+        $providerServicesType = ProviderServicesType::findOrFail($id);
+        return $providerServicesType->delete();
+    }
+}

+ 30 - 0
database/migrations/2026_02_09_115732_create_provider_services_types_table.php

@@ -0,0 +1,30 @@
+<?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::create('provider_services_types', function (Blueprint $table) {
+            $table->id();
+            $table->foreignId('provider_id')->constrained('providers')->onDelete('cascade');
+            $table->foreignId('service_type_id')->constrained('service_types')->onDelete('cascade');
+            $table->timestamps();
+            $table->softDeletes();
+
+            $table->index('provider_id');
+            $table->index('service_type_id');
+        });
+
+        DB::statement('CREATE UNIQUE INDEX provider_service_type_unique ON provider_services_types(provider_id, service_type_id) WHERE deleted_at IS NULL');
+    }
+
+    public function down(): void
+    {
+        DB::statement('DROP INDEX IF EXISTS provider_service_type_unique');
+        Schema::dropIfExists('provider_services_types');
+    }
+};

+ 6 - 0
database/seeders/PermissionSeeder.php

@@ -88,6 +88,12 @@ class PermissionSeeder extends Seeder
                         "bits" => 271,
                         "children" => [],
                     ],
+                    [
+                        "scope" => "config.provider_services_types",
+                        "description" => "Configurações de Tipos de Serviços do Prestador",
+                        "bits" => 271,
+                        "children" => [],
+                    ],
                     [
                         "scope" => "config.improvement_type",
                         "description" => "Configurações de Tipos de Melhoria",

+ 1 - 0
database/seeders/UserTypePermissionSeeder.php

@@ -37,6 +37,7 @@ class UserTypePermissionSeeder extends Seeder
                         ['scope' => 'config.country', 'bits' => 1],
                         ['scope' => 'config.state', 'bits' => 1],
                         ['scope' => 'config.provider', 'bits' => 271],
+                        ['scope' => 'config.provider_services_types', 'bits' => 271],
                         ['scope' => 'config.improvement_type', 'bits' => 271],
                         ['scope' => 'config.media', 'bits' => 271],
                         ['scope' => 'config.service_type', 'bits' => 271],

+ 10 - 0
routes/authRoutes/provider_services_type.php

@@ -0,0 +1,10 @@
+<?php
+
+use Illuminate\Support\Facades\Route;
+use App\Http\Controllers\ProviderServicesTypeController;
+
+Route::get('/provider/service-types/{providerId}', [ProviderServicesTypeController::class, 'index'])->middleware('permission:config.provider_services_types,view');
+
+Route::post('/provider/service-types/{providerId}', [ProviderServicesTypeController::class, 'store'])->middleware('permission:config.provider_services_types,add');
+
+Route::delete('/provider/service-types/{providerId}/{id}', [ProviderServicesTypeController::class, 'destroy'])->middleware('permission:config.provider_services_types,delete');