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

refactor: optimize api load #18

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Actions/GetArticleListAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public function run(): PaginatedDataCollection
{
$articles = $this->articleRepository->getPaginatedList();

return ArticleData::collect($articles, PaginatedDataCollection::class)->except('content');
return ArticleData::collect($articles, PaginatedDataCollection::class);
}
}
2 changes: 1 addition & 1 deletion src/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,6 @@ public function image(): Attribute

protected static function newFactory(): ArticleFactory
{
return new ArticleFactory();
return new ArticleFactory;
}
}
18 changes: 17 additions & 1 deletion src/Repositories/ArticleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ public function getModelClass(): string
public function getPaginatedList(): LengthAwarePaginator
{
return QueryBuilder::for($this->getModelClass())
->with(['media', 'seo'])
->select([
'id',
'slug',
'title',
'short_content',
'published_at',
])
->with(['media'])
->allowedFilters([
'id',
'slug',
Expand All @@ -43,6 +50,15 @@ public function getPaginatedList(): LengthAwarePaginator
public function getBySlug(string $slug): Model
{
return $this->model()
->select([
'id',
'slug',
'title',
'content',
'short_content',
'published_at',
])
->with(['media', 'seo', 'seo.media'])
->where('slug', $slug)
->isPublished()
->firstOrFail();
Expand Down
26 changes: 10 additions & 16 deletions src/UI/API/Data/ArticleData.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ public function __construct(
public Lazy|string $image,
public Lazy|string $content,
public Lazy|string|null $short_content,
public Lazy|bool $pinned,
public Lazy|Carbon $published_at,
public Lazy|Carbon $created_at,
public Lazy|Carbon $updated_at,
public array $seo,
public Lazy|array $seo,
) {}

public static function fromModel(Article $article): self
Expand All @@ -34,21 +31,18 @@ public static function fromModel(Article $article): self
Lazy::when(fn () => isset($article->image), fn () => $article->image),
Lazy::when(fn () => isset($article->content), fn () => $article->content),
Lazy::when(fn () => isset($article->short_content), fn () => $article->short_content),
Lazy::when(fn () => isset($article->pinned), fn () => $article->pinned),
Lazy::when(fn () => isset($article->published_at), fn () => $article->published_at),
Lazy::when(fn () => isset($article->created_at), fn () => $article->created_at),
Lazy::when(fn () => isset($article->updated_at), fn () => $article->updated_at),
[
'title' => $article->seo?->title,
'description' => $article->seo?->description,
'keywords' => $article->seo?->keywords,
Lazy::when(fn () => $article->relationLoaded('seo'), fn () => [
'title' => $article->seo->title,
'description' => $article->seo->description,
'keywords' => $article->seo->keywords,
'og' => [
'url' => $article->seo?->og_url,
'title' => $article->seo?->og_title,
'description' => $article->seo?->og_description,
'image' => $article->seo?->getFirstMediaUrl(),
'url' => $article->seo->og_url,
'title' => $article->seo->og_title,
'description' => $article->seo->og_description,
'image' => $article->seo->getFirstMediaUrl(),
],
]
]),
);
}
}