movie.controller.js 8.41 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) {
Kim, Subin's avatar
Kim, Subin committed
48
49
50
        return res.status(500).send(error.message || "영화 가져오기 중 에러 발생");
    }
}
Jiwon Yoon's avatar
Jiwon Yoon committed
51

Kim, Subin's avatar
Kim, Subin committed
52
53
const getMovieByCategory = async (req, res, next, category) => {
    try {
Kim, Subin's avatar
Kim, Subin committed
54
55
56
57
58
59
60
        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
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
        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
88
        )
Kim, Subin's avatar
Kim, Subin committed
89
90
91
92
        console.log(elements)
        res.json(elements)
    } catch (error) {
        return res.status(500).send(error.message || "영화 가져오기 중 에러 발생");
Jiwon Yoon's avatar
Jiwon Yoon committed
93
94
95
    }
}

96
const movieforAdmin = async (req, res) => {
Kim, Subin's avatar
Kim, Subin committed
97
98
    try {
        const TMDBmovieIds = []
99
        const TMDBmovies = req.TMDBmovies
Kim, Subin's avatar
Kim, Subin committed
100
101
102
        TMDBmovies.forEach(element => {
            TMDBmovieIds.push(element.id)
        })
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
        const findDirectorResult = await Promise.all(TMDBmovieIds.map(async (movieId) => {
            let newObj = { id: movieId, name: "" }
            const { data } = await axios.get(`https://api.themoviedb.org/3/movie/${movieId}/credits?api_key=${process.env.TMDB_APP_KEY}&language=ko-KR`)
            const findDirectors = await data.crew.filter(element => element.job === "Director")
            if (findDirectors.length !== 0) {
                const name = findDirectors.reduce((acc, cur, idx) => {
                    if (idx !== 0) return acc + ', ' + cur.name
                    else return acc + cur.name}, '')
                newObj.name = name
            } else newObj.name = "없음"

            return newObj
        }))
        findDirectorResult.forEach(element => TMDBmovies.forEach(movie => { 
            if (element.id === movie.id) movie.director = element.name
        }))
Kim, Subin's avatar
Kim, Subin committed
119
120
121
122
123
124
        const responseAfterCompare = await Movie.findAll({
            where: {
                movieId: TMDBmovieIds
            },
            attributes: ['movieId']
        })
125
126
127
        responseAfterCompare.forEach(element => TMDBmovies.forEach((movie) => {
            if (movie.existed !== true && movie.id === element.movieId) movie.existed = true
            else if (movie.existed !== true) movie.existed = false
Kim, Subin's avatar
Kim, Subin committed
128
129
130
131
132
133
        }))
        return res.json(TMDBmovies)
    } catch (error) {
        return res.status(500).send(error.message || "영화 가져오는 중 에러 발생")
    }
}
Jiwon Yoon's avatar
Jiwon Yoon committed
134

135
136
137
138
139
140
141
142
143
144
const getAllMovie = async (req, res, next) => {
    try {
        const { pageNum } = req.query
        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}`)
        req.TMDBmovies = response.data.results
        next()
    } catch (error) {
        return res.status(500).send(error.message || "영화 가져오는 중 에러 발생")
Jiwon Yoon's avatar
Jiwon Yoon committed
145
146
147
    }
}

Jiwon Yoon's avatar
Jiwon Yoon committed
148
const getMovieList = async(req,res)=>{
Jiwon Yoon's avatar
Jiwon Yoon committed
149
    try {
Jiwon Yoon's avatar
Jiwon Yoon committed
150
151
152
153
154
        const movieList = await Movie.findAll()
        // console.log(movieList)
        const movieIds=[]
        movieList.forEach(el => {
            movieIds.push(el.movieId)
Jiwon Yoon's avatar
Jiwon Yoon committed
155
        })
Jiwon Yoon's avatar
Jiwon Yoon committed
156
157
158
        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
159
160
                return movie.data
            })
Jiwon Yoon's avatar
Jiwon Yoon committed
161
162
163
        )  
        console.log(elements)
        res.json(elements)
Jiwon Yoon's avatar
Jiwon Yoon committed
164
165
166
167
168
    } catch (error) {
        console.log(error)
    }
}

169
170
171
const create = async (req, res) => {
    try {
        const { movieId } = req.params
Kim, Subin's avatar
Kim, Subin committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
        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 || "영화 삭제 중 에러 발생");
    }
}

190
const findonlyTitle = async (req, res) => {
Kim, Subin's avatar
Kim, Subin committed
191
    try {
192
        const { keyword } = req.query
Kim, Subin's avatar
Kim, Subin committed
193
        const movieIds = []
Kim, Subin's avatar
Kim, Subin committed
194
195
196
        const { count, rows } = await Movie.findAndCountAll({
            where: {
                title: {
197
                    [Op.substring]: keyword
Kim, Subin's avatar
Kim, Subin committed
198
199
200
                }
            }
        });
Kim, Subin's avatar
Kim, Subin committed
201
202
203
204
205
206
207
208
209
210
        if (rows) {
            rows.forEach(movie => movieIds.push(movie.movieId))
            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
                })
            )
            return res.json({ count: movieIds.length, results: elements })
        } else return res.json({ count: count, results: rows })
211
    } catch (error) {
Kim, Subin's avatar
Kim, Subin committed
212
        return res.status(500).send(error.message || "영화 검색 중 에러 발생");
213
214
215
    }
}

216
217
218
219
220
221
const findaboutAll = async (req, res, next) => {
    try {
        const { keyword } = req.query
        const response = await axios.get(`https://api.themoviedb.org/3/search/movie?api_key=${process.env.TMDB_APP_KEY}&language=kr-KR&query=${encodeURI(keyword)}&region=KR`)
        req.TMDBmovies = response.data.results
        next()
222
    } catch (error) {
223
        return res.status(500).send(error.message || "영화 검색 중 에러 발생");
224
225
226
227
    }
}

export default {
Jiwon Yoon's avatar
Jiwon Yoon committed
228
229
    getMovieByCategory,
    getMovieById,
Kim, Subin's avatar
Kim, Subin committed
230
    getAllMovie,
Jiwon Yoon's avatar
Jiwon Yoon committed
231
    getMovieList,
232
    create,
Kim, Subin's avatar
Kim, Subin committed
233
    remove,
234
235
236
    findonlyTitle,
    findaboutAll,
    movieforAdmin
Jiwon Yoon's avatar
Jiwon Yoon committed
237
}