Commit 75723912 authored by Jiwon Yoon's avatar Jiwon Yoon
Browse files

Merge branch 'jiwon'

parents d2351773 b606631f
...@@ -3,67 +3,67 @@ import sequelize from 'sequelize'; ...@@ -3,67 +3,67 @@ import sequelize from 'sequelize';
const { Op } = sequelize const { Op } = sequelize
import { Movie } from '../db/index.js' import { Movie } from '../db/index.js'
const comparePopularMovie = async (req, res) => { const getMovieByCategory = async (req, res, next, category) => {
const responsePopular = await axios.get(`https://api.themoviedb.org/3/movie/popular?api_key=${process.env.TMDB_APP_KEY}&language=ko-KR`) const responsePopular = await axios.get(`https://api.themoviedb.org/3/movie/${category}?api_key=${process.env.TMDB_APP_KEY}&language=ko-KR&page=1`)
const movies = responsePopular.data.results const TMDBmovies = responsePopular.data.results
const movieIds = [] const TMDBmovieIds = []
movies.forEach(element => { TMDBmovies.forEach(element => {
movieIds.push(element.id) TMDBmovieIds.push(element.id)
}); });
console.log(movieIds) console.log(TMDBmovieIds)
try { try {
const responseAfterCompare = await Movie.findAll({ const responseAfterCompare = await Movie.findAll({
where: { where: {
movieId: { movieId: {
[Op.or]: movieIds [Op.or]: TMDBmovieIds
} }
} }
}) })
const popularMovieIds = [] const movieIds = []
responseAfterCompare.forEach(el => { responseAfterCompare.forEach(el => {
popularMovieIds.push(el.movieId) movieIds.push(el.movieId)
}) })
console.log('popularMovieIds=', popularMovieIds) console.log('movieIds=', movieIds)
const m = await Promise.all( req.movieIds = movieIds
popularMovieIds.map(async(el) => { next()
const movie = await axios.get(`https://api.themoviedb.org/3/movie/${el}?api_key=${process.env.TMDB_APP_KEY}&language=ko-KR`) } catch (error) {
console.log(error)
}
}
const getMovieById = async (req, res) => {
try {
const movieIds = req.movieIds
console.log(movieIds)
const elements = await Promise.all(
movieIds.map(async (movieId) => {
const movie = await axios.get(`https://api.themoviedb.org/3/movie/${movieId}?api_key=${process.env.TMDB_APP_KEY}&language=ko-KR`)
return movie.data return movie.data
}) })
) )
res.json(m) console.log(elements)
res.json(elements)
} catch (error) { } catch (error) {
console.log(error)
} }
} }
const upcommingMovie = async (req, res) => { const getMovieList = async(req,res)=>{
const responsePopular = await axios.get(`https://api.themoviedb.org/3/movie/upcoming?api_key=${process.env.TMDB_APP_KEY}&language=ko-KR&page=1`)
const movies = responsePopular.data.results
const movieIds = []
movies.forEach(element => {
movieIds.push(element.id)
});
console.log(movieIds)
try { try {
const responseAfterCompare = await Movie.findAll({ const movieList = await Movie.findAll()
where: { // console.log(movieList)
movieId: { const movieIds=[]
[Op.or]: movieIds movieList.forEach(el => {
} movieIds.push(el.movieId)
}
})
const popularMovieIds = []
responseAfterCompare.forEach(el => {
popularMovieIds.push(el.movieId)
}) })
console.log('popularMovieIds=', popularMovieIds) const elements = await Promise.all(
const m = await Promise.all( movieIds.map(async (movieId) => {
popularMovieIds.map(async(el) => { const movie = await axios.get(`https://api.themoviedb.org/3/movie/${movieId}?api_key=${process.env.TMDB_APP_KEY}&language=ko-KR`)
const movie = await axios.get(`https://api.themoviedb.org/3/movie/${el}?api_key=${process.env.TMDB_APP_KEY}&language=ko-KR`)
return movie.data return movie.data
}) })
) )
res.json(m) console.log(elements)
res.json(elements)
} catch (error) { } catch (error) {
console.log(error) console.log(error)
} }
...@@ -73,13 +73,15 @@ const create = async (req, res) => { ...@@ -73,13 +73,15 @@ const create = async (req, res) => {
try { try {
const { movieId } = req.params const { movieId } = req.params
const newMovie = await Movie.create({ movieId: movieId }); const newMovie = await Movie.create({ movieId: movieId });
return res.json(newMovie); res.json(newMovie);
} catch (error) { } catch (error) {
return res.status(500).send(error.message || "영화 등록 중 에러 발생"); res.status(500).send(error.message || "영화 등록 중 에러 발생");
} }
} }
export default { export default {
comparePopularMovie, getMovieByCategory,
getMovieById,
getMovieList,
create, create,
} }
...@@ -5,11 +5,17 @@ const router = express.Router(); ...@@ -5,11 +5,17 @@ const router = express.Router();
router router
.route("/") .route("/")
.post(movieCtrl.comparePopularMovie) // .post(movieCtrl.comparePopularMovie)
.get(movieCtrl.comparePopularMovie)
router.route('/showmovies/:category')
.get(movieCtrl.getMovieById)
router.route('/movielist')
.get(movieCtrl.getMovieList)
router router
.route("/:movieId") .route("/:movieId")
.post(movieCtrl.create) .post(movieCtrl.create)
router.param('category', movieCtrl.getMovieByCategory)
export default router; export default router;
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment