movie.controller.js 2.55 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 getMovieByCategory = async (req, res, next, category) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
7
    try {
Jiwon Yoon's avatar
Jiwon Yoon committed
8
9
10
11
12
13
14
15
        const responsePopular = await axios.get(`https://api.themoviedb.org/3/movie/${category}?api_key=${process.env.TMDB_APP_KEY}&language=ko-KR`)
        const TMDBmovies = responsePopular.data.results
        console.log(TMDBmovies)
        const TMDBmovieIds = []
        TMDBmovies.forEach(element => {
            TMDBmovieIds.push(element.id)
        });
        // console.log(TMDBmovieIds)
Jiwon Yoon's avatar
Jiwon Yoon committed
16
17
18
        const responseAfterCompare = await Movie.findAll({
            where: {
                movieId: {
Jiwon Yoon's avatar
Jiwon Yoon committed
19
                    [Op.or]: TMDBmovieIds
Jiwon Yoon's avatar
Jiwon Yoon committed
20
21
22
                }
            }
        })
Jiwon Yoon's avatar
Jiwon Yoon committed
23
        const movieIds = []
Jiwon Yoon's avatar
Jiwon Yoon committed
24
        responseAfterCompare.forEach(el => {
Jiwon Yoon's avatar
Jiwon Yoon committed
25
            movieIds.push(el.movieId)
Jiwon Yoon's avatar
Jiwon Yoon committed
26
        })
Jiwon Yoon's avatar
Jiwon Yoon committed
27
        // console.log('movieIds=', movieIds)
Jiwon Yoon's avatar
Jiwon Yoon committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
        req.movieIds = movieIds
        next()
    } 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`)
Jiwon Yoon's avatar
Jiwon Yoon committed
42
43
                return movie.data
            })
Jiwon Yoon's avatar
Jiwon Yoon committed
44
45
46
        )  
        console.log(elements)
        res.json(elements)
Jiwon Yoon's avatar
Jiwon Yoon committed
47
    } catch (error) {
Jiwon Yoon's avatar
Jiwon Yoon committed
48

Jiwon Yoon's avatar
Jiwon Yoon committed
49
50
51
    }
}

Jiwon Yoon's avatar
Jiwon Yoon committed
52
const getMovieList = async(req,res)=>{
Jiwon Yoon's avatar
Jiwon Yoon committed
53
    try {
Jiwon Yoon's avatar
Jiwon Yoon committed
54
55
56
57
58
        const movieList = await Movie.findAll()
        // console.log(movieList)
        const movieIds=[]
        movieList.forEach(el => {
            movieIds.push(el.movieId)
Jiwon Yoon's avatar
Jiwon Yoon committed
59
        })
Jiwon Yoon's avatar
Jiwon Yoon committed
60
61
62
        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`)
Jiwon Yoon's avatar
Jiwon Yoon committed
63
64
                return movie.data
            })
Jiwon Yoon's avatar
Jiwon Yoon committed
65
66
67
        )  
        console.log(elements)
        res.json(elements)
Jiwon Yoon's avatar
Jiwon Yoon committed
68
69
70
71
72
    } catch (error) {
        console.log(error)
    }
}

73
74
75
76
const create = async (req, res) => {
    try {
        const { movieId } = req.params
        const newMovie = await Movie.create({ movieId: movieId });
Jiwon Yoon's avatar
Jiwon Yoon committed
77
        res.json(newMovie);
78
    } catch (error) {
Jiwon Yoon's avatar
Jiwon Yoon committed
79
        res.status(500).send(error.message || "영화 등록 중 에러 발생");
80
81
82
83
    }
}

export default {
Jiwon Yoon's avatar
Jiwon Yoon committed
84
85
86
    getMovieByCategory,
    getMovieById,
    getMovieList,
87
    create,
Jiwon Yoon's avatar
Jiwon Yoon committed
88
}