Skip to content

Commit d9ccc1f

Browse files
authored
Task 5175: product review and search filter API fixed (#6)
Co-authored-by: ct-amit <[email protected]>
1 parent f621d54 commit d9ccc1f

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

src/data/products.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const products = [
7474
image: 'prod-4.png',
7575
name: 'Luxury Watches Centrix Gold',
7676
brand: 'Centrix',
77-
offer: '30%',
77+
offer: '20%',
7878
description: '7655 Couple (Refurbished)...',
7979
about: 'This product from Centrix is a classic choice who love classical products. It it made of pure gold and weighs around 50 grams',
8080
quantity: 3,
@@ -119,7 +119,7 @@ export const products = [
119119
image: 'prod-6.png',
120120
name: 'Apple iPhone 13 Mini ',
121121
brand: 'Apple',
122-
offer: '30%',
122+
offer: '70%',
123123
description: '13 cm (5.4-inch) Super',
124124
about:
125125
'It fits for those who love photography since it has 48MP camera which shoots great photos even in low light. Also it has 8GB of RAm and 4000MAH battery which can last upto 12 hours a day ',

src/pages/api/products/filter.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,18 @@ export default async function handler(req, res) {
3333
let searchMatches = true;
3434

3535
if (filter.search) {
36-
const properties = ['name', 'description', 'rating', 'salePrice', 'offerPrice', 'gender'];
36+
const propertiesString = ['name', 'brand', 'offer'];
37+
const propertiesNumberic = ['rating', 'salePrice', 'offerPrice'];
3738
let containsQuery = false;
3839

39-
properties.forEach((property) => {
40-
if (product[property].toString().toLowerCase().includes(filter.search.toString().toLowerCase())) {
40+
propertiesString.forEach((property) => {
41+
if (product[property] && product[property].toString().toLowerCase().includes(filter.search.toString().toLowerCase())) {
42+
containsQuery = true;
43+
}
44+
});
45+
46+
propertiesNumberic.forEach((property) => {
47+
if (product[property] && Number(product[property]) === Number(filter.search)) {
4148
containsQuery = true;
4249
}
4350
});

src/pages/api/review/list.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { sub } from 'date-fns';
22
import { Chance } from 'chance';
3+
import cors from 'utils/cors';
34

45
const chance = new Chance();
56

@@ -39,6 +40,7 @@ const productReviews = [
3940
}
4041
];
4142

42-
export default function handler(req, res) {
43-
res.status(200).json({ productReviews });
43+
export default async function handler(req, res) {
44+
await cors(req, res);
45+
return res.status(200).json({ productReviews });
4446
}

0 commit comments

Comments
 (0)