timetable.controller.js 4.13 KB
Newer Older
1
2
import { TimeTable, Theater } from "../db/index.js";
import moment from 'moment';
3
4
5
import sequelize from 'sequelize'
const { Op } = sequelize

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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 || "상영시간표 정보 가져오는 중 에러 발생")
    }
}

31
32
const submit = async (req, res) => {
    try {
Kim, Subin's avatar
Kim, Subin committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
        const { movieId, title, theater, runtime, release_date, date } = req.body
        const result = await Promise.all(
            theater.map(async (theater) => {
                const startTime = getTime(theater.start)
                const endTime = getTime(theater.start, runtime)
                const isTimeTable = await TimeTable.findAll({
                    where: {
                        [Op.and]: [
                            { theater: theater.theater },
                            {
                                [Op.or]: [
                                    { [Op.and]: [{ start_time: { [Op.gt]: startTime } }, { end_time: { [Op.lte]: endTime } }] },
                                    { start_time: { [Op.between]: [startTime, endTime] } }
                                ]
                            }
                        ]
                    }
                })
                if (isTimeTable.length !== 0) return "unvalid"
                else return "valid"
            })
        )
55
        result.map(el => {
Kim, Subin's avatar
Kim, Subin committed
56
            if (el !== "valid") throw new Error("유효하지 않은 데이터입니다. 다시 등록해주시길 바랍니다.")
57
        })
Kim, Subin's avatar
Kim, Subin committed
58
59
60
61
62
63
64
65
66
67
        let curDate = new Date(release_date)
        const endDate = new Date(date)
        do {
            let day = curDate.getDay()
            await Promise.all(
                theater.map(async (theater) => {
                    let partTime = ""
                    if ('06:00' <= theater.start < '10:00') partTime = "morning"
                    else if ('00:00' <= theater.start < '06:00') partTime = "night"
                    else partTime = "day"
68
                    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" })
Kim, Subin's avatar
Kim, Subin committed
69
70
                })
            )
71
72
            curDate.setDate(curDate.getDate() + 1)
        } while (curDate <= endDate)
Kim, Subin's avatar
Kim, Subin committed
73
        res.send("success!")
74
75
76
77
78
    } catch (error) {
        return res.status(500).send(error.message || "상영시간표 저장 중 에러 발생")
    }
}

Kim, Subin's avatar
Kim, Subin committed
79
const getTime = (string, runtime = 0) => {
80
81
82
83
84
    const arr = string.split(':')
    const date = new Date(0, 0, 0, Number(arr[0]), Number(arr[1]) + runtime)
    return date
}

85
86
87
88
89
90
91
92
93
94
95
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 || "상영시간표 삭제 중 에러 발생")
    }
}

96
export default {
97
98
99
    getAll,
    submit,
    remove
100
}