This repository has been archived by the owner on Feb 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: automatic slug when creating or renaming a company (#1239)
- Loading branch information
Showing
11 changed files
with
192 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
app/Services/Company/Adminland/Company/UpdateCompanySlug.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace App\Services\Company\Adminland\Company; | ||
|
||
use Illuminate\Support\Str; | ||
use App\Services\BaseService; | ||
use App\Models\Company\Company; | ||
|
||
class UpdateCompanySlug extends BaseService | ||
{ | ||
private Company $company; | ||
|
||
/** | ||
* Get the validation rules that apply to the service. | ||
* | ||
* @return array | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'company_id' => 'required|integer|exists:companies,id', | ||
]; | ||
} | ||
|
||
/** | ||
* Updates the company slug. | ||
* | ||
* @param array $data | ||
* | ||
* @return Company | ||
*/ | ||
public function execute(array $data): Company | ||
{ | ||
$this->company = Company::find($data['company_id']); | ||
|
||
$slug = Str::slug($this->company->name, '-'); | ||
|
||
$isSlugUnique = false; | ||
while ($isSlugUnique === false) { | ||
$existingCompaniesWithThisSlug = Company::where('slug', '=', $slug) | ||
->where('id', '!=', $this->company->id) | ||
->count(); | ||
|
||
if ($existingCompaniesWithThisSlug > 0) { | ||
$slug = $slug.'-'.rand(1, 999); | ||
} | ||
|
||
$isSlugUnique = Company::where('slug', $slug) | ||
->where('id', '!=', $this->company->id) | ||
->count() == 0; | ||
} | ||
|
||
$this->company->slug = $slug; | ||
$this->company->save(); | ||
|
||
return $this->company->refresh(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
database/migrations/2021_08_02_184706_add_company_slug_to_companies.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Str; | ||
use App\Models\Company\Company; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class AddCompanySlugToCompanies extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up() | ||
{ | ||
// necessary for SQLlite | ||
Schema::enableForeignKeyConstraints(); | ||
|
||
Schema::table('companies', function (Blueprint $table) { | ||
$table->string('slug')->after('name')->nullable(); | ||
}); | ||
|
||
Company::chunk(200, function ($companies) { | ||
foreach ($companies as $company) { | ||
$company->slug = Str::slug($company->name, '-'); | ||
$company->save(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
tests/Unit/Services/Company/Adminland/Company/UpdateCompanySlugTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Services\Company\Adminland\Company; | ||
|
||
use Tests\TestCase; | ||
use App\Models\Company\Company; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
use App\Services\Company\Adminland\Company\UpdateCompanySlug; | ||
|
||
class UpdateCompanySlugTest extends TestCase | ||
{ | ||
use DatabaseTransactions; | ||
|
||
/** @test */ | ||
public function it_updates_a_slug(): void | ||
{ | ||
$company = Company::factory()->create([ | ||
'name' => 'Dunder Mifflin', | ||
]); | ||
|
||
$request = [ | ||
'company_id' => $company->id, | ||
]; | ||
|
||
$company = (new UpdateCompanySlug)->execute($request); | ||
|
||
$this->assertInstanceOf( | ||
Company::class, | ||
$company | ||
); | ||
|
||
$this->assertDatabaseHas('companies', [ | ||
'id' => $company->id, | ||
'slug' => 'dunder-mifflin', | ||
]); | ||
} | ||
|
||
/** @test */ | ||
public function it_updates_a_slug_that_is_unique(): void | ||
{ | ||
$company = Company::factory()->create([ | ||
'name' => 'Dunder Mifflin', | ||
]); | ||
Company::factory()->create([ | ||
'slug' => 'dunder-mifflin', | ||
]); | ||
|
||
$request = [ | ||
'company_id' => $company->id, | ||
]; | ||
|
||
$company = (new UpdateCompanySlug)->execute($request); | ||
|
||
$this->assertInstanceOf( | ||
Company::class, | ||
$company | ||
); | ||
|
||
$this->assertEquals( | ||
'dunder-mifflin-', | ||
substr($company->slug, 0, 15) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters