taktik - laravel
This commit is contained in:
32
app/Services/CacheKeyBuilder.php
Normal file
32
app/Services/CacheKeyBuilder.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
class CacheKeyBuilder
|
||||
{
|
||||
/**
|
||||
* @param mixed ...$args
|
||||
* @return string
|
||||
*/
|
||||
public function buildCacheKeyFromArgs(...$args): string
|
||||
{
|
||||
$cacheName = '';
|
||||
foreach ($args as $key => $arg) {
|
||||
if (is_array($arg)) {
|
||||
$cacheName .= http_build_query($arg, '', '|');
|
||||
} elseif ($arg === null) {
|
||||
$cacheName .= '|N';
|
||||
} elseif (is_scalar($arg)) {
|
||||
$cacheName .= '|' . (string) $arg;
|
||||
} elseif ($arg instanceof CacheKeyInterface) {
|
||||
$cacheName .= $arg->toCacheKey();
|
||||
} else {
|
||||
throw new \InvalidArgumentException("Invalid argument $key type for key generator.");
|
||||
}
|
||||
}
|
||||
|
||||
return $cacheName;
|
||||
}
|
||||
}
|
||||
10
app/Services/CacheKeyInterface.php
Normal file
10
app/Services/CacheKeyInterface.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
interface CacheKeyInterface
|
||||
{
|
||||
public function toCacheKey(): string;
|
||||
}
|
||||
70
app/Services/Category/CategoryService.php
Normal file
70
app/Services/Category/CategoryService.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Category;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Services\PaginableResource;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryFilter;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryFilterDTO;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryOrder;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryOrderDTO;
|
||||
|
||||
class CategoryService implements CategoryServiceInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected readonly CategoryFilter $categoryFilter,
|
||||
protected readonly CategoryOrder $categoryOrder,
|
||||
protected readonly int $paginate = 10,
|
||||
) {
|
||||
}
|
||||
|
||||
public function fetchCategories(int $page, ?CategoryFilterDTO $filters, ?CategoryOrderDTO $orderDef): PaginableResource
|
||||
{
|
||||
$categories = $this->categoryFilter->apply(
|
||||
$this->categoryOrder->apply(Category::query(), $orderDef),
|
||||
$filters
|
||||
)->paginate($this->paginate);
|
||||
return PaginableResource::createFromLengthAwarePaginator($categories);
|
||||
}
|
||||
|
||||
public function findCategory(int $id): ?Category
|
||||
{
|
||||
return Category::find($id);
|
||||
}
|
||||
|
||||
public function storeCategory(array $data): Category
|
||||
{
|
||||
$category = Category::create($data);
|
||||
return Category::findOrFail($category->id);
|
||||
}
|
||||
|
||||
public function updateCategory(array $data, int $id): ?Category
|
||||
{
|
||||
$category = Category::find($id);
|
||||
|
||||
if ($category === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$category->update($data);
|
||||
return Category::findOrFail($id);
|
||||
}
|
||||
|
||||
public function deleteCategory(int $id): bool
|
||||
{
|
||||
$category = Category::find($id);
|
||||
|
||||
if ($category === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($category->posts()->count() > 0) {
|
||||
// TODO: return code
|
||||
return false;
|
||||
}
|
||||
|
||||
return $category->delete();
|
||||
}
|
||||
}
|
||||
33
app/Services/Category/CategoryServiceInterface.php
Normal file
33
app/Services/Category/CategoryServiceInterface.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Category;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Services\PaginableResource;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryFilterDTO;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryOrderDTO;
|
||||
|
||||
interface CategoryServiceInterface
|
||||
{
|
||||
/**
|
||||
* @return PaginableResource<Category>
|
||||
*/
|
||||
public function fetchCategories(int $page, ?CategoryFilterDTO $filters, ?CategoryOrderDTO $orderDef): PaginableResource;
|
||||
|
||||
public function findCategory(int $id): ?Category;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @return Category
|
||||
*/
|
||||
public function storeCategory(array $data): Category;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function updateCategory(array $data, int $id): ?Category;
|
||||
|
||||
public function deleteCategory(int $id): bool;
|
||||
}
|
||||
30
app/Services/Comment/CommentServiceInterface.php
Normal file
30
app/Services/Comment/CommentServiceInterface.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Comment;
|
||||
|
||||
use App\Models\Comment;
|
||||
use App\Services\PaginableResource;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentFilterDTO;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentOrderDTO;
|
||||
|
||||
interface CommentServiceInterface
|
||||
{
|
||||
/**
|
||||
* @return PaginableResource<Comment>
|
||||
*/
|
||||
public function fetchComments(int $remoteId, int $page, ?CommentFilterDTO $filters, ?CommentOrderDTO $orderDef): PaginableResource;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function storeComment(array $data, int $postId): ?Comment;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function updateComment(array $data, int $remoteId, int $id): ?Comment;
|
||||
|
||||
public function deleteComment(int $remoteId, int $id): bool;
|
||||
}
|
||||
82
app/Services/Comment/PostCommentService.php
Normal file
82
app/Services/Comment/PostCommentService.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Comment;
|
||||
|
||||
use App\Models\Comment;
|
||||
use App\Models\Post;
|
||||
use App\Services\PaginableResource;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentFilter;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentFilterDTO;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentOrder;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentOrderDTO;
|
||||
|
||||
class PostCommentService implements CommentServiceInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected readonly CommentFilter $commentFilter,
|
||||
protected readonly CommentOrder $commentOrder,
|
||||
protected readonly int $paginate = 10,
|
||||
) {
|
||||
}
|
||||
|
||||
public function fetchComments(int $remoteId, int $page, ?CommentFilterDTO $filters, ?CommentOrderDTO $orderDef): PaginableResource
|
||||
{
|
||||
$post = Post::findOrFail($remoteId);
|
||||
|
||||
$comments = $this->commentOrder->apply(
|
||||
$this->commentFilter->apply($post->comments()->getQuery(), $filters),
|
||||
$orderDef
|
||||
)->paginate($this->paginate);
|
||||
|
||||
return PaginableResource::createFromLengthAwarePaginator($comments);
|
||||
}
|
||||
|
||||
public function storeComment(array $data, int $postId): ?Comment
|
||||
{
|
||||
$post = Post::findOrFail($postId);
|
||||
|
||||
$comment = $post->comments()->create([
|
||||
'content' => $data['content'],
|
||||
]);
|
||||
|
||||
return Comment::findOrFail($comment->id);
|
||||
}
|
||||
|
||||
public function deleteComment(int $remoteId, int $id): bool
|
||||
{
|
||||
$post = Post::find($remoteId);
|
||||
|
||||
if ($post === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$comment = $post->comments()->find($id);
|
||||
|
||||
if ($comment === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $comment->delete();
|
||||
}
|
||||
|
||||
public function updateComment(array $data, int $remoteId, int $id): ?Comment
|
||||
{
|
||||
$post = Post::find($remoteId);
|
||||
|
||||
if ($post === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$comment = $post->comments()->find($id);
|
||||
|
||||
if ($comment === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$comment->update($data);
|
||||
|
||||
return Comment::findOrFail($comment->id);
|
||||
}
|
||||
}
|
||||
30
app/Services/PaginableResource.php
Normal file
30
app/Services/PaginableResource.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
*/
|
||||
class PaginableResource
|
||||
{
|
||||
/**
|
||||
* @param array<int, T> $data
|
||||
* @param int $totalCount
|
||||
*/
|
||||
public function __construct(public array $data, public int $totalCount, public int $totalPages)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param LengthAwarePaginator<int, T> $paginator
|
||||
* @return PaginableResource<T>
|
||||
*/
|
||||
public static function createFromLengthAwarePaginator(LengthAwarePaginator $paginator): PaginableResource
|
||||
{
|
||||
return new PaginableResource($paginator->items(), $paginator->total(), (int)ceil($paginator->total() / $paginator->perPage()));
|
||||
}
|
||||
}
|
||||
75
app/Services/Post/CachedPostService.php
Normal file
75
app/Services/Post/CachedPostService.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Post;
|
||||
|
||||
use App\Models\Post;
|
||||
use App\Services\CacheKeyBuilder;
|
||||
use App\Services\PaginableResource;
|
||||
use App\Services\QueryRequestModifiers\Post\PostFilter;
|
||||
use App\Services\QueryRequestModifiers\Post\PostFilterDTO;
|
||||
use App\Services\QueryRequestModifiers\Post\PostOrder;
|
||||
use App\Services\QueryRequestModifiers\Post\PostOrderDTO;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class CachedPostService extends PostService
|
||||
{
|
||||
public function __construct(
|
||||
PostFilter $postFilter,
|
||||
PostOrder $postOrder,
|
||||
protected readonly CacheKeyBuilder $cacheKeyBuilder,
|
||||
protected readonly int $cacheTtl = 60,
|
||||
) {
|
||||
parent::__construct($postFilter, $postOrder);
|
||||
}
|
||||
|
||||
public function fetchPosts(int $page, ?PostFilterDTO $filters, ?PostOrderDTO $orderDef): PaginableResource
|
||||
{
|
||||
return Cache::tags(['posts', 'fetch-posts', 'post-page-' . $page])
|
||||
->remember(
|
||||
$this->cacheKeyBuilder->buildCacheKeyFromArgs('post-page', $page, $filters, $orderDef),
|
||||
$this->cacheTtl,
|
||||
function () use ($page, $filters, $orderDef) {
|
||||
return parent::fetchPosts($page, $filters, $orderDef);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function findPost(int $id): ?Post
|
||||
{
|
||||
return Cache::tags(['posts', 'find-post-' . $id])
|
||||
->remember('post-' . $id, $this->cacheTtl, function () use ($id) {
|
||||
return parent::findPost($id);
|
||||
});
|
||||
}
|
||||
|
||||
public function storePost(array $data): Post
|
||||
{
|
||||
$newPost = parent::storePost($data);
|
||||
|
||||
Cache::tags('fetch-posts')->flush();
|
||||
|
||||
return $newPost;
|
||||
}
|
||||
|
||||
public function updatePost(array $data, int $id): ?Post
|
||||
{
|
||||
$updatedPost = parent::updatePost($data, $id);
|
||||
|
||||
if ($updatedPost !== null) {
|
||||
Cache::tags('fetch-posts')->flush();
|
||||
Cache::tags('find-post-' . $id)->flush();
|
||||
}
|
||||
|
||||
return $updatedPost;
|
||||
}
|
||||
|
||||
public function deletePost(int $id): bool
|
||||
{
|
||||
Cache::tags('fetch-posts')->flush();
|
||||
Cache::tags('find-post-' . $id)->flush();
|
||||
|
||||
return parent::deletePost($id); // TODO: Change the autogenerated stub
|
||||
}
|
||||
}
|
||||
106
app/Services/Post/PostService.php
Normal file
106
app/Services/Post/PostService.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Post;
|
||||
|
||||
use App\Models\Post;
|
||||
use App\Models\Tag;
|
||||
use App\Services\PaginableResource;
|
||||
use App\Services\QueryRequestModifiers\Post\PostFilter;
|
||||
use App\Services\QueryRequestModifiers\Post\PostFilterDTO;
|
||||
use App\Services\QueryRequestModifiers\Post\PostOrder;
|
||||
use App\Services\QueryRequestModifiers\Post\PostOrderDTO;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class PostService implements PostServiceInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected readonly PostFilter $postFilter,
|
||||
protected readonly PostOrder $postOrder,
|
||||
protected readonly int $paginate = 10,
|
||||
) {
|
||||
}
|
||||
|
||||
public function fetchPosts(int $page, ?PostFilterDTO $filters, ?PostOrderDTO $orderDef): PaginableResource
|
||||
{
|
||||
$posts = $this->postOrder->apply(
|
||||
$this->postFilter->apply(Post::with(['category', 'tags', 'comments']), $filters),
|
||||
$orderDef
|
||||
)->paginate($this->paginate, page: $page);
|
||||
return PaginableResource::createFromLengthAwarePaginator($posts);
|
||||
}
|
||||
|
||||
public function findPost(int $id): ?Post
|
||||
{
|
||||
return Post::with(['category', 'tags', 'comments'])->find($id);
|
||||
}
|
||||
|
||||
public function storePost(array $data): Post
|
||||
{
|
||||
DB::beginTransaction();
|
||||
|
||||
$post = Post::create($data);
|
||||
|
||||
if (isset($data['tags'])) {
|
||||
$tags = [];
|
||||
|
||||
foreach ($data['tags'] as $tag) {
|
||||
$tag = Tag::firstOrCreate(['name' => $tag]);
|
||||
$tags[] = $tag;
|
||||
}
|
||||
|
||||
$post->tags()->sync($tags);
|
||||
}
|
||||
|
||||
$post = $this->findPost($post->id);
|
||||
|
||||
if ($post === null) {
|
||||
throw new \InvalidArgumentException('This should never happen - post is null');
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $post;
|
||||
}
|
||||
|
||||
public function updatePost(array $data, int $id): ?Post
|
||||
{
|
||||
DB::beginTransaction();
|
||||
|
||||
$post = Post::find($id);
|
||||
|
||||
if ($post === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isset($data['tags'])) {
|
||||
$tags = [];
|
||||
|
||||
foreach ($data['tags'] as $tag) {
|
||||
$tag = Tag::firstOrCreate(['name' => $tag]);
|
||||
$tags[] = $tag;
|
||||
}
|
||||
|
||||
$post->tags()->sync($tags);
|
||||
}
|
||||
|
||||
$post->update($data);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $this->findPost($post->id);
|
||||
}
|
||||
|
||||
public function deletePost(int $id): bool
|
||||
{
|
||||
$post = Post::find($id);
|
||||
|
||||
if ($post === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$post->delete();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
33
app/Services/Post/PostServiceInterface.php
Normal file
33
app/Services/Post/PostServiceInterface.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Post;
|
||||
|
||||
use App\Models\Post;
|
||||
use App\Services\PaginableResource;
|
||||
use App\Services\QueryRequestModifiers\Post\PostFilterDTO;
|
||||
use App\Services\QueryRequestModifiers\Post\PostOrderDTO;
|
||||
|
||||
interface PostServiceInterface
|
||||
{
|
||||
/**
|
||||
* @return PaginableResource<Post>
|
||||
*/
|
||||
public function fetchPosts(int $page, ?PostFilterDTO $filters, ?PostOrderDTO $orderDef): PaginableResource;
|
||||
|
||||
public function findPost(int $id): ?Post;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @return Post
|
||||
*/
|
||||
public function storePost(array $data): Post;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function updatePost(array $data, int $id): ?Post;
|
||||
|
||||
public function deletePost(int $id): bool;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Category;
|
||||
|
||||
use App\Http\Requests\Category\ListCategoryRequest;
|
||||
use App\Models\Category;
|
||||
use App\Services\QueryRequestModifiers\Filterable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class CategoryFilter
|
||||
{
|
||||
/**
|
||||
* @use Filterable<Category, CategoryFilterDTO>
|
||||
*/
|
||||
use Filterable;
|
||||
|
||||
/**
|
||||
* @param Builder<Category> $query
|
||||
* @return Builder<Category>
|
||||
*/
|
||||
public function apply(Builder $query, ?CategoryFilterDTO $filters): Builder
|
||||
{
|
||||
return $this->applyFilterable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListCategoryRequest $request): ?CategoryFilterDTO
|
||||
{
|
||||
return $this->makeFilterableFromRequest($request, CategoryFilterDTO::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function validateRules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'sometimes|string|max:255',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
protected static function keys(): array
|
||||
{
|
||||
return ['name'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Category;
|
||||
|
||||
use App\Services\CacheKeyInterface;
|
||||
|
||||
class CategoryFilterDTO implements CacheKeyInterface
|
||||
{
|
||||
/**
|
||||
* @param array<string, string> $filters
|
||||
*/
|
||||
public function __construct(public array $filters)
|
||||
{
|
||||
}
|
||||
|
||||
public function toCacheKey(): string
|
||||
{
|
||||
return http_build_query($this->filters, '', '|');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Category;
|
||||
|
||||
use App\Http\Requests\Category\ListCategoryRequest;
|
||||
use App\Models\Category;
|
||||
use App\Services\QueryRequestModifiers\Orderable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class CategoryOrder
|
||||
{
|
||||
/**
|
||||
* @use Orderable<Category, CategoryOrderDTO>
|
||||
*/
|
||||
use Orderable;
|
||||
|
||||
|
||||
/**
|
||||
* @param Builder<Category> $query
|
||||
* @return Builder<Category>
|
||||
*/
|
||||
public function apply(Builder $query, ?CategoryOrderDTO $filters): Builder
|
||||
{
|
||||
return $this->applyOrderable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListCategoryRequest $request): ?CategoryOrderDTO
|
||||
{
|
||||
return $this->makeOrderableFromRequest($request, CategoryOrderDTO::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Category;
|
||||
|
||||
use App\Services\CacheKeyInterface;
|
||||
use App\Services\QueryRequestModifiers\OrderableDTO;
|
||||
use App\Services\QueryRequestModifiers\SortDirection;
|
||||
|
||||
/**
|
||||
* @implements OrderableDTO<CategoryOrderDTO>
|
||||
*/
|
||||
class CategoryOrderDTO implements CacheKeyInterface, OrderableDTO
|
||||
{
|
||||
public function __construct(public string $column, public SortDirection $direction)
|
||||
{
|
||||
}
|
||||
|
||||
public function toCacheKey(): string
|
||||
{
|
||||
return $this->column . '|' . $this->direction->value;
|
||||
}
|
||||
|
||||
public function getColumn(): string
|
||||
{
|
||||
return $this->column;
|
||||
}
|
||||
|
||||
public function getDirection(): SortDirection
|
||||
{
|
||||
return $this->direction;
|
||||
}
|
||||
|
||||
public static function createFromValues(string $column, SortDirection $sortDirection): OrderableDTO
|
||||
{
|
||||
return new self(
|
||||
$column,
|
||||
$sortDirection
|
||||
);
|
||||
}
|
||||
}
|
||||
51
app/Services/QueryRequestModifiers/Comment/CommentFilter.php
Normal file
51
app/Services/QueryRequestModifiers/Comment/CommentFilter.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Comment;
|
||||
|
||||
use App\Http\Requests\Comment\ListCommentRequest;
|
||||
use App\Models\Comment;
|
||||
use App\Services\QueryRequestModifiers\Filterable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class CommentFilter
|
||||
{
|
||||
/**
|
||||
* @use Filterable<Comment, CommentFilterDTO>
|
||||
*/
|
||||
use Filterable;
|
||||
|
||||
/**
|
||||
* @param Builder<Comment> $query
|
||||
* @return Builder<Comment>
|
||||
*/
|
||||
public function apply(Builder $query, ?CommentFilterDTO $filters): Builder
|
||||
{
|
||||
return $this->applyFilterable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListCommentRequest $request): ?CommentFilterDTO
|
||||
{
|
||||
return $this->makeFilterableFromRequest($request, CommentFilterDTO::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function validateRules(): array
|
||||
{
|
||||
return [
|
||||
'content' => 'sometimes|string',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
protected static function keys(): array
|
||||
{
|
||||
return ['content'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Comment;
|
||||
|
||||
use App\Services\CacheKeyInterface;
|
||||
|
||||
readonly class CommentFilterDTO implements CacheKeyInterface
|
||||
{
|
||||
/**
|
||||
* @param array<string, string> $filters
|
||||
*/
|
||||
public function __construct(public array $filters)
|
||||
{
|
||||
}
|
||||
|
||||
public function toCacheKey(): string
|
||||
{
|
||||
return http_build_query($this->filters, '', '|');
|
||||
}
|
||||
}
|
||||
33
app/Services/QueryRequestModifiers/Comment/CommentOrder.php
Normal file
33
app/Services/QueryRequestModifiers/Comment/CommentOrder.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Comment;
|
||||
|
||||
use App\Http\Requests\Comment\ListCommentRequest;
|
||||
use App\Models\Comment;
|
||||
use App\Services\QueryRequestModifiers\Orderable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class CommentOrder
|
||||
{
|
||||
/**
|
||||
* @use Orderable<Comment, CommentOrderDTO>
|
||||
*/
|
||||
use Orderable;
|
||||
|
||||
|
||||
/**
|
||||
* @param Builder<Comment> $query
|
||||
* @return Builder<Comment>
|
||||
*/
|
||||
public function apply(Builder $query, ?CommentOrderDTO $filters): Builder
|
||||
{
|
||||
return $this->applyOrderable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListCommentRequest $request): ?CommentOrderDTO
|
||||
{
|
||||
return $this->makeOrderableFromRequest($request, CommentOrderDTO::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Comment;
|
||||
|
||||
use App\Services\QueryRequestModifiers\OrderableDTO;
|
||||
use App\Services\QueryRequestModifiers\SortDirection;
|
||||
|
||||
/**
|
||||
* @implements OrderableDTO<CommentOrderDTO>
|
||||
*/
|
||||
readonly class CommentOrderDTO implements OrderableDTO
|
||||
{
|
||||
public function __construct(public string $column, public SortDirection $direction)
|
||||
{
|
||||
}
|
||||
|
||||
public function toCacheKey(): string
|
||||
{
|
||||
return $this->column . '|' . $this->direction->value;
|
||||
}
|
||||
|
||||
public function getColumn(): string
|
||||
{
|
||||
return $this->column;
|
||||
}
|
||||
|
||||
public function getDirection(): SortDirection
|
||||
{
|
||||
return $this->direction;
|
||||
}
|
||||
|
||||
public static function createFromValues(string $column, SortDirection $sortDirection): OrderableDTO
|
||||
{
|
||||
return new self(
|
||||
$column,
|
||||
$sortDirection
|
||||
);
|
||||
}
|
||||
}
|
||||
52
app/Services/QueryRequestModifiers/Filterable.php
Normal file
52
app/Services/QueryRequestModifiers/Filterable.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
/**
|
||||
* @template C of Model
|
||||
* @template D
|
||||
*/
|
||||
trait Filterable
|
||||
{
|
||||
abstract public static function keys();
|
||||
|
||||
/**
|
||||
* @param Builder<C> $query
|
||||
* @return Builder<C>
|
||||
*/
|
||||
protected function applyFilterable(Builder $query, ?object $filters): Builder
|
||||
{
|
||||
if ($filters === null) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
foreach (self::keys() as $filterName) {
|
||||
if (isset($filters->filters[$filterName])) {
|
||||
$query->where($filterName, '=', $filters->filters[$filterName]);
|
||||
}
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string<D> $className
|
||||
* @return D|null
|
||||
*/
|
||||
protected function makeFilterableFromRequest(FormRequest $request, string $className): ?object
|
||||
{
|
||||
$keys = $request->all(self::keys());
|
||||
|
||||
// no filtering
|
||||
if (count($keys) === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new $className($keys);
|
||||
}
|
||||
}
|
||||
75
app/Services/QueryRequestModifiers/Orderable.php
Normal file
75
app/Services/QueryRequestModifiers/Orderable.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
/**
|
||||
* @template C of Model
|
||||
* @template D of OrderableDTO
|
||||
*/
|
||||
trait Orderable
|
||||
{
|
||||
/**
|
||||
* @param Builder<C> $query
|
||||
* @param ?OrderableDTO<D> $orderDef
|
||||
* @return Builder<C>
|
||||
*/
|
||||
protected function applyOrderable(Builder $query, ?OrderableDTO $orderDef): Builder
|
||||
{
|
||||
if ($orderDef !== null) {
|
||||
$query->orderBy($orderDef->getColumn(), $orderDef->getDirection()->value);
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string<D> $className
|
||||
* @return D|null
|
||||
*/
|
||||
protected function makeOrderableFromRequest(FormRequest $request, string $className): ?object
|
||||
{
|
||||
$keys = $request->all(self::keys());
|
||||
|
||||
if (!isset($keys['order']) || !isset($keys['direction'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$validator = Validator::make($keys, $this->validateRules());
|
||||
|
||||
if ($validator->fails()) {
|
||||
// this should never happen... Invalid request
|
||||
throw new \InvalidArgumentException($validator->errors()->first());
|
||||
}
|
||||
|
||||
$column = $keys['order'];
|
||||
$direction = $keys['direction'];
|
||||
|
||||
return call_user_func([$className, 'createFromValues'], $column, SortDirection::from($direction));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function validateRules(): array
|
||||
{
|
||||
return [
|
||||
'order' => 'sometimes|string|in:title',
|
||||
'direction' => 'sometimes|string|in:asc,desc',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
protected static function keys(): array
|
||||
{
|
||||
return ['order', 'direction'];
|
||||
}
|
||||
}
|
||||
19
app/Services/QueryRequestModifiers/OrderableDTO.php
Normal file
19
app/Services/QueryRequestModifiers/OrderableDTO.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers;
|
||||
|
||||
/**
|
||||
* @template T of OrderableDTO
|
||||
*/
|
||||
interface OrderableDTO
|
||||
{
|
||||
public function getColumn(): string;
|
||||
public function getDirection(): SortDirection;
|
||||
|
||||
/**
|
||||
* @return T
|
||||
*/
|
||||
public static function createFromValues(string $column, SortDirection $sortDirection): self;
|
||||
}
|
||||
52
app/Services/QueryRequestModifiers/Post/PostFilter.php
Normal file
52
app/Services/QueryRequestModifiers/Post/PostFilter.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Post;
|
||||
|
||||
use App\Http\Requests\Post\ListPostRequest;
|
||||
use App\Models\Post;
|
||||
use App\Services\QueryRequestModifiers\Filterable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class PostFilter
|
||||
{
|
||||
/**
|
||||
* @use Filterable<Post, PostFilterDTO>
|
||||
*/
|
||||
use Filterable;
|
||||
|
||||
/**
|
||||
* @param Builder<Post> $query
|
||||
* @return Builder<Post>
|
||||
*/
|
||||
public function apply(Builder $query, ?PostFilterDTO $filters): Builder
|
||||
{
|
||||
return $this->applyFilterable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListPostRequest $request): ?PostFilterDTO
|
||||
{
|
||||
return $this->makeFilterableFromRequest($request, PostFilterDTO::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function validateRules(): array
|
||||
{
|
||||
return [
|
||||
'title' => 'sometimes|string',
|
||||
'content' => 'sometimes|string',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
protected static function keys(): array
|
||||
{
|
||||
return ['title', 'content'];
|
||||
}
|
||||
}
|
||||
22
app/Services/QueryRequestModifiers/Post/PostFilterDTO.php
Normal file
22
app/Services/QueryRequestModifiers/Post/PostFilterDTO.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Post;
|
||||
|
||||
use App\Services\CacheKeyInterface;
|
||||
|
||||
readonly class PostFilterDTO implements CacheKeyInterface
|
||||
{
|
||||
/**
|
||||
* @param array<string, string> $filters
|
||||
*/
|
||||
public function __construct(public array $filters)
|
||||
{
|
||||
}
|
||||
|
||||
public function toCacheKey(): string
|
||||
{
|
||||
return http_build_query($this->filters, '', '|');
|
||||
}
|
||||
}
|
||||
33
app/Services/QueryRequestModifiers/Post/PostOrder.php
Normal file
33
app/Services/QueryRequestModifiers/Post/PostOrder.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Post;
|
||||
|
||||
use App\Http\Requests\Post\ListPostRequest;
|
||||
use App\Models\Post;
|
||||
use App\Services\QueryRequestModifiers\Orderable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class PostOrder
|
||||
{
|
||||
/**
|
||||
* @use Orderable<Post, PostOrderDTO>
|
||||
*/
|
||||
use Orderable;
|
||||
|
||||
|
||||
/**
|
||||
* @param Builder<Post> $query
|
||||
* @return Builder<Post>
|
||||
*/
|
||||
public function apply(Builder $query, ?PostOrderDTO $filters): Builder
|
||||
{
|
||||
return $this->applyOrderable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListPostRequest $request): ?PostOrderDTO
|
||||
{
|
||||
return $this->makeOrderableFromRequest($request, PostOrderDTO::class);
|
||||
}
|
||||
}
|
||||
42
app/Services/QueryRequestModifiers/Post/PostOrderDTO.php
Normal file
42
app/Services/QueryRequestModifiers/Post/PostOrderDTO.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Post;
|
||||
|
||||
use App\Services\CacheKeyInterface;
|
||||
use App\Services\QueryRequestModifiers\SortDirection;
|
||||
use App\Services\QueryRequestModifiers\OrderableDTO;
|
||||
|
||||
/**
|
||||
* @implements OrderableDTO<PostOrderDTO>
|
||||
*/
|
||||
readonly class PostOrderDTO implements CacheKeyInterface, OrderableDTO
|
||||
{
|
||||
public function __construct(public string $column, public SortDirection $direction)
|
||||
{
|
||||
}
|
||||
|
||||
public function toCacheKey(): string
|
||||
{
|
||||
return $this->column . '|' . $this->direction->value;
|
||||
}
|
||||
|
||||
public function getColumn(): string
|
||||
{
|
||||
return $this->column;
|
||||
}
|
||||
|
||||
public function getDirection(): SortDirection
|
||||
{
|
||||
return $this->direction;
|
||||
}
|
||||
|
||||
public static function createFromValues(string $column, SortDirection $sortDirection): OrderableDTO
|
||||
{
|
||||
return new self(
|
||||
$column,
|
||||
$sortDirection
|
||||
);
|
||||
}
|
||||
}
|
||||
11
app/Services/QueryRequestModifiers/SortDirection.php
Normal file
11
app/Services/QueryRequestModifiers/SortDirection.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers;
|
||||
|
||||
enum SortDirection: string
|
||||
{
|
||||
case ASC = "asc";
|
||||
case DESC = "desc";
|
||||
}
|
||||
Reference in New Issue
Block a user