Skip to content

Commit

Permalink
add: latest phone endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
azharimm committed Sep 5, 2021
1 parent 1361b88 commit 1e76e66
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/controllers/v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ exports.index = (req, res) => {
endpoint: '/v2/search',
example: fullUrl+'/search?query=Iphone 12 pro max'
},
latest: {
endpoint: '/v2/latest',
example: fullUrl+'/latest'
},
});
}
34 changes: 34 additions & 0 deletions src/controllers/v2/miscController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const request = require("request-promise");
const cheerio = require("cheerio");
const { json, errorJson } = require("../../utils/response");

exports.index = async (req, res) => {
try {
const url = `${process.env.BASE_URL}`;
const htmlResult = await request.get(url);
const $ = await cheerio.load(htmlResult);
const title = "Latest Devices";
const phones = [];
$(".module-latest")
.find("a")
.each((index, el) => {
const phone_name = $(el).text();
const image = $(el).find("img").attr("src");
const slug = $(el).attr("href").replace(".php", "");
phones.push({
phone_name,
slug,
image,
detail:
req.protocol + "://" + req.get("host") + "/v2/" + slug,
});
});

return json(res, {
title,
phones,
});
} catch (error) {
return errorJson(res, error);
}
};
2 changes: 2 additions & 0 deletions src/routes/v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ const indexController = require('../../controllers/v2/index')
const brandController = require('../../controllers/v2/brandController')
const specController = require('../../controllers/v2/specController')
const searchController = require('../../controllers/v2/searchController')
const miscController = require('../../controllers/v2/miscController')

router.get("/", indexController.index);
router.get("/brands", brandController.index);
router.get("/brands/:slug", brandController.show);
router.get("/search", searchController.index);
router.get("/latest", miscController.index);
router.get("/:slug", specController.index);

module.exports = router;

0 comments on commit 1e76e66

Please sign in to comment.