-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7f1c62
commit 4e2de86
Showing
14 changed files
with
185 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Observers; | ||
|
||
use App\Models\News; | ||
use Illuminate\Support\Str; | ||
|
||
class NewsObserver | ||
{ | ||
public function creating(News $news): void | ||
{ | ||
if ($news->slug) { | ||
return; | ||
} | ||
|
||
$slug = Str::slug($news->published_at->format("Y-m-d") . " " . $news->title); | ||
|
||
if (!$this->checkIfSlugExists($slug)) { | ||
$news->slug = $slug; | ||
|
||
return; | ||
} | ||
|
||
$i = 2; | ||
|
||
while (true) { | ||
$newSlug = Str::slug($slug . " " . $i); | ||
|
||
if (!$this->checkIfSlugExists($newSlug)) { | ||
$news->slug = $newSlug; | ||
|
||
return; | ||
} | ||
|
||
$i++; | ||
} | ||
} | ||
|
||
protected function checkIfSlugExists(string $slug): bool | ||
{ | ||
return News::query()->where("slug", $slug)->count() > 0; | ||
} | ||
} |
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
26 changes: 0 additions & 26 deletions
26
database/migrations/2023_10_01_181311_create_news_table.php
This file was deleted.
Oops, something went wrong.
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Database\Seeders; | ||
|
||
use App\Models\News; | ||
use Illuminate\Database\Seeder; | ||
|
||
class DemoSeeder extends Seeder | ||
{ | ||
public function run(): void | ||
{ | ||
News::factory(20)->create(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<script setup> | ||
defineProps({ | ||
pagination: Object, | ||
}) | ||
</script> | ||
|
||
<template> | ||
<nav class="relative flex justify-center"> | ||
<template v-for="link in pagination.links" :key="link.label"> | ||
<InertiaLink | ||
preserve-scroll | ||
:href="link.url ?? ''" | ||
class="flex items-center justify-center rounded-lg px-3 py-2 text-sm text-gray-600" | ||
:class="{ 'bg-gray-200': link.active, '!text-gray-300': !link.url }" | ||
> | ||
{{ link.label }} | ||
</InertiaLink> | ||
</template> | ||
</nav> | ||
</template> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Models\News; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Tests\TestCase; | ||
|
||
class NewsSlugTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function testNewsSlugging(): void | ||
{ | ||
News::factory()->create([ | ||
"title" => "Test", | ||
"published_at" => "2024-06-01 15:00:00", | ||
]); | ||
|
||
$this->assertEquals("2024-06-01-test", News::query()->first()->slug); | ||
} | ||
|
||
public function testMultipleNewsSlugging(): void | ||
{ | ||
News::factory()->create([ | ||
"title" => "Test", | ||
"published_at" => "2024-06-01 15:00:00", | ||
]); | ||
|
||
News::factory()->create([ | ||
"title" => "Test", | ||
"published_at" => "2024-06-15 15:00:00", | ||
]); | ||
|
||
$news = News::query()->get(); | ||
|
||
$this->assertEquals("2024-06-01-test", $news[0]->slug); | ||
$this->assertEquals("2024-06-15-test", $news[1]->slug); | ||
} | ||
|
||
public function testMultipleNewsWithRepeatedTitleSlugging(): void | ||
{ | ||
News::factory()->create([ | ||
"title" => "Test", | ||
"published_at" => "2024-06-01 15:00:00", | ||
]); | ||
|
||
News::factory()->create([ | ||
"title" => "Test", | ||
"published_at" => "2024-06-01 15:00:00", | ||
]); | ||
|
||
News::factory()->create([ | ||
"title" => "Test", | ||
"published_at" => "2024-06-15 15:00:00", | ||
]); | ||
|
||
News::factory()->create([ | ||
"title" => "Test", | ||
"published_at" => "2024-06-01 15:00:00", | ||
]); | ||
|
||
$news = News::query()->get(); | ||
|
||
$this->assertEquals("2024-06-01-test", $news[0]->slug); | ||
$this->assertEquals("2024-06-01-test-2", $news[1]->slug); | ||
$this->assertEquals("2024-06-15-test", $news[2]->slug); | ||
$this->assertEquals("2024-06-01-test-3", $news[3]->slug); | ||
} | ||
} |