Skip to content

Commit

Permalink
filteration done
Browse files Browse the repository at this point in the history
  • Loading branch information
devansh-webkul committed Jun 23, 2023
1 parent 2d701ed commit 12616d2
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 28 deletions.
65 changes: 48 additions & 17 deletions packages/Webkul/Category/src/Repositories/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,60 @@
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Webkul\Category\Contracts\Category;
use Webkul\Category\Models\CategoryTranslationProxy;
use Webkul\Core\Eloquent\Repository;

class CategoryRepository extends Repository
{
/**
* Specify model class name.
*
* @return string
*/
public function model(): string
{
return 'Webkul\Category\Contracts\Category';
return Category::class;
}

/**
* Get categories.
*
* @return void
*/
public function getAll(array $params = [])
{
$queryBuilder = $this->query()
->leftJoin('category_translations', 'category_translations.category_id', '=', 'categories.id');

if (isset($params['name'])) {
$queryBuilder->where('category_translations.name', 'like', '%' . urldecode($params['name']) . '%');
}

if (isset($params['description'])) {
$queryBuilder->where('category_translations.description', 'like', '%' . urldecode($params['description']) . '%');
}

if (isset($params['status'])) {
$queryBuilder->where('categories.status', $params['status']);
}

if (isset($params['only_children'])) {
$queryBuilder->whereNotNull('categories.parent_id');
}

if (isset($params['parent_id'])) {
$queryBuilder->where('categories.parent_id', $params['parent_id']);
}

if (isset($params['locale'])) {
$queryBuilder->where('category_translations.locale', $params['locale']);
}

return $queryBuilder->paginate();
}

/**
* Create category.
*
* @param array $data
* @return \Webkul\Category\Contracts\Category
*/
public function create(array $data)
Expand All @@ -49,7 +84,7 @@ public function create(array $data)

$this->uploadImages($data, $category);
$this->uploadImages($data, $category, 'category_banner');

if (isset($data['attributes'])) {
$category->filterableAttributes()->sync($data['attributes']);
}
Expand All @@ -60,7 +95,6 @@ public function create(array $data)
/**
* Update category.
*
* @param array $data
* @param int $id
* @param string $attribute
* @return \Webkul\Category\Contracts\Category
Expand Down Expand Up @@ -169,7 +203,7 @@ public function isSlugUnique($id, $slug)
/**
* Retrieve category from slug.
*
* @param string $slug
* @param string $slug
* @return \Webkul\Category\Contracts\Category
*/
public function findBySlug($slug)
Expand All @@ -184,7 +218,7 @@ public function findBySlug($slug)
/**
* Retrieve category from slug.
*
* @param string $slug
* @param string $slug
* @return \Webkul\Category\Contracts\Category
*/
public function findBySlugOrFail($slug)
Expand All @@ -203,7 +237,6 @@ public function findBySlugOrFail($slug)
/**
* Find by path.
*
* @param string $urlPath
* @return \Webkul\Category\Contracts\Category
*/
public function findByPath(string $urlPath)
Expand All @@ -216,15 +249,15 @@ public function findByPath(string $urlPath)
*
* @param array $data
* @param \Webkul\Category\Contracts\Category $category
* @param string $type
* @param string $type
* @return void
*/
public function uploadImages($data, $category, $type = 'image')
{

if (isset($data[$type])) {
$request = request();

foreach ($data[$type] as $imageId => $image) {
$file = $type . '.' . $imageId;
$dir = 'category/' . $category->id;
Expand All @@ -245,11 +278,10 @@ public function uploadImages($data, $category, $type = 'image')
}

$category->{$type} = null;

$category->save();
}
}


/**
* Get partials.
Expand Down Expand Up @@ -285,8 +317,7 @@ public function getPartial($columns = null)
* To Do: Move column from the `category_translations` to `category` table. And remove
* this created method.
*
* @param array $data
* @param string $attributeNames
* @param string $attributeNames
* @return array
*/
private function setSameAttributeValueToAllLocale(array $data, ...$attributeNames)
Expand All @@ -297,7 +328,7 @@ private function setSameAttributeValueToAllLocale(array $data, ...$attributeName

foreach ($attributeNames as $attributeName) {
foreach (core()->getAllLocales() as $locale) {
if ($requestedLocale == $locale->code) {
if ($requestedLocale == $locale->code) {
foreach ($model->translatedAttributes as $attribute) {
if ($attribute === $attributeName) {
$data[$locale->code][$attribute] = isset($data[$requestedLocale][$attribute])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public function __construct(
protected AttributeRepository $attributeRepository,
protected CategoryRepository $categoryRepository,
protected ProductRepository $productRepository

) {
}

Expand All @@ -29,10 +28,16 @@ public function __construct(
*/
public function index(): JsonResource
{
$categories = $this->categoryRepository
->whereNotNull('parent_id')
->where('status', 1)
->paginate();
/**
* These are the default parameters. By default, only the enabled category
* will be shown in the current locale.
*/
$defaultParams = [
'status' => 1,
'locale' => app()->getLocale(),
];

$categories = $this->categoryRepository->getAll(array_merge($defaultParams, request()->all()));

return CategoryResource::collection($categories);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class="w-[110px] h-[110px] rounded-full"
</div>

<p
class="text-black text-[20px] font-medium"
class="text-center text-black text-[20px] font-medium"
v-text="category.name"
>
</p>
Expand Down Expand Up @@ -78,6 +78,8 @@ class="bs-carousal-prev flex border border-black items-center justify-center rou
isLoading: true,
categories: [],
offset: 720,
};
},
Expand All @@ -100,13 +102,13 @@ class="bs-carousal-prev flex border border-black items-center justify-center rou
swipeLeft() {
const container = this.$refs.swiperContainer;
container.scrollLeft -= 720;
container.scrollLeft -= this.offset;
},
swipeRight() {
const container = this.$refs.swiperContainer;
container.scrollLeft += 720;
container.scrollLeft += this.offset;
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class="block mx-auto text-navyBlue text-base w-max font-medium py-[11px] px-[43p
isLoading: true,
products: [],
offset: 323,
};
},
Expand All @@ -91,13 +93,13 @@ class="block mx-auto text-navyBlue text-base w-max font-medium py-[11px] px-[43p
swipeLeft() {
const container = this.$refs.swiperContainer;
container.scrollLeft -= 720;
container.scrollLeft -= this.offset;
},
swipeRight() {
const container = this.$refs.swiperContainer;
container.scrollLeft += 720;
container.scrollLeft += this.offset;
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class="block text-[22px] py-[20px] font-medium text-center bg-[#E8EDFE] font-dms
{{-- Categories carousel --}}
<x-shop::categories.carousel
title="Categories Collections"
:src="route('shop.api.categories.index')"
:src="route('shop.api.categories.index', ['only_children' => true])"
:navigation-link="route('shop.home.index')"
>
</x-shop::categories.carousel>
Expand Down

0 comments on commit 12616d2

Please sign in to comment.