movie.controller.js 4.63 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
const comparePopularMovie = async (req, res) => {
Kim, Subin's avatar
Kim, Subin committed
7
    const response = await axios.get(`https://api.themoviedb.org/3/movie/popular?api_key=${process.env.TMDB_APP_KEY}&language=ko-KR`)
Jiwon Yoon's avatar
Jiwon Yoon committed
8
9
10
11
12
    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
const getMovieByCategory = async (req, res, next, category) => {
    try {
Kim, Subin's avatar
Kim, Subin committed
19
20
21
22
23
24
25
        const TMDBmovieIds = []
        const movieIds = []
        const response = await axios.get(`https://api.themoviedb.org/3/movie/${category}?api_key=${process.env.TMDB_APP_KEY}&language=ko-KR&page=1`)
        const TMDBmovies = response.data.results
        TMDBmovies.forEach(element => {
            TMDBmovieIds.push(element.id)
        })
Kim, Subin's avatar
Kim, Subin committed
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
        const responseAfterCompare = await Movie.findAll({
            where: {
                movieId: {
                    [Op.or]: TMDBmovieIds
                }
            }
        })
        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
            })
Kim, Subin's avatar
Kim, Subin committed
53
        )
Kim, Subin's avatar
Kim, Subin committed
54
55
56
57
        console.log(elements)
        res.json(elements)
    } catch (error) {
        return res.status(500).send(error.message || "영화 가져오기 중 에러 발생");
Jiwon Yoon's avatar
Jiwon Yoon committed
58
59
60
    }
}

Kim, Subin's avatar
Kim, Subin committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const getAllMovie = async (req, res) => {
    try {
        const { pageNum } = req.query
        const TMDBmovieIds = []
        const now = new Date()
        const monthAgo = new Date(now.setMonth(now.getMonth() - 1)).toJSON().split(/T/)[0]
        const response = await axios.get(`https://api.themoviedb.org/3/discover/movie?api_key=${process.env.TMDB_APP_KEY}&language=ko-KR&region=KR&sort_by=release_date.asc&release_date.gte=${monthAgo}&page=${pageNum}`)
        const TMDBmovies = response.data.results
        TMDBmovies.forEach(element => {
            TMDBmovieIds.push(element.id)
        })
        const responseAfterCompare = await Movie.findAll({
            where: {
                movieId: TMDBmovieIds
            },
            attributes: ['movieId']
        })
        responseAfterCompare.forEach(element => TMDBmovies.find((movie) => {
            if (movie.existed !== true && movie.id === element.movieId) {
                movie.existed = true
            } else if (movie.existed !== true) movie.existed = false
        }))
        return res.json(TMDBmovies)
    } catch (error) {
        return res.status(500).send(error.message || "영화 가져오는 중 에러 발생")
    }
}

89
90
91
const create = async (req, res) => {
    try {
        const { movieId } = req.params
Kim, Subin's avatar
Kim, Subin committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
        const { data } = await axios.get(`https://api.themoviedb.org/3/movie/${movieId}?api_key=${process.env.TMDB_APP_KEY}&language=ko-KR`)
        const newMovie = await Movie.create({ movieId: data.id, title: data.title, release_date: data.release_date })
        return res.json(newMovie)
    } catch (error) {
        return res.status(500).send(error.message || "영화 등록 중 에러 발생")
    }
}

const remove = async (req, res) => {
    try {
        const { movieId } = req.params
        const delMovie = await Movie.destroy({ where: { movieId: movieId } })
        return res.json(delMovie)
    } catch (error) {
        return res.status(500).send(error.message || "영화 삭제 중 에러 발생");
    }
}

const findforKeyword = async (req, res) => {
    try {
        console.log("req==", req.query)
        const { title } = req.query
        const { count, rows } = await Movie.findAndCountAll({
            where: {
                title: {
                    [Op.substring]: title
                }
            }
        });
        console.log("finds==", rows)
        return res.json({ count, rows })
123
    } catch (error) {
Kim, Subin's avatar
Kim, Subin committed
124
        return res.status(500).send(error.message || "영화 검색 중 에러 발생");
125
126
127
128
129
    }
}

export default {
    comparePopularMovie,
Kim, Subin's avatar
Kim, Subin committed
130
131
    getMovieByCategory,
    getMovieById,
Kim, Subin's avatar
Kim, Subin committed
132
    getAllMovie,
133
    create,
Kim, Subin's avatar
Kim, Subin committed
134
135
    remove,
    findforKeyword
136
}