movie.controller.js 2.36 KB
Newer Older
Jiwon Yoon's avatar
Jiwon Yoon committed
1
import axios from 'axios'
Jiwon Yoon's avatar
Jiwon Yoon committed
2
3
4
import sequelize from 'sequelize';
const { Op } = sequelize
import { Movie } from '../db/index.js'
Jiwon Yoon's avatar
Jiwon Yoon committed
5

Jiwon Yoon's avatar
Jiwon Yoon committed
6
const comparePopularMovie = async (req, res) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
7
8
9
10
11
12
13
    const responsePopular = await axios.get(`https://api.themoviedb.org/3/movie/popular?api_key=${process.env.TMDB_APP_KEY}&language=ko-KR`)
    const movies = responsePopular.data.results
    const movieIds = []
    movies.forEach(element => {
        movieIds.push(element.id)
    });
    console.log(movieIds)
Jiwon Yoon's avatar
Jiwon Yoon committed
14
    try {
Jiwon Yoon's avatar
Jiwon Yoon committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
        const responseAfterCompare = await Movie.findAll({
            where: {
                movieId: {
                    [Op.or]: movieIds
                }
            }
        })
        const popularMovieIds = []
        responseAfterCompare.forEach(el => {
            popularMovieIds.push(el.movieId)
        })
        console.log('popularMovieIds=', popularMovieIds)
        const m = await Promise.all(
            popularMovieIds.map(async(el) => {
                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
            })
        )
        res.json(m)
Jiwon Yoon's avatar
Jiwon Yoon committed
34
    } catch (error) {
Jiwon Yoon's avatar
Jiwon Yoon committed
35
        console.log(error)
Jiwon Yoon's avatar
Jiwon Yoon committed
36
37
38
    }
}

Jiwon Yoon's avatar
Jiwon Yoon committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const upcommingMovie = 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 {
        const responseAfterCompare = await Movie.findAll({
            where: {
                movieId: {
                    [Op.or]: movieIds
                }
            }
        })
        const popularMovieIds = []
        responseAfterCompare.forEach(el => {
            popularMovieIds.push(el.movieId)
        })
        console.log('popularMovieIds=', popularMovieIds)
        const m = await Promise.all(
            popularMovieIds.map(async(el) => {
                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
            })
        )
        res.json(m)
    } catch (error) {
        console.log(error)
    }
}

export default { comparePopularMovie, upcommingMovie }