Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: creted end point #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

node_modules
./node_modules
/.vscode
/.vs

package-lock.json
3 changes: 3 additions & 0 deletions constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
UrlPrefix: "https://data.moviebuff.com/"
}
68 changes: 68 additions & 0 deletions controller/index.js
Original file line number Diff line number Diff line change
@@ -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, {});
}

}
}
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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")
});
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
41 changes: 41 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -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.
50 changes: 50 additions & 0 deletions utils/index.js
Original file line number Diff line number Diff line change
@@ -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)
});
},

}
19 changes: 19 additions & 0 deletions validation/index.js
Original file line number Diff line number Diff line change
@@ -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', {});

}
}
}