Skip to content

Commit bb0514a

Browse files
feat:[LAR-85] add articles Approuved in sitemap
1 parent 7efd57d commit bb0514a

File tree

6 files changed

+45
-10
lines changed

6 files changed

+45
-10
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Actions\Article;
6+
7+
use App\Gamify\Points\ArticlePublished;
8+
use App\Models\Article;
9+
10+
final class ApprovedArticleAction
11+
{
12+
public function execute(Article $article): Article
13+
{
14+
$article->approved_at = now();
15+
$article->save();
16+
17+
givePoint(new ArticlePublished($article));
18+
19+
return $article;
20+
}
21+
}

app/Console/Commands/GenerateSitemap.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
namespace App\Console\Commands;
66

7+
use App\Models\Article;
78
use Illuminate\Console\Command;
89
use Illuminate\Support\Str;
910
use Psr\Http\Message\UriInterface;
11+
use Spatie\Sitemap\Sitemap;
1012
use Spatie\Sitemap\SitemapGenerator;
1113
use Spatie\Sitemap\Tags\Url;
1214

@@ -37,6 +39,10 @@ public function handle(): void
3739
return $url;
3840
})
3941
->writeToFile(public_path('sitemap.xml'));
42+
43+
Sitemap::create()
44+
->add(Article::query()->whereNotNull('approved_at')->get())
45+
->writeToFile(public_path('sitemap.xml'));
4046
}
4147

4248
private function shouldNotIndex(string $path): bool

app/Filament/Resources/ArticleResource.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace App\Filament\Resources;
66

7+
use App\Actions\Article\ApprovedArticleAction;
78
use App\Filament\Resources\ArticleResource\Pages;
8-
use App\Gamify\Points\ArticlePublished;
99
use App\Models\Article;
1010
use Filament\Resources\Resource;
1111
use Filament\Support\Enums\MaxWidth;
@@ -89,10 +89,7 @@ public static function table(Table $table): Table
8989
->action(function ($record): void {
9090
Gate::authorize('approve', $record);
9191

92-
$record->approved_at = now();
93-
$record->save();
94-
95-
givePoint(new ArticlePublished($record));
92+
app(ApprovedArticleAction::class)->execute($record);
9693
}),
9794
Action::make('declined')
9895
->visible(fn (Article $record) => $record->isAwaitingApproval())

app/Models/Article.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
use App\Traits\HasTags;
1313
use App\Traits\Reactable;
1414
use App\Traits\RecordsActivity;
15+
use Carbon\Carbon;
1516
use CyrildeWit\EloquentViewable\Contracts\Viewable;
1617
use CyrildeWit\EloquentViewable\InteractsWithViews;
1718
use Illuminate\Database\Eloquent\Factories\HasFactory;
1819
use Illuminate\Database\Eloquent\Model;
1920
use Illuminate\Support\Str;
2021
use Spatie\MediaLibrary\HasMedia;
2122
use Spatie\MediaLibrary\InteractsWithMedia;
23+
use Spatie\Sitemap\Contracts\Sitemapable;
24+
use Spatie\Sitemap\Tags\Url;
2225

2326
/**
2427
* @property-read int $id
@@ -43,7 +46,7 @@
4346
* @property \Carbon\Carbon $updated_at
4447
* @property \Illuminate\Database\Eloquent\Collection | Tag[] $tags
4548
*/
46-
final class Article extends Model implements HasMedia, ReactableInterface, Viewable
49+
final class Article extends Model implements HasMedia, ReactableInterface, Sitemapable, Viewable
4750
{
4851
use HasAuthor;
4952
use HasFactory;
@@ -237,4 +240,12 @@ public function delete(): ?bool
237240

238241
return parent::delete();
239242
}
243+
244+
public function toSitemapTag(): Url|string|array
245+
{
246+
return Url::create(route('articles.show', $this))
247+
->setLastModificationDate(Carbon::create($this->updated_at))
248+
->setChangeFrequency(Url::CHANGE_FREQUENCY_YEARLY)
249+
->setPriority(0.1);
250+
}
240251
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"spatie/laravel-feed": "^4.2.1",
4343
"spatie/laravel-google-fonts": "^1.2.3",
4444
"spatie/laravel-permission": "^6.10.0",
45-
"spatie/laravel-sitemap": "^7.2.1",
45+
"spatie/laravel-sitemap": "^7.3",
4646
"stevebauman/location": "^7.4.0",
4747
"symfony/http-client": "^7.1.8",
4848
"symfony/mailgun-mailer": "^7.1",

composer.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)