taktik - laravel
This commit is contained in:
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;
|
||||
}
|
||||
Reference in New Issue
Block a user