Skip to content

Commit

Permalink
Merge pull request #15 from IBEC-BOX/fix/repository
Browse files Browse the repository at this point in the history
fix: correcting repository pattern
  • Loading branch information
ast21 authored Jul 23, 2024
2 parents 32fbd95 + 13e5ac4 commit ba5add0
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 22 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"cviebrock/eloquent-sluggable": "^10.0",
"filament/filament": "^3.0-stable",
"illuminate/contracts": "^10.0|^11.0",
"ibecsystems/admin-kit-core": "^3.1",
"ibecsystems/admin-kit-core": "^3.5",
"ibecsystems/admin-kit-seo": "^3.4",
"spatie/laravel-package-tools": "^1.14.0",
"spatie/laravel-data": "^4.5",
Expand Down
4 changes: 2 additions & 2 deletions src/Actions/GetArticleBySlugAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace AdminKit\Articles\Actions;

use AdminKit\Articles\Repositories\ArticleRepository;
use AdminKit\Articles\UI\API\DTO\ArticleDTO;
use AdminKit\Articles\UI\API\Data\ArticleData;
use Spatie\LaravelData\Data;

class GetArticleBySlugAction
Expand All @@ -18,6 +18,6 @@ public function run(string $slug): Data
{
$article = $this->articleRepository->getBySlug($slug);

return ArticleDTO::from($article);
return ArticleData::from($article);
}
}
4 changes: 2 additions & 2 deletions src/Actions/GetArticleListAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace AdminKit\Articles\Actions;

use AdminKit\Articles\Repositories\ArticleRepository;
use AdminKit\Articles\UI\API\DTO\ArticleDTO;
use AdminKit\Articles\UI\API\Data\ArticleData;
use Spatie\LaravelData\PaginatedDataCollection;

class GetArticleListAction
Expand All @@ -18,6 +18,6 @@ public function run(): PaginatedDataCollection
{
$articles = $this->articleRepository->getPaginatedList();

return ArticleDTO::collection($articles)->except('content');
return ArticleData::collect($articles, PaginatedDataCollection::class)->except('content');
}
}
3 changes: 1 addition & 2 deletions src/Repositories/ArticleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

namespace AdminKit\Articles\Repositories;

use AdminKit\Core\Abstracts\Repositories\RepositoryInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;

interface ArticleInterface extends RepositoryInterface
interface ArticleInterface
{
public function getPaginatedList(): LengthAwarePaginator;

Expand Down
7 changes: 4 additions & 3 deletions src/Repositories/ArticleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@

class ArticleRepository extends AbstractRepository implements ArticleInterface
{
public function model(): string
public function getModelClass(): string
{
return Article::class;
}

public function getPaginatedList(): LengthAwarePaginator
{
return QueryBuilder::for($this->model())
return QueryBuilder::for($this->getModelClass())
->with(['media', 'seo'])
->allowedFilters([
'id',
'slug',
Expand All @@ -41,7 +42,7 @@ public function getPaginatedList(): LengthAwarePaginator

public function getBySlug(string $slug): Model
{
return $this->model
return $this->model()
->where('slug', $slug)
->isPublished()
->firstOrFail();
Expand Down
21 changes: 9 additions & 12 deletions src/UI/API/DTO/ArticleDTO.php → src/UI/API/Data/ArticleData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

declare(strict_types=1);

namespace AdminKit\Articles\UI\API\DTO;
namespace AdminKit\Articles\UI\API\Data;

use AdminKit\Articles\Models\Article;
use Carbon\Carbon;
use Spatie\LaravelData\Concerns\WithDeprecatedCollectionMethod;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Lazy;

class ArticleDTO extends Data
class ArticleData extends Data
{
use WithDeprecatedCollectionMethod;

public function __construct(
public Lazy|int $id,
public Lazy|string $slug,
Expand Down Expand Up @@ -42,14 +39,14 @@ public static function fromModel(Article $article): self
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,
'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(),
],
]
);
Expand Down

0 comments on commit ba5add0

Please sign in to comment.