Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#24 - news #39

Merged
merged 13 commits into from
Aug 19, 2024
2 changes: 1 addition & 1 deletion .github/workflows/test-and-lint-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/[email protected]
with:
php-version: 8.2
php-version: 8.3
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, pdo_pgsql, intl
coverage: none

Expand Down
22 changes: 19 additions & 3 deletions app/Http/Controllers/Public/NewsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,30 @@
namespace App\Http\Controllers\Public;

use App\Http\Controllers\Controller;
use App\Models\News;
use Inertia\Response;

class NewsController extends Controller
{
public function __invoke(): Response
public function index(): Response
{
return inertia("Public/News", [
"title" => "mgr inż.",
$news = News::query()
->orderBy("published_at", "desc")
->paginate(9);

return inertia("Public/News/Index", [
"paginator" => $news,
]);
}

public function get(string $slug): Response
{
$news = News::query()
->where("slug", $slug)
->sole();

return inertia("Public/News/News", [
"news" => $news,
]);
}
}
7 changes: 2 additions & 5 deletions app/Models/GradeColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Observers\GradeColumnObserver;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -23,6 +24,7 @@
* @property-read Group $group
* @property-read Collection<Grade> $grades
*/
#[ObservedBy(GradeColumnObserver::class)]
class GradeColumn extends Model
{
use HasFactory;
Expand All @@ -46,9 +48,4 @@ public function grades(): HasMany
{
return $this->hasMany(Grade::class);
}

protected static function booted(): void
{
self::observe(GradeColumnObserver::class);
}
}
3 changes: 3 additions & 0 deletions app/Models/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace App\Models;

use App\Observers\NewsObserver;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -16,6 +18,7 @@
* @property string $content
* @property Carbon $published_at
*/
#[ObservedBy(NewsObserver::class)]
class News extends Model
{
use HasFactory;
Expand Down
45 changes: 45 additions & 0 deletions app/Observers/NewsObserver.php
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;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"php": "^8.2",
"php": "^8.3",
"ext-pdo": "*",
"fakerphp/faker": "^1.23",
"guzzlehttp/guzzle": "^7.8",
Expand Down
Loading
Loading