feat: add Categories

This commit is contained in:
Nikita Kiselev
2025-07-14 22:34:51 +03:00
parent b25f6d3c73
commit 6a8ea048ea
13 changed files with 320 additions and 92 deletions

View File

@@ -0,0 +1,70 @@
<?php
namespace App\Handlers;
use Closure;
use Openguru\OpenCartFramework\Application;
use Openguru\OpenCartFramework\Http\JsonResponse;
use Openguru\OpenCartFramework\Http\Request;
use Openguru\OpenCartFramework\QueryBuilder\Builder;
use Openguru\OpenCartFramework\QueryBuilder\JoinClause;
class CategoriesHandler
{
private Builder $queryBuilder;
public function __construct(Builder $queryBuilder)
{
$this->queryBuilder = $queryBuilder;
}
public function index(Request $request): JsonResponse
{
$languageId = 1;
$perPage = $request->get('perPage', 10);
$imageWidth = 150;
$imageHeight = 150;
$categories = $this->queryBuilder->newQuery()
->select([
'categories.category_id' => 'id',
'categories.image' => 'image',
'descriptions.name' => 'name',
'descriptions.description' => 'description',
])
->from(db_table('category'), 'categories')
->join(
db_table('category_description') . ' AS descriptions',
function (JoinClause $join) use ($languageId) {
$join->on('categories.category_id', '=', 'descriptions.category_id')
->where('descriptions.language_id', '=', $languageId);
}
)
->where('categories.parent_id', '=', 0)
->where('categories.status', '=', 1)
->orderBy('sort_order')
->limit($perPage)
->get();
/** @var Closure $resize */
$resize = Application::getInstance()->get('image_resize');
return new JsonResponse([
'data' => array_map(static function ($category) use ($resize, $imageWidth, $imageHeight) {
if ($category['image']) {
$image = $resize($category['image'], $imageWidth, $imageHeight);
} else {
$image = $resize('no_image.png', $imageWidth, $imageHeight);
}
return [
'id' => (int)$category['id'],
'image' => $image,
'name' => $category['name'],
'description' => $category['description'],
];
}, $categories),
]);
}
}

View File

@@ -33,10 +33,21 @@ class ProductsHandler
$languageId = 1;
$page = $request->get('page', 1);
$perPage = $request->get('perPage', 10);
$categoryId = (int)$request->get('categoryId', 0);
$categoryName = '';
$imageWidth = 200;
$imageHeight = 200;
if ($categoryId) {
$categoryName = $this->queryBuilder->newQuery()
->select(['name'])
->from(db_table('category_description'), 'category')
->where('language_id', '=', $languageId)
->where('category_id', '=', $categoryId)
->value('name');
}
$products = $this->queryBuilder->newQuery()
->select([
'products.product_id' => 'product_id',
@@ -54,20 +65,34 @@ class ProductsHandler
->where('product_description.language_id', '=', $languageId);
}
)
->when($categoryId !== 0, function (Builder $query) use ($categoryId) {
$query->join(
db_table('product_to_category') . ' AS product_to_category',
function (JoinClause $join) use ($categoryId) {
$join->on('product_to_category.product_id', '=', 'products.product_id')
->where('product_to_category.category_id', '=', $categoryId);
}
);
})
->forPage($page, $perPage)
->orderBy('date_added', 'DESC')
->get();
$productIds = Arr::pluck($products, 'product_id');
$productsImages = $this->queryBuilder->newQuery()
->select([
'products_images.product_id' => 'product_id',
'products_images.image' => 'image',
])
->from(db_table('product_image'), 'products_images')
->orderBy('products_images.sort_order', 'ASC')
->whereIn('product_id', $productIds)
->get();
$productsImages = [];
if ($productIds) {
$productsImages = $this->queryBuilder->newQuery()
->select([
'products_images.product_id' => 'product_id',
'products_images.image' => 'image',
])
->from(db_table('product_image'), 'products_images')
->orderBy('products_images.sort_order', 'ASC')
->whereIn('product_id', $productIds)
->get();
}
/** @var Closure $resize */
$resize = Application::getInstance()->get('image_resize');
@@ -103,7 +128,7 @@ class ProductsHandler
$this->settings->get('oc_currency'),
);
if (! empty($productsImagesMap[$product['product_id']])) {
if (!empty($productsImagesMap[$product['product_id']])) {
$allImages = array_merge($allImages, $productsImagesMap[$product['product_id']]);
}
@@ -115,12 +140,16 @@ class ProductsHandler
'images' => $allImages,
];
}, $products),
'meta' => [
'currentCategoryName' => $categoryName,
]
]);
}
public function show(Request $request): JsonResponse
{
$productId = (int) $request->get('id');
$productId = (int)$request->get('id');
$languageId = 1;
$imageWidth = 500;
$imageHeight = 500;

View File

@@ -1,5 +1,6 @@
<?php
use App\Handlers\CategoriesHandler;
use App\Handlers\HelloWorldHandler;
use App\Handlers\OrderCreateHandler;
use App\Handlers\ProductsHandler;
@@ -8,4 +9,6 @@ return [
'products' => [ProductsHandler::class, 'handle'],
'product_show' => [ProductsHandler::class, 'show'],
'order_create' => [OrderCreateHandler::class, 'handle'],
'categoriesList' => [CategoriesHandler::class, 'index'],
];