|
@ -28,15 +28,22 @@ const tmdb = axios.create({ |
|
|
}, |
|
|
}, |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
(async function main() { |
|
|
const getProot = () => |
|
|
const posterRoot = await tmdb |
|
|
tmdb |
|
|
.get("/configuration", { |
|
|
.get("/configuration", { |
|
|
params: { |
|
|
params: { |
|
|
api_key: process.env.TMDB_KEY, |
|
|
api_key: process.env.TMDB_KEY, |
|
|
}, |
|
|
}, |
|
|
}) |
|
|
}) |
|
|
.then(res => res.data) |
|
|
.then(res => res.data) |
|
|
.then(data => data.images.base_url + "w500"); |
|
|
.then(data => data.images.base_url + "w500") |
|
|
|
|
|
.catch(e => { |
|
|
|
|
|
console.error("ERR on tmdb configuration", e.message); |
|
|
|
|
|
return getProot(); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
(async function main() { |
|
|
|
|
|
const posterRoot = await getProot(); |
|
|
|
|
|
|
|
|
app.use(bodyParser.json()); |
|
|
app.use(bodyParser.json()); |
|
|
|
|
|
|
|
@ -69,7 +76,7 @@ const tmdb = axios.create({ |
|
|
|
|
|
|
|
|
res.send({ success: true, msg: "Successfully created user." }); |
|
|
res.send({ success: true, msg: "Successfully created user." }); |
|
|
} catch (e) { |
|
|
} catch (e) { |
|
|
console.log(e); |
|
|
console.error("ERR on /register", e.message); |
|
|
|
|
|
|
|
|
res.statusCode = 500; |
|
|
res.statusCode = 500; |
|
|
res.send({ |
|
|
res.send({ |
|
@ -103,8 +110,6 @@ const tmdb = axios.create({ |
|
|
|
|
|
|
|
|
const verifyUser = async (req, res, next) => { |
|
|
const verifyUser = async (req, res, next) => { |
|
|
if (!req.headers["authentication"]) { |
|
|
if (!req.headers["authentication"]) { |
|
|
console.log(req.headers); |
|
|
|
|
|
|
|
|
|
|
|
res.statusCode = 401; |
|
|
res.statusCode = 401; |
|
|
return res.send({ |
|
|
return res.send({ |
|
|
success: false, |
|
|
success: false, |
|
@ -129,113 +134,147 @@ const tmdb = axios.create({ |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
app.get("/search/:query", verifyUser, async (req, res) => { |
|
|
app.get("/search/:query", verifyUser, async (req, res) => { |
|
|
const results = ( |
|
|
try { |
|
|
await tmdb.get(`/search/movie`, { |
|
|
const results = ( |
|
|
params: { |
|
|
await tmdb.get(`/search/movie`, { |
|
|
query: req.params.query, |
|
|
params: { |
|
|
api_key: process.env.TMDB_KEY, |
|
|
query: req.params.query, |
|
|
|
|
|
api_key: process.env.TMDB_KEY, |
|
|
|
|
|
}, |
|
|
|
|
|
}) |
|
|
|
|
|
).data.results; |
|
|
|
|
|
|
|
|
|
|
|
const movies = results.map( |
|
|
|
|
|
({ poster_path, overview, release_date, id, title }) => { |
|
|
|
|
|
const movie = { |
|
|
|
|
|
poster: posterRoot + poster_path, |
|
|
|
|
|
overview, |
|
|
|
|
|
release: release_date, |
|
|
|
|
|
movieId: id, |
|
|
|
|
|
title, |
|
|
|
|
|
}; |
|
|
|
|
|
return movie; |
|
|
}, |
|
|
}, |
|
|
}) |
|
|
); |
|
|
).data.results; |
|
|
|
|
|
|
|
|
|
|
|
const movies = results.map( |
|
|
res.send({ success: true, results: movies }); |
|
|
({ poster_path, overview, release_date, id, title }) => { |
|
|
} catch (e) { |
|
|
const movie = { |
|
|
console.error("ERR on /search", e.message); |
|
|
poster: posterRoot + poster_path, |
|
|
|
|
|
overview, |
|
|
|
|
|
release: release_date, |
|
|
|
|
|
movieId: id, |
|
|
|
|
|
title, |
|
|
|
|
|
}; |
|
|
|
|
|
return movie; |
|
|
|
|
|
}, |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
res.send({ success: true, results: movies }); |
|
|
res.statusCode = 500; |
|
|
|
|
|
res.send({ |
|
|
|
|
|
success: false, |
|
|
|
|
|
msg: "An error occured, please try again", |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
app.get("/movie/:id", verifyUser, async (req, res) => { |
|
|
app.get("/movie/:id", verifyUser, async (req, res) => { |
|
|
const fromDB = await Movie.findOne({ movieId: req.params.id }); |
|
|
try { |
|
|
|
|
|
const fromDB = await Movie.findOne({ movieId: req.params.id }); |
|
|
|
|
|
|
|
|
if (!fromDB) { |
|
|
if (!fromDB) { |
|
|
const result = await tmdb.get(`/movie/${req.params.id}`, { |
|
|
const result = ( |
|
|
api_key: process.env.TMDB_KEY, |
|
|
await tmdb.get(`/movie/${req.params.id}`, { |
|
|
}); |
|
|
params: { api_key: process.env.TMDB_KEY }, |
|
|
|
|
|
}) |
|
|
|
|
|
).data; |
|
|
|
|
|
|
|
|
const movie = { |
|
|
const movie = { |
|
|
poster: posterRoot + result.poster_path, |
|
|
poster: posterRoot + result.poster_path, |
|
|
overview: result.overview, |
|
|
overview: result.overview, |
|
|
release: result.release_date, |
|
|
release: result.release_date, |
|
|
movieId: req.params.id, |
|
|
movieId: req.params.id, |
|
|
title: result.title, |
|
|
title: result.title, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
res.send(movie); |
|
|
res.send(movie); |
|
|
|
|
|
|
|
|
await Movie.create(movie); |
|
|
await Movie.create(movie); |
|
|
} else { |
|
|
} else { |
|
|
const data = await Userdata.findOne({ |
|
|
const data = await Userdata.findOne({ |
|
|
userId: req["Container"].user._id, |
|
|
userId: req["Container"].user._id, |
|
|
movieId: req.params.id, |
|
|
movieId: req.params.id, |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
const movie = fromDB.toJSON(); |
|
|
const movie = fromDB.toJSON(); |
|
|
|
|
|
|
|
|
if (data) { |
|
|
if (data) { |
|
|
movie.watchStatus = data.watchStatus; |
|
|
movie.watchStatus = data.watchStatus; |
|
|
movie.rating = data.rating; |
|
|
movie.rating = data.rating; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
movie.poster = posterRoot + movie.poster; |
|
|
|
|
|
|
|
|
movie.poster = posterRoot + movie.poster; |
|
|
delete movie._id; |
|
|
|
|
|
delete movie.__v; |
|
|
|
|
|
|
|
|
delete movie._id; |
|
|
res.send(movie); |
|
|
delete movie.__v; |
|
|
} |
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
console.error("ERR on /movie", e.message); |
|
|
|
|
|
|
|
|
res.send(movie); |
|
|
res.statusCode = 500; |
|
|
|
|
|
res.send({ |
|
|
|
|
|
success: false, |
|
|
|
|
|
msg: "An error occured, please try again", |
|
|
|
|
|
}); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
app.post("/movie/:id", verifyUser, async (req, res) => { |
|
|
app.post("/movie/:id", verifyUser, async (req, res) => { |
|
|
const userId = req["Container"].user._id; |
|
|
try { |
|
|
const movieId = req.params.id; |
|
|
const userId = req["Container"].user._id; |
|
|
|
|
|
const movieId = req.params.id; |
|
|
|
|
|
|
|
|
const { watchStatus, rating } = req.body; |
|
|
const { watchStatus, rating } = req.body; |
|
|
|
|
|
|
|
|
if (!movieId) { |
|
|
if (!movieId) { |
|
|
res.statusCode = 400; |
|
|
res.statusCode = 400; |
|
|
return res.send({ success: false, msg: "movieId was not sent." }); |
|
|
return res.send({ |
|
|
} |
|
|
success: false, |
|
|
|
|
|
msg: "movieId was not sent.", |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (watchStatus && !WATCH_STATUS.includes(watchStatus)) { |
|
|
if (watchStatus && !WATCH_STATUS.includes(watchStatus)) { |
|
|
res.statusCode = 400; |
|
|
res.statusCode = 400; |
|
|
return res.send({ |
|
|
return res.send({ |
|
|
success: false, |
|
|
success: false, |
|
|
msg: "watchStatus must be one of " + WATCH_STATUS.join(", "), |
|
|
msg: |
|
|
}); |
|
|
"watchStatus must be one of " + WATCH_STATUS.join(", "), |
|
|
} |
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (rating && !RATINGS.includes(rating)) { |
|
|
if (rating && !RATINGS.includes(rating)) { |
|
|
res.statusCode = 400; |
|
|
res.statusCode = 400; |
|
|
return res.send({ |
|
|
return res.send({ |
|
|
|
|
|
success: false, |
|
|
|
|
|
msg: "rating must be one of " + RATINGS.join(", "), |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
await Userdata.updateOne( |
|
|
|
|
|
{ userId, movieId }, |
|
|
|
|
|
{ |
|
|
|
|
|
userId, |
|
|
|
|
|
movieId, |
|
|
|
|
|
watchStatus: req.body.watchStatus, |
|
|
|
|
|
rating: req.body.rating, |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
upsert: true, |
|
|
|
|
|
}, |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
res.send({ success: true }); |
|
|
|
|
|
} catch { |
|
|
|
|
|
res.statusCode = 500; |
|
|
|
|
|
res.send({ |
|
|
success: false, |
|
|
success: false, |
|
|
msg: "rating must be one of " + RATINGS.join(", "), |
|
|
msg: "An error occured, please try again", |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
await Userdata.updateOne( |
|
|
|
|
|
{ userId, movieId }, |
|
|
|
|
|
{ |
|
|
|
|
|
userId, |
|
|
|
|
|
movieId, |
|
|
|
|
|
watchStatus: req.body.watchStatus, |
|
|
|
|
|
rating: req.body.rating, |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
upsert: true, |
|
|
|
|
|
}, |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
res.send({ success: true }); |
|
|
|
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
app.listen(process.env.PORT, () => |
|
|
app.listen(process.env.PORT, () => |
|
|