Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
students
butter-studio
Commits
15dff40b
Commit
15dff40b
authored
Jul 21, 2021
by
Kim, Subin
Browse files
findonlyTitle, findaboutAll, movieforAdmin
parent
1f3acca1
Changes
1
Show whitespace changes
Inline
Side-by-side
server/controllers/movie.controller.js
View file @
15dff40b
...
...
@@ -58,27 +58,38 @@ const getMovieById = async (req, res) => {
}
}
const
getAllMovie
=
async
(
req
,
res
)
=>
{
const
movieforAdmin
=
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®ion=KR&sort_by=release_date.asc&release_date.gte=
${
monthAgo
}
&page=
${
pageNum
}
`
)
const
TMDBmovies
=
response
.
data
.
results
const
TMDBmovies
=
req
.
TMDBmovies
TMDBmovies
.
forEach
(
element
=>
{
TMDBmovieIds
.
push
(
element
.
id
)
})
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
}))
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
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
}))
return
res
.
json
(
TMDBmovies
)
}
catch
(
error
)
{
...
...
@@ -86,6 +97,19 @@ const getAllMovie = async (req, res) => {
}
}
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®ion=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
||
"
영화 가져오는 중 에러 발생
"
)
}
}
const
create
=
async
(
req
,
res
)
=>
{
try
{
const
{
movieId
}
=
req
.
params
...
...
@@ -107,14 +131,14 @@ const remove = async (req, res) => {
}
}
const
find
forKeyword
=
async
(
req
,
res
)
=>
{
const
find
onlyTitle
=
async
(
req
,
res
)
=>
{
try
{
const
{
title
}
=
req
.
query
const
{
keyword
}
=
req
.
query
const
movieIds
=
[]
const
{
count
,
rows
}
=
await
Movie
.
findAndCountAll
({
where
:
{
title
:
{
[
Op
.
substring
]:
title
[
Op
.
substring
]:
keyword
}
}
});
...
...
@@ -133,6 +157,17 @@ const findforKeyword = async (req, res) => {
}
}
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
)}
®ion=KR`
)
req
.
TMDBmovies
=
response
.
data
.
results
next
()
}
catch
(
error
)
{
return
res
.
status
(
500
).
send
(
error
.
message
||
"
영화 검색 중 에러 발생
"
);
}
}
export
default
{
comparePopularMovie
,
getMovieByCategory
,
...
...
@@ -140,5 +175,7 @@ export default {
getAllMovie
,
create
,
remove
,
findforKeyword
findonlyTitle
,
findaboutAll
,
movieforAdmin
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment