diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2024-11-15 16:17:13 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2024-11-15 16:17:13 +0700 |
| commit | 607ddd5050b5ee900606984b60e74d094fbe89f9 (patch) | |
| tree | 4abc6d8576be0c447a60af969022c04516777ddd /src | |
| parent | 9e967a55c575d24b740620325838ab857e7eef04 (diff) | |
<iman> update fix code
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/home/components/PreferredBrand.jsx | 3 | ||||
| -rw-r--r-- | src/pages/api/shop/preferredBrand.js | 69 |
2 files changed, 43 insertions, 29 deletions
diff --git a/src/lib/home/components/PreferredBrand.jsx b/src/lib/home/components/PreferredBrand.jsx index 9fb1228a..b7a30503 100644 --- a/src/lib/home/components/PreferredBrand.jsx +++ b/src/lib/home/components/PreferredBrand.jsx @@ -19,9 +19,8 @@ const PreferredBrand = () => { setIsLoading(true); const name = startWith ? `${startWith}*` : ''; const result = await axios( - `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/brands?rows=20` + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/preferredBrand?rows=20` ); - setIsLoading(false); setManufactures((manufactures) => [...result.data]); }, [startWith]); diff --git a/src/pages/api/shop/preferredBrand.js b/src/pages/api/shop/preferredBrand.js index 9480c63b..4cb35c84 100644 --- a/src/pages/api/shop/preferredBrand.js +++ b/src/pages/api/shop/preferredBrand.js @@ -1,6 +1,7 @@ -import odooApi from '@/core/api/odooApi'; +import axios from 'axios'; import { createClient } from 'redis'; +const SOLR_HOST = process.env.SOLR_HOST; const client = createClient(); client.on('error', (err) => console.error('Redis Client Error', err)); @@ -12,35 +13,49 @@ const connectRedis = async () => { }; export default async function handler(req, res) { + await connectRedis(); + try { - await connectRedis(); - // await client.del('preferredBrand'); - - let cachedData; - if (req.method === 'GET') { - cachedData = await client.get('preferredBrand'); - - if (!cachedData) { - const items = await odooApi( - 'GET', - '/api/v1/manufacture?level=prioritas' - ); - await client.set( - 'preferredBrand', - JSON.stringify(items), - 'EX', - 259200 // Expiry 3 hari - ); - cachedData = await client.get('preferredBrand'); + let params = '*:*'; + let sort = + 'sort=if(exists(sequence_i),0,1) asc,sequence_i asc, if(exists(image_s),0,1) asc '; + let rows = 20; + + if (req.query.params) { + rows = 20; + switch (req.query.params) { + case 'level_s': + params = 'level_s:prioritas'; + break; + case 'search': + params = `name_s:"${req.query.q.toLowerCase()}"`; + sort = ''; + rows = 1; + break; + default: + params = `name_s:${req.query.params}`.toLowerCase(); } - const data = cachedData ? JSON.parse(cachedData) : null; - res.status(200).json({ data }); - } else { - res.setHeader('Allow', ['GET']); - res.status(405).end(`Method ${req.method} Not Allowed`); } + if (req.query.rows) rows = req.query.rows; + + const url = `${SOLR_HOST}/solr/brands/select?q=${params}&q.op=OR&indent=true&rows=${rows}&${sort}`; + const brands = await axios(url); + const dataBrands = responseMap(brands.data.response.docs); + + res.status(200).json(dataBrands); } catch (error) { - console.error('Error interacting with Redis:', error); - res.status(500).json({ error: 'Error interacting with Redis' }); + console.error('Error fetching data from Solr:', error); + res.status(500).json({ error: 'Internal Server Error' }); } } + +const responseMap = (brands) => { + return brands.map((brand) => { + return { + id: brand.id, + name: brand.display_name_s, + logo: brand.image_s || '', + sequence: brand.sequence_i || '', + }; + }); +}; |
