فهرست منبع

pagina de configuracoes da empresa para alterar dados da landingpage

Gustavo Zanatta 1 ماه پیش
والد
کامیت
d2751c3584

+ 97 - 0
app/Http/Controllers/CompanySettingController.php

@@ -0,0 +1,97 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Http\Requests\CompanyBenefitRequest;
+use App\Http\Requests\CompanyHeroImageRequest;
+use App\Http\Requests\CompanySettingRequest;
+use App\Http\Resources\CompanyBenefitResource;
+use App\Http\Resources\CompanySettingResource;
+use App\Services\CompanyBenefitService;
+use App\Services\CompanySettingService;
+use Illuminate\Http\JsonResponse;
+use Illuminate\Http\Request;
+
+class CompanySettingController extends Controller
+{
+    public function __construct(
+        protected CompanySettingService $settingService,
+        protected CompanyBenefitService $benefitService,
+    ) {}
+
+    public function show(): JsonResponse
+    {
+        $setting = $this->settingService->get();
+        return $this->successResponse(payload: new CompanySettingResource($setting));
+    }
+
+    public function update(CompanySettingRequest $request): JsonResponse
+    {
+        $setting = $this->settingService->upsert($request->validated());
+        return $this->successResponse(
+            payload: new CompanySettingResource($setting),
+            message: __('messages.updated'),
+        );
+    }
+
+    public function uploadHeroImage(CompanyHeroImageRequest $request): JsonResponse
+    {
+        $setting = $this->settingService->uploadHeroBackground($request->file('image'));
+        return $this->successResponse(
+            payload: new CompanySettingResource($setting),
+            message: __('messages.updated'),
+        );
+    }
+
+    public function removeHeroImage(): JsonResponse
+    {
+        $setting = $this->settingService->removeHeroBackground();
+        return $this->successResponse(
+            payload: new CompanySettingResource($setting),
+            message: __('messages.updated'),
+        );
+    }
+
+    public function indexBenefits(): JsonResponse
+    {
+        $benefits = $this->benefitService->getAll();
+        return $this->successResponse(payload: CompanyBenefitResource::collection($benefits));
+    }
+
+    public function storeBenefit(CompanyBenefitRequest $request): JsonResponse
+    {
+        $benefit = $this->benefitService->create($request->validated());
+        return $this->successResponse(
+            payload: new CompanyBenefitResource($benefit),
+            message: __('messages.created'),
+            code: 201,
+        );
+    }
+
+    public function updateBenefit(CompanyBenefitRequest $request, int $id): JsonResponse
+    {
+        $benefit = $this->benefitService->update($id, $request->validated());
+        return $this->successResponse(
+            payload: new CompanyBenefitResource($benefit),
+            message: __('messages.updated'),
+        );
+    }
+
+    public function reorderBenefits(Request $request): JsonResponse
+    {
+        $items = $request->validate([
+            '*.id'    => 'required|integer|exists:company_benefits,id',
+            '*.order' => 'required|integer|min:0',
+        ]);
+
+        $this->benefitService->reorder($items);
+
+        return $this->successResponse(message: __('messages.updated'));
+    }
+
+    public function destroyBenefit(int $id): JsonResponse
+    {
+        $this->benefitService->delete($id);
+        return $this->successResponse(message: __('messages.deleted'), code: 204);
+    }
+}

+ 25 - 0
app/Http/Requests/CompanyBenefitRequest.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace App\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class CompanyBenefitRequest extends FormRequest
+{
+    public function rules(): array
+    {
+        $rules = [
+            'title'       => 'sometimes|string|max:255',
+            'icon'        => 'sometimes|string|max:100',
+            'description' => 'sometimes|nullable|string|max:500',
+            'order'       => 'sometimes|integer|min:0',
+        ];
+
+        if ($this->isMethod('post')) {
+            $rules['title'] = 'required|string|max:255';
+            $rules['icon']  = 'required|string|max:100';
+        }
+
+        return $rules;
+    }
+}

+ 15 - 0
app/Http/Requests/CompanyHeroImageRequest.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace App\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class CompanyHeroImageRequest extends FormRequest
+{
+    public function rules(): array
+    {
+        return [
+            'image' => 'required|image|max:8192',
+        ];
+    }
+}

+ 25 - 0
app/Http/Requests/CompanySettingRequest.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace App\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class CompanySettingRequest extends FormRequest
+{
+    public function rules(): array
+    {
+        return [
+            'hero_title'        => 'sometimes|nullable|string|max:255',
+            'hero_subtitle'     => 'sometimes|nullable|string|max:1000',
+            'stat1_value'       => 'sometimes|nullable|string|max:50',
+            'stat1_label'       => 'sometimes|nullable|string|max:100',
+            'stat2_value'       => 'sometimes|nullable|string|max:50',
+            'stat2_label'       => 'sometimes|nullable|string|max:100',
+            'stat3_value'       => 'sometimes|nullable|string|max:50',
+            'stat3_label'       => 'sometimes|nullable|string|max:100',
+            'contact_email'     => 'sometimes|nullable|email|max:255',
+            'contact_phone'     => 'sometimes|nullable|string|max:50',
+            'contact_location'  => 'sometimes|nullable|string|max:255',
+        ];
+    }
+}

+ 23 - 0
app/Http/Resources/CompanyBenefitResource.php

@@ -0,0 +1,23 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Carbon\Carbon;
+use Illuminate\Http\Request;
+use Illuminate\Http\Resources\Json\JsonResource;
+
+class CompanyBenefitResource extends JsonResource
+{
+    public function toArray(Request $request): array
+    {
+        return [
+            'id'          => $this->id,
+            'title'       => $this->title,
+            'icon'        => $this->icon,
+            'description' => $this->description,
+            'order'       => $this->order,
+            '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'),
+        ];
+    }
+}

+ 36 - 0
app/Http/Resources/CompanySettingResource.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace App\Http\Resources;
+
+use Carbon\Carbon;
+use Illuminate\Http\Request;
+use Illuminate\Http\Resources\Json\JsonResource;
+use Illuminate\Support\Facades\Storage;
+
+class CompanySettingResource extends JsonResource
+{
+    public function toArray(Request $request): array
+    {
+        return [
+            'id'                     => $this->id,
+            'hero_background_path'   => $this->hero_background_path,
+            'hero_background_url'    => $this->hero_background_path
+                                            ? Storage::disk('s3')->temporaryUrl($this->hero_background_path, now()->addHours(24))
+                                            : null,
+            'hero_title'             => $this->hero_title,
+            'hero_subtitle'          => $this->hero_subtitle,
+            'stat1_value'            => $this->stat1_value,
+            'stat1_label'            => $this->stat1_label,
+            'stat2_value'            => $this->stat2_value,
+            'stat2_label'            => $this->stat2_label,
+            'stat3_value'            => $this->stat3_value,
+            'stat3_label'            => $this->stat3_label,
+            'contact_email'          => $this->contact_email,
+            'contact_phone'          => $this->contact_phone,
+            'contact_location'       => $this->contact_location,
+            'updated_at'             => $this->updated_at
+                                            ? Carbon::parse($this->updated_at)->format('Y-m-d H:i:s')
+                                            : null,
+        ];
+    }
+}

+ 13 - 0
app/Models/CompanyBenefit.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class CompanyBenefit extends Model
+{
+    use SoftDeletes;
+
+    protected $guarded = ['id'];
+}

+ 10 - 0
app/Models/CompanySetting.php

@@ -0,0 +1,10 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class CompanySetting extends Model
+{
+    protected $guarded = ['id'];
+}

+ 57 - 0
app/Services/CompanyBenefitService.php

@@ -0,0 +1,57 @@
+<?php
+
+namespace App\Services;
+
+use App\Models\CompanyBenefit;
+use Illuminate\Database\Eloquent\Collection;
+
+class CompanyBenefitService
+{
+    public function getAll(): Collection
+    {
+        return CompanyBenefit::orderBy('order')->orderBy('id')->get();
+    }
+
+    public function findById(int $id): ?CompanyBenefit
+    {
+        return CompanyBenefit::find($id);
+    }
+
+    public function create(array $data): CompanyBenefit
+    {
+        if (!isset($data['order'])) {
+            $data['order'] = CompanyBenefit::max('order') + 1;
+        }
+        return CompanyBenefit::create($data);
+    }
+
+    public function update(int $id, array $data): ?CompanyBenefit
+    {
+        $model = $this->findById($id);
+
+        if (!$model) {
+            return null;
+        }
+
+        $model->update($data);
+        return $model->fresh();
+    }
+
+    public function reorder(array $items): void
+    {
+        foreach ($items as $item) {
+            CompanyBenefit::where('id', $item['id'])->update(['order' => $item['order']]);
+        }
+    }
+
+    public function delete(int $id): bool
+    {
+        $model = $this->findById($id);
+
+        if (!$model) {
+            return false;
+        }
+
+        return $model->delete();
+    }
+}

+ 51 - 0
app/Services/CompanySettingService.php

@@ -0,0 +1,51 @@
+<?php
+
+namespace App\Services;
+
+use App\Models\CompanySetting;
+use Illuminate\Http\UploadedFile;
+use Illuminate\Support\Facades\Storage;
+
+class CompanySettingService
+{
+    public function get(): CompanySetting
+    {
+        return CompanySetting::firstOrNew(['id' => 1]);
+    }
+
+    public function upsert(array $data): CompanySetting
+    {
+        $setting = CompanySetting::firstOrNew(['id' => 1]);
+        $setting->fill($data);
+        $setting->save();
+        return $setting->fresh();
+    }
+
+    public function uploadHeroBackground(UploadedFile $file): CompanySetting
+    {
+        $setting = $this->get();
+
+        if ($setting->hero_background_path) {
+            Storage::disk('s3')->delete($setting->hero_background_path);
+        }
+
+        $path = $file->store('company/hero', 's3');
+        $setting->hero_background_path = $path;
+        $setting->save();
+
+        return $setting->fresh();
+    }
+
+    public function removeHeroBackground(): CompanySetting
+    {
+        $setting = $this->get();
+
+        if ($setting->hero_background_path) {
+            Storage::disk('s3')->delete($setting->hero_background_path);
+            $setting->hero_background_path = null;
+            $setting->save();
+        }
+
+        return $setting->fresh();
+    }
+}

+ 33 - 0
database/migrations/2026_06_19_000001_create_company_settings_table.php

@@ -0,0 +1,33 @@
+<?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('company_settings', function (Blueprint $table) {
+            $table->id();
+            $table->string('hero_background_path')->nullable();
+            $table->string('hero_title')->nullable();
+            $table->text('hero_subtitle')->nullable();
+            $table->string('stat1_value')->nullable();
+            $table->string('stat1_label')->nullable();
+            $table->string('stat2_value')->nullable();
+            $table->string('stat2_label')->nullable();
+            $table->string('stat3_value')->nullable();
+            $table->string('stat3_label')->nullable();
+            $table->string('contact_email')->nullable();
+            $table->string('contact_phone')->nullable();
+            $table->string('contact_location')->nullable();
+            $table->timestamps();
+        });
+    }
+
+    public function down(): void
+    {
+        Schema::dropIfExists('company_settings');
+    }
+};

+ 26 - 0
database/migrations/2026_06_19_000002_create_company_benefits_table.php

@@ -0,0 +1,26 @@
+<?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('company_benefits', function (Blueprint $table) {
+            $table->id();
+            $table->string('title');
+            $table->string('icon');
+            $table->string('description')->nullable();
+            $table->unsignedSmallInteger('order')->default(0);
+            $table->timestamps();
+            $table->softDeletes();
+        });
+    }
+
+    public function down(): void
+    {
+        Schema::dropIfExists('company_benefits');
+    }
+};

+ 6 - 0
database/seeders/PermissionSeeder.php

@@ -205,6 +205,12 @@ class PermissionSeeder extends Seeder
                 "bits" => Permission::MENU | Permission::VIEW,
                 "bits" => Permission::MENU | Permission::VIEW,
                 "children" => [],
                 "children" => [],
             ],
             ],
+            [
+                "scope" => "configuracoes-empresa",
+                "description" => "Configurações da Empresa",
+                "bits" => Permission::CRUD,
+                "children" => [],
+            ],
         ];
         ];
 
 
         $this->createPermissionsAndChildren(permissions: $permissions);
         $this->createPermissionsAndChildren(permissions: $permissions);

+ 29 - 0
routes/authRoutes/company_settings.php

@@ -0,0 +1,29 @@
+<?php
+
+use App\Http\Controllers\CompanySettingController;
+use Illuminate\Support\Facades\Route;
+
+Route::controller(CompanySettingController::class)->group(function () {
+    Route::put('/company-settings', 'update')
+        ->middleware('permission:configuracoes-empresa,edit');
+
+    Route::post('/company-settings/hero-image', 'uploadHeroImage')
+        ->middleware('permission:configuracoes-empresa,edit');
+
+    Route::delete('/company-settings/hero-image', 'removeHeroImage')
+        ->middleware('permission:configuracoes-empresa,edit');
+
+    Route::prefix('company-benefits')->group(function () {
+        Route::post('/', 'storeBenefit')
+            ->middleware('permission:configuracoes-empresa,add');
+
+        Route::put('/reorder', 'reorderBenefits')
+            ->middleware('permission:configuracoes-empresa,edit');
+
+        Route::put('/{id}', 'updateBenefit')
+            ->middleware('permission:configuracoes-empresa,edit');
+
+        Route::delete('/{id}', 'destroyBenefit')
+            ->middleware('permission:configuracoes-empresa,delete');
+    });
+});

+ 9 - 0
routes/noAuthRoutes/company_settings.php

@@ -0,0 +1,9 @@
+<?php
+
+use App\Http\Controllers\CompanySettingController;
+use Illuminate\Support\Facades\Route;
+
+Route::controller(CompanySettingController::class)->group(function () {
+    Route::get('/company-settings', 'show');
+    Route::get('/company-benefits', 'indexBenefits');
+});