movie.controller.js 7.25 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
        console.log(category)
Kim, Subin's avatar
Kim, Subin committed
9
10
        const TMDBmovieIds = []
        const movieIds = []
Jiwon Yoon's avatar
Jiwon Yoon committed
11
        console.log(process.env.TMDB_APP_KEY)
Kim, Subin's avatar
Kim, Subin committed
12
        const response = await axios.get(`https://api.themoviedb.org/3/movie/${category}?api_key=${process.env.TMDB_APP_KEY}&language=ko-KR&page=1`)
Jiwon Yoon's avatar
Jiwon Yoon committed
13
        console.log(response.data)
Kim, Subin's avatar
Kim, Subin committed
14
        const TMDBmovies = response.data.results
Jiwon Yoon's avatar
Jiwon Yoon committed
15

Kim, Subin's avatar
Kim, Subin committed
16
17
18
        TMDBmovies.forEach(element => {
            TMDBmovieIds.push(element.id)
        })
Jiwon Yoon's avatar
Jiwon Yoon committed
19
        console.log(TMDBmovies)
Kim, Subin's avatar
Kim, Subin committed
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
        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
47
        )
Kim, Subin's avatar
Kim, Subin committed
48
49
50
51
        console.log(elements)
        res.json(elements)
    } catch (error) {
        return res.status(500).send(error.message || "영화 가져오기 중 에러 발생");
Jiwon Yoon's avatar
Jiwon Yoon committed
52
53
54
    }
}

55
const movieforAdmin = async (req, res) => {
Kim, Subin's avatar
Kim, Subin committed
56
57
    try {
        const TMDBmovieIds = []
58
        const TMDBmovies = req.TMDBmovies
Kim, Subin's avatar
Kim, Subin committed
59
60
61
        TMDBmovies.forEach(element => {
            TMDBmovieIds.push(element.id)
        })
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
        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
78
79
80
81
82
83
        const responseAfterCompare = await Movie.findAll({
            where: {
                movieId: TMDBmovieIds
            },
            attributes: ['movieId']
        })
84
85
86
        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
87
88
89
90
91
92
        }))
        return res.json(TMDBmovies)
    } catch (error) {
        return res.status(500).send(error.message || "영화 가져오는 중 에러 발생")
    }
}
Jiwon Yoon's avatar
Jiwon Yoon committed
93

94
95
96
97
98
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]
Jiwon Yoon's avatar
Jiwon Yoon committed
99
100
        // 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 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=6`)
101
102
103
104
        req.TMDBmovies = response.data.results
        next()
    } catch (error) {
        return res.status(500).send(error.message || "영화 가져오는 중 에러 발생")
Jiwon Yoon's avatar
Jiwon Yoon committed
105
106
107
    }
}

Jiwon Yoon's avatar
Jiwon Yoon committed
108
const getMovieList = async(req,res)=>{
Jiwon Yoon's avatar
Jiwon Yoon committed
109
    try {
Jiwon Yoon's avatar
Jiwon Yoon committed
110
111
112
113
114
        const movieList = await Movie.findAll()
        // console.log(movieList)
        const movieIds=[]
        movieList.forEach(el => {
            movieIds.push(el.movieId)
Jiwon Yoon's avatar
Jiwon Yoon committed
115
        })
Jiwon Yoon's avatar
Jiwon Yoon committed
116
117
118
        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
119
120
                return movie.data
            })
Jiwon Yoon's avatar
Jiwon Yoon committed
121
122
123
        )  
        console.log(elements)
        res.json(elements)
Jiwon Yoon's avatar
Jiwon Yoon committed
124
125
126
127
128
    } catch (error) {
        console.log(error)
    }
}

129
130
131
const create = async (req, res) => {
    try {
        const { movieId } = req.params
Kim, Subin's avatar
Kim, Subin committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
        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 || "영화 삭제 중 에러 발생");
    }
}

150
const findonlyTitle = async (req, res) => {
Kim, Subin's avatar
Kim, Subin committed
151
    try {
152
        const { keyword } = req.query
Kim, Subin's avatar
Kim, Subin committed
153
        const movieIds = []
Kim, Subin's avatar
Kim, Subin committed
154
155
156
        const { count, rows } = await Movie.findAndCountAll({
            where: {
                title: {
157
                    [Op.substring]: keyword
Kim, Subin's avatar
Kim, Subin committed
158
159
160
                }
            }
        });
Kim, Subin's avatar
Kim, Subin committed
161
162
163
164
165
166
167
168
169
170
        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 })
171
    } catch (error) {
Kim, Subin's avatar
Kim, Subin committed
172
        return res.status(500).send(error.message || "영화 검색 중 에러 발생");
173
174
175
    }
}

176
177
178
179
180
181
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()
182
    } catch (error) {
183
        return res.status(500).send(error.message || "영화 검색 중 에러 발생");
184
185
186
187
    }
}

export default {
Jiwon Yoon's avatar
Jiwon Yoon committed
188
189
    getMovieByCategory,
    getMovieById,
Kim, Subin's avatar
Kim, Subin committed
190
    getAllMovie,
Jiwon Yoon's avatar
Jiwon Yoon committed
191
    getMovieList,
192
    create,
Kim, Subin's avatar
Kim, Subin committed
193
    remove,
194
195
196
    findonlyTitle,
    findaboutAll,
    movieforAdmin
Jiwon Yoon's avatar
Jiwon Yoon committed
197
}