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

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

parent ef75ff69
import axios from "axios";
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 { data } = await axios.post(`${baseUrl}/api/timetable`, sendData)
return data
}
const remove = async () => {
const { data } = await axios.delete(`${baseUrl}/api/timetable/`)
return data
}
const timetableApi = {
submit
getAll,
submit,
remove
}
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'
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) => {
try {
console.log("req.body==", req.body)
const { movieId, title, theater, runtime, release_date, date } = req.body
const result = await Promise.all(
theater.map(async (theater) => {
......@@ -27,8 +52,7 @@ const submit = async (req, res) => {
else return "valid"
})
)
result.map((el, index)=> {
console.log("idx==",index)
result.map(el => {
if (el !== "valid") throw new Error("유효하지 않은 데이터입니다. 다시 등록해주시길 바랍니다.")
})
let curDate = new Date(release_date)
......@@ -41,11 +65,11 @@ const submit = async (req, res) => {
if ('06:00' <= theater.start < '10:00') partTime = "morning"
else if ('00:00' <= theater.start < '06:00') partTime = "night"
else partTime = "day"
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)
} while (curDate <= endDate)
curDate.setDate(curDate.getDate() + 1)
} while (curDate <= endDate)
res.send("success!")
} catch (error) {
return res.status(500).send(error.message || "상영시간표 저장 중 에러 발생")
......@@ -58,6 +82,19 @@ const getTime = (string, runtime = 0) => {
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 {
submit
getAll,
submit,
remove
}
\ No newline at end of file
......@@ -5,6 +5,11 @@ const router = express.Router();
router
.route("/")
.get(timetableCtrl.getAll)
.post(timetableCtrl.submit)
router
.route("/:timeId")
.delete(timetableCtrl.remove)
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