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

Jiwon Yoon's avatar
Jiwon Yoon committed
6
7
8
9
10
11
12
const comparePopularMovie = async (req, res) => {
    const response = await axios.get('https://api.themoviedb.org/3/movie/popular?api_key=1477348488076cafd4dcf973a314957d&language=ko-KR')
    const movies = response.data
    console.log('movies', movies)
    try {

    } catch (error) {
Kim, Subin's avatar
Kim, Subin committed
13
14
15
        return res.status(500).send(error.message || "영화 가져오기 중 에러 발생");
    }
}
Jiwon Yoon's avatar
Jiwon Yoon committed
16

Kim, Subin's avatar
Kim, Subin committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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)
    });
    console.log(TMDBmovieIds)
    try {
        const responseAfterCompare = await Movie.findAll({
            where: {
                movieId: {
                    [Op.or]: TMDBmovieIds
                }
            }
        })
        const movieIds = []
        responseAfterCompare.forEach(el => {
            movieIds.push(el.movieId)
        })
        console.log('movieIds=', movieIds)
        req.movieIds = movieIds
        next()
    } catch (error) {
        return res.status(500).send(error.message || "영화 가져오기 중 에러 발생");
    }
}

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
            })
        )  
        console.log(elements)
        res.json(elements)
    } catch (error) {
        return res.status(500).send(error.message || "영화 가져오기 중 에러 발생");
Jiwon Yoon's avatar
Jiwon Yoon committed
59
60
61
    }
}

62
63
64
65
66
67
68
69
70
71
72
73
const create = async (req, res) => {
    try {
        const { movieId } = req.params
        const newMovie = await Movie.create({ movieId: movieId });
        return res.json(newMovie);
    } catch (error) {
        return res.status(500).send(error.message || "영화 등록 중 에러 발생");
    }
}

export default {
    comparePopularMovie,
Kim, Subin's avatar
Kim, Subin committed
74
75
    getMovieByCategory,
    getMovieById,
76
77
    create,
}