Commit ad4f6a49 authored by Kim, Subin's avatar Kim, Subin
Browse files

movie 서버 등록 및 TMDB upcoming data 가져오기

parent 13fc7bb7
import axios from "axios";
import { baseUrl, TMDBUrl } from "../utils/baseUrl";
const getUpcomingfromTM = async () => {
const { data } = await axios.get(`${TMDBUrl}/upcoming?api_key=${process.env.REACT_APP_TMDB_API_KEY}&language=ko-KR`)
return data.results
}
const submit = async (movieId) => {
const { data } = await axios.post(`${baseUrl}/api/movie/${movieId}`)
console.log("data==",data)
}
const movieApi = {
getUpcomingfromTM,
submit
}
export default movieApi
\ No newline at end of file
import { useState, useEffect } from "react";
import Search from "../Search"; import Search from "../Search";
import MovieTable from "../MovieTable"; import MovieTable from "../MovieTable";
import Pagination from "../Pagination"; import Pagination from "../Pagination";
import movieApi from "../../apis/movie.api.js";
import catchErrors from "../../utils/catchErrors.js";
import styles from "./admin.module.scss"; import styles from "./admin.module.scss";
const MovieEdit = () => { const MovieEdit = () => {
const [movieList, setMovieList] = useState([])
const [error, setError] = useState("")
useEffect(() => {
getMovieList()
}, [])
async function getMovieList() {
try {
setError("");
const getMovieList = await movieApi.getUpcomingfromTM()
setMovieList(getMovieList)
} catch (error) {
catchErrors(error, setError)
}
}
return ( return (
<> <>
{console.log("List==",movieList)}
<div className="d-flex justify-content-end mb-3"> <div className="d-flex justify-content-end mb-3">
<Search type="admin" /> <Search type="admin" />
</div> </div>
<MovieTable /> <MovieTable movieList={movieList} />
<div className="d-flex flex-wrap"> <div className="d-flex flex-wrap">
<Pagination /> <Pagination />
<div className="d-flex justify-content-end col-12 col-md-4 my-2"> <div className="d-flex justify-content-end col-12 col-md-4 my-2">
......
import { useState } from "react";
import movieApi from "../../apis/movie.api.js";
import catchErrors from "../../utils/catchErrors.js";
import styles from "./movie-table.module.scss"; import styles from "./movie-table.module.scss";
const MovieTable = () => { const MovieTable = ({ movieList }) => {
const [error, setError] = useState("")
async function handleClick(e, movieId) {
e.preventDefault();
try {
setError("");
await movieApi.submit(movieId)
alert("서버 등록이 완료되었습니다.")
} catch (error) {
catchErrors(error, setError);
}
}
return ( return (
<table className={`table text-center ${styles.tableForm}`}> <table className={`table text-center ${styles.tableForm}`}>
<thead className={`table-dark ${styles.dNone}`}> <thead className={`table-dark ${styles.dNone}`}>
...@@ -15,28 +31,32 @@ const MovieTable = () => { ...@@ -15,28 +31,32 @@ const MovieTable = () => {
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr className={styles.Row} data-bs-toggle="collapse" data-bs-target={"#movie1"}> {movieList?.map(movie =>
<td className="d-inline-block d-md-table-cell">블랙위도우</td> <>
<td data-label="- " className={`d-inline-block d-md-table-cell ${styles.data}`}>케이트 쇼트랜드</td> <tr className={styles.Row} data-bs-toggle="collapse" data-bs-target={"#movie" + movie.id}>
<td data-label="/ " className={`d-inline-block d-md-table-cell ${styles.data}`}>2021-07-07</td> <td className="d-inline-block d-md-table-cell">{movie.title}</td>
<td className="d-none d-md-table-cell">O</td> <td data-label="- " className={`d-inline-block d-md-table-cell ${styles.data}`}>케이트 쇼트랜드</td>
<td className="d-none d-md-table-cell">O</td> <td data-label="/ " className={`d-inline-block d-md-table-cell ${styles.data}`}>{movie.release_date}</td>
<td className="d-none d-md-table-cell">X</td> <td className="d-none d-md-table-cell">{movie.overview !== '' ? 'O' : 'X'}</td>
<td className="d-none d-md-table-cell">X</td> <td className="d-none d-md-table-cell">{movie.poster_path !== '' ? 'O' : 'X'}</td>
</tr> <td className="d-none d-md-table-cell">{movie.backdrop_path !== '' ? 'O' : 'X'}</td>
<tr className={styles.Row}> <td className="d-none d-md-table-cell">{movie.video !== false ? 'O' : 'X'}</td>
<td colSpan="7" className="collapse" id={"movie1"}> </tr>
<div className={`d-inline-block d-md-none ${styles.word} mb-2`}> <tr className={styles.Row}>
줄거리 - O / <td colSpan="7" className="collapse" id={"movie" + movie.id}>
포스터 - O / <div className={`d-inline-block d-md-none ${styles.word} mb-2`}>
스틸컷 - X / 줄거리 - {movie.overview !== '' ? 'O' : 'X'} /
예고편 - X 포스터 - {movie.poster_path !== '' ? 'O' : 'X'} /
</div> 스틸컷 - {movie.backdrop_path !== '' ? 'O' : 'X'} /
<div className="d-flex justify-content-end"> 예고편 - {movie.video !== false ? 'O' : 'X'}
<button type="button" className="btn btn-danger">삭제</button> </div>
</div> <div className="d-flex justify-content-end">
</td> <button type="button" className="btn btn-primary" onClick={(e) => handleClick(e, movie.id)}>등록</button>
</tr> {/* <button type="button" className="btn btn-danger">삭제</button> */}
</div>
</td>
</tr>
</>)}
</tbody> </tbody>
</table> </table>
) )
......
const catchErrors = (error, displayError) => {
let errorMsg
if (error.response) {
errorMsg = error.response.data
console.log('Error response:', errorMsg)
} else if (error.request) {
errorMsg = error.request
console.log('Error request:', errorMsg)
} else {
errorMsg = error.message
console.log('Error message:', errorMsg)
}
displayError(errorMsg)
}
export default catchErrors
\ No newline at end of file
import axios from 'axios' import axios from 'axios'
import { Movie } from "../db/index.js";
const comparePopularMovie = async (req, res) => { const comparePopularMovie = async (req, res) => {
const response = await axios.get('https://api.themoviedb.org/3/movie/popular?api_key=1477348488076cafd4dcf973a314957d&language=ko-KR') const response = await axios.get('https://api.themoviedb.org/3/movie/popular?api_key=1477348488076cafd4dcf973a314957d&language=ko-KR')
const movies = response.data const movies = response.data
...@@ -10,4 +12,17 @@ const comparePopularMovie = async (req, res) => { ...@@ -10,4 +12,17 @@ const comparePopularMovie = async (req, res) => {
} }
} }
export default { comparePopularMovie } const create = async (req, res) => {
\ No newline at end of file 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,
create,
}
\ No newline at end of file
...@@ -12,7 +12,8 @@ const MovieModel = (sequelize) => { ...@@ -12,7 +12,8 @@ const MovieModel = (sequelize) => {
autoIncrement: true, autoIncrement: true,
}, },
movieId: { movieId: {
type: DataTypes.INTEGER type: DataTypes.INTEGER,
unique: true,
} }
}, },
{ {
......
...@@ -8,6 +8,8 @@ router ...@@ -8,6 +8,8 @@ router
.post(movieCtrl.comparePopularMovie) .post(movieCtrl.comparePopularMovie)
.get(movieCtrl.comparePopularMovie) .get(movieCtrl.comparePopularMovie)
router
.route("/:movieId")
.post(movieCtrl.create)
export default router; export default router;
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment