Skip to content

Commit

Permalink
#24 - news (#39)
Browse files Browse the repository at this point in the history
* #24 - news draft

* #24 - news pagination

* #24 - composer update

* #24 - gha php bump

* #24 - code review changes

* #24 - lintf

* #24 - lintf

* #24 - sanitizeHtml

* #24 - csf

* #24 - conflicts
  • Loading branch information
krzysztofrewak authored Aug 19, 2024
1 parent 818c257 commit 2f713a2
Show file tree
Hide file tree
Showing 22 changed files with 528 additions and 185 deletions.
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/setup-php@c541c155eee45413f5b09a52248675b1a2575231 # 2.31.1
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.1",
"guzzlehttp/guzzle": "^7.9.2",
Expand Down
42 changes: 21 additions & 21 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"debug" => (bool)env("APP_DEBUG", false),
"url" => env("APP_URL", "http://localhost"),
"asset_url" => env("ASSET_URL"),
"timezone" => env("APP_TIMEZONE", "UTC"),
"timezone" => env("APP_TIMEZONE", "Europe/Warsaw"),
"locale" => "pl",
"fallback_locale" => "en",
"faker_locale" => "pl",
Expand Down
16 changes: 11 additions & 5 deletions database/factories/NewsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@

namespace Database\Factories;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class NewsFactory extends Factory
{
protected const array NEWS_TITLES = [
"Odwołane zajęcia",
"Przeniesione zajęcia",
"Nieobecność",
"Początek roku akademickiego",
"Koniec roku akademickiego",
"Terminy egzaminów",
];

public function definition(): array
{
$title = fake()->unique()->name();
$title = fake()->randomElement(static::NEWS_TITLES);

return [
"title" => $title,
"slug" => Str::slug($title),
"content" => fake()->realText(),
"published_at" => Carbon::now(),
"published_at" => fake()->dateTimeThisYear(),
];
}
}
16 changes: 16 additions & 0 deletions database/seeders/DemoSeeder.php
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();
}
}
1 change: 1 addition & 0 deletions lang/pl/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
"university_name" => "nazwa uczelni",
"department_name" => "nazwa wydziału",
"title" => "tytuł",
"content" => "treść",
"value" => "opis",
],
];
Loading

0 comments on commit 2f713a2

Please sign in to comment.