diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6ea1f07 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ + +node_modules +./node_modules +/.vscode +/.vs + +package-lock.json diff --git a/constant.js b/constant.js new file mode 100644 index 0000000..baed4c0 --- /dev/null +++ b/constant.js @@ -0,0 +1,3 @@ +module.exports = { + UrlPrefix: "https://data.moviebuff.com/" +} \ No newline at end of file diff --git a/controller/index.js b/controller/index.js new file mode 100644 index 0000000..283aa9d --- /dev/null +++ b/controller/index.js @@ -0,0 +1,68 @@ + +const utils = require("../utils") + +module.exports = { + degreeOfSeparation: async (req, res, next) => { + try { + let separatedMovies = new Map(); + let includingMovies = []; + let moviesCheck = []; + let moviesData = await Promise.all([utils.fetchDataFromUrl(req.value.actor1), utils.fetchDataFromUrl(req.value.actor2)]) + .catch((err) => { + throw 'ACTOR_MOVIES_URL_ISSUE' + }) + + if (!moviesData[0]?.movies || !moviesData[1]?.movies) + throw 'MOVIES_NOT_FOUND' + + utils.pushDataInMap(separatedMovies, moviesData[0].movies, moviesData[0].name); + utils.pushDataInMap(separatedMovies, moviesData[1].movies, moviesData[1].name); + for (const [a, b] of separatedMovies) { + moviesCheck.push( + new Promise((resolve, reject) => { + utils.fetchDataFromUrl(a).then((element) => { + + if (element.error) { + includingMovies.push(element.url) + } + else { + let hasActor = (b == req.value.actor1) ? req.value.actor2 : req.value.actor1 + if (element.cast.find((e) => e.name == hasActor)) { + separatedMovies.delete(a); + } + } + resolve() + }) + .catch((err) => { + reject(err); + }) + }) + ) + } + await Promise.all(moviesCheck); + return utils.sendResponse(res, 200, 'Fetched Successfully', { + degreeOfSeparation: separatedMovies.size, + needToIncludeMovies: includingMovies + }); + } catch (error) { + let code = 400, message; + switch (error) { + case 'ACTOR_MOVIES_URL_ISSUE': + message = 'URL ISSUE'; + break; + case 'MOVIES_NOT_FOUND': + message = 'Requested data not found'; + break; + case 'INVALID_URL': + message = 'URL ISSUE'; + break; + default: + code = 500; + message = 'Something Went Wrong'; + break; + } + return utils.sendResponse(res, code, message, {}); + } + + } +} \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..c4c77ce --- /dev/null +++ b/index.js @@ -0,0 +1,11 @@ +const express = require('express'); +const validation = require('./validation'); +const controller = require('./controller'); +const app = express() + +app.get('/degree-of-separation', validation.degreeOfSeparation, controller.degreeOfSeparation) + + +app.listen(4000, () => { + console.log("App is running on the port>>>>4000") +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..02390b4 --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "degree_of_separation", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start": "node index.js" + }, + "author": "Suraj Rana", + "license": "ISC", + "dependencies": { + "axios": "^1.4.0", + "express": "^4.18.2", + "joi": "^17.9.2" + } +} \ No newline at end of file diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..ba21940 --- /dev/null +++ b/readme.txt @@ -0,0 +1,41 @@ +To install dependency -> npm i +To run the code -> npm start +running port -> 4000 + +request to hit the :- +Get http://localhost:4000/degree-of-separation?actor1=amitabh-bachchan&actor2=robert-de-niro + +send actor1, actor2 in query + +success response 200 +{ + "message": "Fetched Successfully", + "data": { + "degreeOfSeparation": 219, + "needToIncludeMovies": [ + "bollywood-the-greatest-love-story-ever-told", + "ki-and-ka", + "international-indian-film-awards", + "gangaa-jamunaa-saraswathi", + "naseeb", + "krantiveer-the-revolution", + "suhaag-1979-hindi", + "pink-2016-hindi", + "dafan", + "toofan-1989-hindi", + "zameer", + "49th-manikchand-filmfare-awards-2003", + "52nd-fair-one-filmfare-awards", + "ahsaas", + "chashme-buddoor", + "charandas", + "amrithadhare" + ] + } +} + +here + degreeOfSeparation -> desired solution + needToIncludeMovies -> while fetching data of some movies end point returns 403 for the + including the response in the degreeOfSeparation and the array of the following + movies has been mentioned in it. \ No newline at end of file diff --git a/utils/index.js b/utils/index.js new file mode 100644 index 0000000..3a56d99 --- /dev/null +++ b/utils/index.js @@ -0,0 +1,50 @@ +const constant = require("../constant"); +const { default: axios } = require("axios"); + +module.exports = { + sendResponse: (res, statusCode, message, data = {}) => { + res.status(statusCode).json({ + message: typeof message === "string" ? message : "", + data: data, + }); + }, + + validateJoi: (schema, req, res, next) => { + if (schema.error) { + const errMsg = schema.error.details[0].message; + return res.status(400).json({ + message: errMsg, + data: {} + }); + } else { + req.value = schema.value; + return next(); + } + }, + fetchDataFromUrl: function (url) { + return axios(constant.UrlPrefix + url).then((data) => { + if (data.status == 200) + return data.data + }).catch((err) => { + if (err.response.status == 403) { + return { + error: true, + url: url + } + }; + throw 'INVALID_URL' + }) + }, + + + pushDataInMap: (map, moviesArray, actorName) => { + moviesArray.forEach(element => { + if (map.has(element.url) && map.get(element.url) === actorName) { + map.delete(element.url); + } + else + map.set(element.url, actorName) + }); + }, + +} \ No newline at end of file diff --git a/validation/index.js b/validation/index.js new file mode 100644 index 0000000..68da79f --- /dev/null +++ b/validation/index.js @@ -0,0 +1,19 @@ +const Joi = require('joi'); +const UtilService = require('../utils'); + +module.exports = { + degreeOfSeparation: (req, res, next) => { + try { + const schema = Joi.object({ + actor1: Joi.string().required().min(1), + actor2: Joi.string().required().min(1), + }).validate(req.query || {}); + + UtilService.validateJoi(schema, req, res, next); + + } catch (err) { + return UtilService.sendResponse(res, 500, 'Something Went Wrong', {}); + + } + } +} \ No newline at end of file