Get stores from vinmonopolet.
Update all endpoint names to distinguish between wine and store actions. Renamed wineController to vinmonopoletController.
This commit is contained in:
85
api/controllers/vinmonopoletController.js
Normal file
85
api/controllers/vinmonopoletController.js
Normal file
@@ -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
|
||||
};
|
||||
@@ -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
|
||||
};
|
||||
@@ -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
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user