Skip to content

Commit 52f6f4f

Browse files
committed
[backend] handle search product
1 parent ab2ab15 commit 52f6f4f

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

backend/src/product/product.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
22
import { IProduct } from './interfaces/product';
33
import { Product } from './entities/product.entity';
44
import { InjectRepository } from '@nestjs/typeorm';
5-
import { In, Repository } from 'typeorm';
5+
import { ILike, In, Repository } from 'typeorm';
66
import { ProductDetail } from './entities/product-details.entity';
77
import { SortByOption, filterByPrice } from 'src/utils/helpers';
88
import { IGetProductsResponse } from 'src/utils/types';
@@ -20,6 +20,7 @@ export class ProductService implements IProduct {
2020
async getAll(query): Promise<IGetProductsResponse> {
2121
const perPage = Number(query['perPage']) || 10;
2222
const currentPage = Number(query['page']) || 1;
23+
const searchValue = query['search'] || '';
2324

2425
const [products, totalProduct] = await this.productRepository.findAndCount({
2526
where: {
@@ -30,6 +31,7 @@ export class ProductService implements IProduct {
3031
},
3132
price: filterByPrice(query['price']),
3233
rate: query['star'] ? Number(query['star']) : undefined,
34+
name: ILike(`%${searchValue}%`),
3335
},
3436
relations: {
3537
comment: true,

frontend/src/components/SearchProductBar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const SearchProductBar = ({setSearchValueRoot}: {setSearchValueRoot: Dispatch<Se
55

66
const [searchValue, setSearchValue] = useState<string>("");
77

8-
const debounceSearchValue = useDebounce({ value: searchValue, delay: 1000});
8+
const debounceSearchValue = useDebounce({ value: searchValue, delay: 500});
99

1010
useEffect(()=>{
1111
setSearchValueRoot(debounceSearchValue);

0 commit comments

Comments
 (0)