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

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

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

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

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