Commit 6b843822 authored by Kim, Subin's avatar Kim, Subin
Browse files

timetable - getAll, remove 기능 추가 (수정 중)

parent ef75ff69
import axios from "axios"; import axios from "axios";
import { baseUrl } from "../utils/baseUrl.js"; import { baseUrl } from "../utils/baseUrl.js";
const getAll = async (selectDate) => {
const { data } = await axios.get(`${baseUrl}/api/timetable?when=${selectDate}`)
return data
}
const submit = async (sendData) => { const submit = async (sendData) => {
const { data } = await axios.post(`${baseUrl}/api/timetable`, sendData) const { data } = await axios.post(`${baseUrl}/api/timetable`, sendData)
return data return data
} }
const remove = async () => {
const { data } = await axios.delete(`${baseUrl}/api/timetable/`)
return data
}
const timetableApi = { const timetableApi = {
submit getAll,
submit,
remove
} }
export default timetableApi export default timetableApi
\ No newline at end of file
import { TimeTable } from "../db/index.js"; import { TimeTable, Theater } from "../db/index.js";
import moment from 'moment';
import sequelize from 'sequelize' import sequelize from 'sequelize'
const { Op } = sequelize const { Op } = sequelize
const getAll = async (req, res) => {
try {
const { when } = req.query
const selectDate = new Date(when)
const theaterArr = []
// const timeTableArr = []
const findAll = await TimeTable.findAll({ where: { date: selectDate }, attributes: { exclude: ['createdAt', 'updatedAt'] }, order: [["theater", "ASC"], ["start_time", "ASC"]] })
findAll.forEach(element => {
if (!theaterArr.includes(element.theater)) theaterArr.push(element.theater)
})
const findTheater = await Theater.findAll({ where: { id: theaterArr }, attributes: { exclude: ['createdAt', 'updatedAt'] }, order: [['theaterName']] })
findTheater.forEach(el => {
const arr = findAll.filter(timetable => {
if (el.id === timetable.theater) return timetable.dataValues
})
el.dataValues.timetable = arr
// timeTableArr.push({ id: el.id, info: arr })
})
return res.json(findTheater)
// return res.json({findTheater, timeTableArr})
} catch (error) {
return res.status(500).send(error.message || "상영시간표 정보 가져오는 중 에러 발생")
}
}
const submit = async (req, res) => { const submit = async (req, res) => {
try { try {
console.log("req.body==", req.body)
const { movieId, title, theater, runtime, release_date, date } = req.body const { movieId, title, theater, runtime, release_date, date } = req.body
const result = await Promise.all( const result = await Promise.all(
theater.map(async (theater) => { theater.map(async (theater) => {
...@@ -27,8 +52,7 @@ const submit = async (req, res) => { ...@@ -27,8 +52,7 @@ const submit = async (req, res) => {
else return "valid" else return "valid"
}) })
) )
result.map((el, index)=> { result.map(el => {
console.log("idx==",index)
if (el !== "valid") throw new Error("유효하지 않은 데이터입니다. 다시 등록해주시길 바랍니다.") if (el !== "valid") throw new Error("유효하지 않은 데이터입니다. 다시 등록해주시길 바랍니다.")
}) })
let curDate = new Date(release_date) let curDate = new Date(release_date)
...@@ -44,7 +68,7 @@ const submit = async (req, res) => { ...@@ -44,7 +68,7 @@ const submit = async (req, res) => {
await TimeTable.create({ theater: theater.theater, movieId, title, release_date, date: curDate, start_time: getTime(theater.start), end_time: getTime(theater.start, runtime), partTime: partTime, week: (day === 0 || day === 6) ? "weekend" : "weekdays" }) await TimeTable.create({ theater: theater.theater, movieId, title, release_date, date: curDate, start_time: getTime(theater.start), end_time: getTime(theater.start, runtime), partTime: partTime, week: (day === 0 || day === 6) ? "weekend" : "weekdays" })
}) })
) )
curDate.setDate(curDate.getDate()+1) curDate.setDate(curDate.getDate() + 1)
} while (curDate <= endDate) } while (curDate <= endDate)
res.send("success!") res.send("success!")
} catch (error) { } catch (error) {
...@@ -58,6 +82,19 @@ const getTime = (string, runtime = 0) => { ...@@ -58,6 +82,19 @@ const getTime = (string, runtime = 0) => {
return date return date
} }
const remove = async (req, res) => {
try {
const { timeId } = req.params
const delNum = await TimeTable.destroy({ where: {} })
if (delNum) res.json(delNum)
else throw new Error("해당 정보를 서버에서 삭제하는데 실패했습니다.")
} catch (error) {
return res.status(500).send(error.message || "상영시간표 삭제 중 에러 발생")
}
}
export default { export default {
submit getAll,
submit,
remove
} }
\ No newline at end of file
...@@ -5,6 +5,11 @@ const router = express.Router(); ...@@ -5,6 +5,11 @@ const router = express.Router();
router router
.route("/") .route("/")
.get(timetableCtrl.getAll)
.post(timetableCtrl.submit) .post(timetableCtrl.submit)
router
.route("/:timeId")
.delete(timetableCtrl.remove)
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