Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
346 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
return [ | ||
'articles' => [ | ||
'enable_routes' => false, | ||
], | ||
]; |
34 changes: 34 additions & 0 deletions
34
src/Containers/ArticleSection/Article/Data/Factories/ArticleFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AdminKit\Core\Containers\ArticleSection\Article\Data\Factories; | ||
|
||
use AdminKit\Core\Containers\ArticleSection\Article\Models\Article; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class ArticleFactory extends Factory | ||
{ | ||
protected $model = Article::class; | ||
|
||
public function definition() | ||
{ | ||
$locales = config('translatable.locales'); | ||
$translations = collect($locales) | ||
->mapWithKeys(fn ($value) => [ | ||
$value => [ | ||
'title' => fake('ru')->realText(100), | ||
'content' => fake('ru')->randomHtml, | ||
'short_content' => fake('ru')->realText(500), | ||
], | ||
]) | ||
->toArray(); | ||
|
||
return [ | ||
'slug' => fake()->slug, | ||
'published_at' => fake()->boolean ? fake()->dateTimeThisMonth : null, | ||
'pinned' => fake()->boolean, | ||
...$translations, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/Containers/ArticleSection/Article/UI/API/DTO/ArticleDTO.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AdminKit\Core\Containers\ArticleSection\Article\UI\API\DTO; | ||
|
||
use AdminKit\Core\Containers\ArticleSection\Article\Models\Article; | ||
use Carbon\Carbon; | ||
use Spatie\LaravelData\Data; | ||
use Spatie\LaravelData\Lazy; | ||
|
||
class ArticleDTO extends Data | ||
{ | ||
public function __construct( | ||
public Lazy|int $id, | ||
public Lazy|string $slug, | ||
public Lazy|string $title, | ||
public Lazy|string $content, | ||
public Lazy|string|null $short_content, | ||
public Lazy|Carbon $published_at, | ||
public Lazy|Carbon $created_at, | ||
public Lazy|Carbon $updated_at, | ||
) { | ||
} | ||
|
||
public static function fromModel(Article $article): self | ||
{ | ||
return new self( | ||
Lazy::when(fn () => isset($article->id), fn () => $article->id), | ||
Lazy::when(fn () => isset($article->slug), fn () => $article->slug), | ||
Lazy::when(fn () => isset($article->title), fn () => $article->title), | ||
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->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), | ||
); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Containers/ArticleSection/Article/UI/API/Repositories/ArticleInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AdminKit\Core\Containers\ArticleSection\Article\UI\API\Repositories; | ||
|
||
use AdminKit\Core\Repositories\RepositoryInterface; | ||
use Spatie\LaravelData\Data; | ||
use Spatie\LaravelData\PaginatedDataCollection; | ||
|
||
interface ArticleInterface extends RepositoryInterface | ||
{ | ||
public function getPaginatedList(): PaginatedDataCollection; | ||
|
||
public function getById(int $id): Data; | ||
|
||
public function getBySlug(string $slug): Data; | ||
} |
95 changes: 95 additions & 0 deletions
95
src/Containers/ArticleSection/Article/UI/API/Repositories/ArticleRepository.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AdminKit\Core\Containers\ArticleSection\Article\UI\API\Repositories; | ||
|
||
use AdminKit\Core\Containers\ArticleSection\Article\Models\Article; | ||
use AdminKit\Core\Containers\ArticleSection\Article\UI\API\DTO\ArticleDTO; | ||
use AdminKit\Core\Repositories\AbstractRepository; | ||
use Exception; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
use Spatie\LaravelData\Data; | ||
use Spatie\LaravelData\PaginatedDataCollection; | ||
use Spatie\QueryBuilder\AllowedFilter; | ||
use Spatie\QueryBuilder\QueryBuilder; | ||
|
||
class ArticleRepository extends AbstractRepository implements ArticleInterface | ||
{ | ||
public function model(): string | ||
{ | ||
return Article::class; | ||
} | ||
|
||
public function getPaginatedList(): PaginatedDataCollection | ||
{ | ||
$perPage = (int) request()->query('per_page'); | ||
|
||
$articles = QueryBuilder::for($this->model()) | ||
->withTranslation() | ||
->allowedFilters([ | ||
'id', | ||
'slug', | ||
'published_at', | ||
'created_at', | ||
'updated_at', | ||
AllowedFilter::scope('title'), | ||
AllowedFilter::scope('content'), | ||
AllowedFilter::scope('short_content'), | ||
'translation.title', | ||
'translation.content', | ||
'translation.short_content', | ||
]) | ||
->allowedFields([ | ||
'id', | ||
'slug', | ||
'published_at', | ||
'created_at', | ||
'updated_at', | ||
'translation.title', | ||
//'translation.content', | ||
'translation.short_content', | ||
]) | ||
->allowedSorts(['id', 'published_at']) | ||
->isPublished() | ||
->isTitleNotNull() | ||
->paginate($perPage); | ||
|
||
return ArticleDTO::collection($articles)->except('content'); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function getById(int $id): Data | ||
{ | ||
$article = $this->model->findOrFail($id); | ||
|
||
$this->checkIsPublished($article); | ||
|
||
return ArticleDTO::from($article); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function getBySlug(string $slug): Data | ||
{ | ||
$article = $this->model->where('slug', $slug)->first(); | ||
|
||
if (is_null($article)) { | ||
throw (new ModelNotFoundException)->setModel($this->model(), $slug); | ||
} | ||
|
||
$this->checkIsPublished($article); | ||
|
||
return ArticleDTO::from($article); | ||
} | ||
|
||
private function checkIsPublished(Article $article) | ||
{ | ||
if (is_null($article->published_at)) { | ||
throw new Exception(__('Article has not been published')); | ||
} | ||
} | ||
} |
6 changes: 0 additions & 6 deletions
6
src/Containers/ArticleSection/Article/UI/API/Routes/GetArticle.v1.public.php
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
src/Containers/ArticleSection/Article/UI/API/Routes/GetArticles.v1.public.php
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
src/Containers/ArticleSection/Article/UI/API/Routes/api.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
use AdminKit\Core\Containers\ArticleSection\Article\UI\API\Controllers\ArticleController; | ||
use Illuminate\Support\Facades\Route; | ||
|
||
if (config('admin-kit.articles.enable_routes')) { | ||
Route::get('/articles', [ArticleController::class, 'index']); | ||
Route::get('/articles/{id}', [ArticleController::class, 'show'])->where('id', '[0-9]+'); | ||
Route::get('/articles/slug/{slug}', [ArticleController::class, 'showBySlug']); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.