Skip to content

Commit

Permalink
Merge pull request #18 from IBEC-BOX/refactor/optimize-api-load
Browse files Browse the repository at this point in the history
refactor: optimize api load
  • Loading branch information
ast21 authored Jul 24, 2024
2 parents f1309b5 + b668a28 commit b85cf44
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
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);
}
}
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(),
],
]
]),
);
}
}

0 comments on commit b85cf44

Please sign in to comment.