From fac50805bd302e3c6e859fc3a45c97c70bfc1364 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Sun, 24 Jan 2021 13:59:34 +0100 Subject: [PATCH] Get stores from vinmonopolet. Update all endpoint names to distinguish between wine and store actions. Renamed wineController to vinmonopoletController. --- api/controllers/vinmonopoletController.js | 85 +++++++++++++++++++++++ api/controllers/wineController.js | 44 ------------ api/vinmonopolet.js | 52 +++++++++++--- 3 files changed, 129 insertions(+), 52 deletions(-) create mode 100644 api/controllers/vinmonopoletController.js delete mode 100644 api/controllers/wineController.js diff --git a/api/controllers/vinmonopoletController.js b/api/controllers/vinmonopoletController.js new file mode 100644 index 0000000..eac34b1 --- /dev/null +++ b/api/controllers/vinmonopoletController.js @@ -0,0 +1,85 @@ +const path = require("path"); +const vinmonopoletRepository = require(path.join(__dirname, "../vinmonopolet")); + +function searchWines(req, res) { + const { name, page } = req.query; + + return vinmonopoletRepository.searchWinesByName(name, page).then(wines => + res.json({ + wines: wines, + count: wines.length, + page: page, + success: true + }) + ); +} + +function wineByEAN(req, res) { + const { ean } = req.params; + + return vinmonopoletRepository.searchByEAN(ean).then(wines => + res.json({ + wines: wines, + success: true + }) + ); +} + +function wineById(req, res) { + const { id } = req.params; + + return vinmonopoletRepository.searchById(id).then(wines => + res.json({ + wine: wines[0], + success: true + }) + ); +} + +function allStores(req, res) { + return vinmonopoletRepository + .allStores() + .then(stores => + res.send({ + stores, + success: true + }) + ) + .catch(error => { + const { statusCode, message } = error; + + return res.status(statusCode || 500).send({ + message: message || "Unexpected error occured while fetch all vinmonopolet stores.", + success: false + }); + }); +} + +function searchStores(req, res) { + const { name } = req.query; + + return vinmonopoletRepository + .searchStoresByName(name) + .then(stores => + res.send({ + stores, + success: true + }) + ) + .catch(error => { + const { statusCode, message } = error; + + return res.status(statusCode || 500).send({ + message: message || "Unexpected error occured while fetch all vinmonopolet stores.", + success: false + }); + }); +} + +module.exports = { + searchWines, + wineByEAN, + wineById, + allStores, + searchStores +}; diff --git a/api/controllers/wineController.js b/api/controllers/wineController.js deleted file mode 100644 index 99390e0..0000000 --- a/api/controllers/wineController.js +++ /dev/null @@ -1,44 +0,0 @@ -const path = require("path"); -const vinmonopoletRepository = require(path.join(__dirname, "../vinmonopolet")); - -function search(req, res) { - const { query, page } = req.query; - console.log(query, page); - - return vinmonopoletRepository.searchByQuery(query, page).then(wines => - res.json({ - wines: wines, - count: wines.length, - page: page, - success: true - }) - ); -} - -function ean(req, res) { - const { ean } = req.params; - - return vinmonopoletRepository.searchByEAN(ean).then(wines => - res.json({ - wines: wines, - success: true - }) - ); -} - -function id(req, res) { - const { id } = req.params; - - return vinmonopoletRepository.searchById(id).then(wines => - res.json({ - wine: wines[0], - success: true - }) - ); -} - -module.exports = { - search, - ean, - id -}; diff --git a/api/vinmonopolet.js b/api/vinmonopolet.js index 32dbacc..15d40c2 100644 --- a/api/vinmonopolet.js +++ b/api/vinmonopolet.js @@ -17,14 +17,22 @@ const convertToOurWineObject = wine => { } }; -const searchByQuery = async (query, page = 1) => { +const convertToOurStoreObject = store => { + return { + id: store.storeId, + name: store.storeName, + ...store.address + }; +}; + +const searchWinesByName = async (name, page = 1) => { const pageSize = 15; let url = new URL( `https://apis.vinmonopolet.no/products/v0/details-normal?productShortNameContains=gato&maxResults=15` ); url.searchParams.set("maxResults", pageSize); url.searchParams.set("start", pageSize * (page - 1)); - url.searchParams.set("productShortNameContains", query); + url.searchParams.set("productShortNameContains", name); const vinmonopoletResponse = await fetch(url, { headers: { @@ -50,14 +58,14 @@ const searchByQuery = async (query, page = 1) => { return winesConverted; }; -const searchByEAN = ean => { +const wineByEAN = ean => { const url = `https://app.vinmonopolet.no/vmpws/v2/vmp/products/barCodeSearch/${ean}`; return fetch(url) .then(resp => resp.json()) .then(response => response.map(convertToOurWineObject)); }; -const searchById = id => { +const wineById = id => { const url = `https://apis.vinmonopolet.no/products/v0/details-normal?productId=${id}`; const options = { headers: { @@ -70,8 +78,36 @@ const searchById = id => { .then(response => response.map(convertToOurWineObject)); }; -module.exports = { - searchByQuery, - searchByEAN, - searchById +const allStores = () => { + const url = `https://apis.vinmonopolet.no/stores/v0/details`; + const options = { + headers: { + "Ocp-Apim-Subscription-Key": config.vinmonopoletToken + } + }; + + return fetch(url, options) + .then(resp => resp.json()) + .then(response => response.map(convertToOurStoreObject)); +}; + +const searchStoresByName = name => { + const url = `https://apis.vinmonopolet.no/stores/v0/details?storeNameContains=${name}`; + const options = { + headers: { + "Ocp-Apim-Subscription-Key": config.vinmonopoletToken + } + }; + + return fetch(url, options) + .then(resp => resp.json()) + .then(response => response.map(convertToOurStoreObject)); +}; + +module.exports = { + searchWinesByName, + wineByEAN, + wineById, + allStores, + searchStoresByName };