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