Skip to content

Commit

Permalink
Task 5175: product review and search filter API fixed (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: ct-amit <[email protected]>
  • Loading branch information
ct-amit and ct-amit authored May 31, 2023
1 parent f621d54 commit d9ccc1f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/data/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const products = [
image: 'prod-4.png',
name: 'Luxury Watches Centrix Gold',
brand: 'Centrix',
offer: '30%',
offer: '20%',
description: '7655 Couple (Refurbished)...',
about: 'This product from Centrix is a classic choice who love classical products. It it made of pure gold and weighs around 50 grams',
quantity: 3,
Expand Down Expand Up @@ -119,7 +119,7 @@ export const products = [
image: 'prod-6.png',
name: 'Apple iPhone 13 Mini ',
brand: 'Apple',
offer: '30%',
offer: '70%',
description: '13 cm (5.4-inch) Super',
about:
'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 ',
Expand Down
13 changes: 10 additions & 3 deletions src/pages/api/products/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ export default async function handler(req, res) {
let searchMatches = true;

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

properties.forEach((property) => {
if (product[property].toString().toLowerCase().includes(filter.search.toString().toLowerCase())) {
propertiesString.forEach((property) => {
if (product[property] && product[property].toString().toLowerCase().includes(filter.search.toString().toLowerCase())) {
containsQuery = true;
}
});

propertiesNumberic.forEach((property) => {
if (product[property] && Number(product[property]) === Number(filter.search)) {
containsQuery = true;
}
});
Expand Down
6 changes: 4 additions & 2 deletions src/pages/api/review/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { sub } from 'date-fns';
import { Chance } from 'chance';
import cors from 'utils/cors';

const chance = new Chance();

Expand Down Expand Up @@ -39,6 +40,7 @@ const productReviews = [
}
];

export default function handler(req, res) {
res.status(200).json({ productReviews });
export default async function handler(req, res) {
await cors(req, res);
return res.status(200).json({ productReviews });
}

1 comment on commit d9ccc1f

@vercel
Copy link

@vercel vercel bot commented on d9ccc1f May 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.